hdu1269(强联通分量模版)

来源:互联网 发布:root刷机软件 编辑:程序博客网 时间:2024/05/16 04:48

注意事项在代码中

#include<cstdio>#include<algorithm>#include<cstring>using namespace std;int head[100009],tot,dfn[100009],low[100009],sta[1000009],top,n,m,pre[300009],edge[300009];int ans=0,id=0;bool s[100009],b[100009];void add(int x,int y){tot++;edge[tot]=y;pre[tot]=head[x];head[x]=tot;}void dfs(int u){dfn[u]=low[u]=++id;sta[++top]=u;s[u]=true;for (int i=head[u];i;i=pre[i])//遍历if (!dfn[edge[i]]){dfs(edge[i]);low[u]=min(low[u],low[edge[i]]);}else if (s[edge[i]]) low[u]=min(low[u],dfn[edge[i]]);if (low[u]==dfn[u])//找到强联通分量{while (u!=sta[top]){s[sta[top]]=false;top--;}s[sta[top]]=false;top--;/////以上均为出栈ans++;//强联通分量个数}}int main(){while (scanf("%d%d",&n,&m)){if (n==0&&m==0) break;int x,y;ans=0;memset(low,0,sizeof(low));memset(dfn,0,sizeof(dfn));memset(sta,0,sizeof(sta));memset(s,false,sizeof(s));memset(head,0,sizeof(head));//每一次都一定要付0tot=0;top=0;for (int i=1;i<=m;i++) scanf("%d%d",&x,&y),add(x,y);for (int i=1;i<=n;i++) if (!dfn[i]) dfs(i);//需用遍历一遍!!if (ans==1) printf("Yes\n");else printf("No\n");}return 0;}


0 0