登 录
http://poj.org/problem?id=2553
//强连通分支,求出度为0 的强连通分支 #include<iostream> #include<vector> using namespace std; #define M 5001 vector<int> graph[M]; vector<int> graph_t[M]; bool v[M]; int outdegree[M], father[M]; vector<int> finish; int n, m; void dfs1(int now) { v[now] = true; for(int i=0; i<graph[now].size(); i++) if(!v[graph[now][i]]) dfs1(graph[now][i]); finish.push_back(now); } void dfs2(int now,int fa) { v[now] = true; for(int i=0; i<graph_t[now].size(); i++) if(!v[graph_t[now][i]]) { father[graph_t[now][i]] = fa; dfs2(graph_t[now][i],fa); } } int main() { // freopen("in.txt","r",stdin); int i, j, s, t; while(scanf("%d",&n)!=EOF) { if(n==0) break; scanf("%d",&m); for(i=1; i<=n; i++) { graph[i].clear(); graph_t[i].clear(); } for(i=1; i<=m; i++) { scanf("%d %d",&s, &t); graph[s].push_back(t); graph_t[t].push_back(s); } finish.clear(); memset(v,0,sizeof(v)); for(i=1; i<=n; i++) if(!v[i]) dfs1(i); memset(v,0,sizeof(v)); memset(father,0,sizeof(father)); memset(outdegree,0,sizeof(outdegree)); int cnt = 0; for(i=finish.size()-1; i>=0; i--) if(!v[finish[i]]) { cnt++; father[finish[i]] = cnt; dfs2(finish[i],cnt); } /* cout<<cnt<<endl; for(i=1; i<=n; i++) cout<<i<<" "<<father[i]<<endl;*/ for(i=1; i<=n; i++) for(j=0; j<graph[i].size(); j++) if(father[i] != father[graph[i][j]]) outdegree[father[i] ]++; int tt=0; for(i=1; i<=n; i++) if(outdegree[father[i]] == 0) { if(tt != 0) printf(" "); printf("%d",i); tt++; } printf("/n"); } return 0; }
抱歉!评论已关闭.