标签:
Description
Input
Output
Sample Input
7 7 1 2 2 3 3 4 2 5 4 5 5 6 5 7
Sample Output
2
Hint
1 2 3Building new paths from 1 to 6 and from 4 to 7 satisfies the conditions.
+---+---+
| |
| |
6 +---+---+ 4
/ 5
/
/
7 +
1 2 3Check some of the routes:
+---+---+
: | |
: | |
6 +---+---+ 4
/ 5 :
/ :
/ :
7 + - - - -
#include<cstdio> #include<iostream> #define LL long long using namespace std; inline LL read() { LL x=0,f=1;char ch=getchar(); while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} return x*f; } struct edge{int to,next;}e[1000010]; int n,m,cnt=1,cnt3,tt,sum; int head[100010]; int dfn[100010],low[100010],belong[100010]; int zhan[100010],top; bool inset[100010]; int I[100010],O[100010]; inline void ins(int u,int v) { e[++cnt].to=v; e[cnt].next=head[u]; head[u]=cnt; } inline void insert(int u,int v) { ins(u,v); ins(v,u); } inline void dfs(int x,int fa) { zhan[++top]=x;inset[x]=1; dfn[x]=low[x]=++tt; for(int i=head[x];i;i=e[i].next) if (i!=(fa^1)) if (!dfn[e[i].to]) { dfs(e[i].to,i); low[x]=min(low[x],low[e[i].to]); }else if (inset[e[i].to])low[x]=min(low[x],dfn[e[i].to]); if (low[x]==dfn[x]) { cnt3++; int p=-1; while (p!=x) { p=zhan[top--]; belong[p]=cnt3; inset[p]=0; } } } inline void tarjan() { for (int i=1;i<=n;i++)if (!dfn[i])dfs(i,0); } int main() { n=read();m=read(); for (int i=1;i<=m;i++) { int x=read(),y=read(); insert(x,y); } tarjan(); for (int i=1;i<=n;i++) for (int j=head[i];j;j=e[j].next) if (belong[i]!=belong[e[j].to]) { O[belong[i]]++; I[belong[e[j].to]]++; } for (int i=1;i<=cnt3;i++) if (I[i]==1)sum++; printf("%d\n",(sum+1)/2); }
标签:
原文地址:http://www.cnblogs.com/zhber/p/4175890.html