POJ 3352 Road Construction


#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #define maxn 1005 using namespace std; int num,to[maxn<<1],nxt[maxn<<1],last[maxn],note[maxn<<1]; int dfn[maxn],low[maxn],cnt; int col[maxn],tot,cal,ans; void tarjan(int x,int fa) { dfn[x]=low[x]=++cnt; for (int i=last[x];i;i=nxt[i]) { if (!dfn[to[i]]) { tarjan(to[i],x); low[x]=min(low[x],low[to[i]]); if (low[to[i]]>dfn[x]) { note[i]=1; if (i%2==0) note[i-1]=1; else note[i+1]=1; } } else if (dfn[to[i]]<dfn[x] && to[i]!=fa) low[x]=min(low[x],dfn[to[i]]); } } void dfs(int x) { col[x]=tot; for(int i=last[x];i;i=nxt[i]) { if(!col[to[i]] && !note[i]) dfs(to[i]); if (note[i]) cal++; } } void add(int x,int y) { to[++num]=y;nxt[num]=last[x];last[x]=num;note[num]=0; } int main() { int x,y,i,n,m; while (scanf("%d%d",&n,&m)!=EOF) { num=0;cnt=0;tot=0;ans=0; memset(last,0,sizeof(last)); memset(dfn,0,sizeof(dfn)); memset(col,0,sizeof(col)); for (i=1;i<=m;i++) { scanf("%d%d",&x,&y); add(x,y);add(y,x); } for (i=1;i<=n;i++) if (!dfn[i]) tarjan(i,0); for (i=1;i<=n;i++) if (!col[i]) { tot++;cal=0; dfs(i); if (cal==1) ans++; } printf("%d ",(ans+1)/2); } return 0; }
原文地址:https://www.cnblogs.com/lsykk/p/12295854.html