标签:open -- color rank 传递闭包 return cto max 节点
#include <bits/stdc++.h> using namespace std; const int maxn=100000+15; int n,m; vector <int> edge[maxn],rev[maxn]; int tim,rank[maxn],ref[maxn]; bool boo[maxn]; int color[maxn],tot; int dfs1(int now) { if (boo[now]) return 0; boo[now]=true; for (int i=0;i<edge[now].size();dfs1(edge[now][i]),i++); rank[now]=++tim; ref[tim]=now; return 0; } int dfs2(int now) { if (color[now]!=0) return 0; color[now]=tot; for (int i=0;i<rev[now].size();dfs2(rev[now][i]),i++); return 0; } int main() { freopen("ko.in","r",stdin); scanf("%d%d",&n,&m); for (int i=1;i<=m;i++) { int x,y; scanf("%d%d",&x,&y); edge[x].push_back(y); rev[y].push_back(x); } for (int i=1;i<=n;i++) if (!boo[i]) dfs1(i); for (int i=n;i>=1;i--) if (color[ref[i]]==0) { tot++; dfs2(ref[i]); } for (int i=1;i<=n;i++) printf("%d ",color[i]); return 0; }
标签:open -- color rank 传递闭包 return cto max 节点
原文地址:http://www.cnblogs.com/9pounds15pence/p/6349697.html