标签:get ems none open target think one void clu
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 5 int n,m; 6 const int maxn=1e5+2; 7 const int maxm=2*maxn; 8 struct nodeg 9 { 10 int to; 11 int nxt; 12 }eg[maxm]; 13 int headg[maxn]; 14 int headgr[maxn]; 15 struct nodegr 16 { 17 int to; 18 int nxt; 19 }egr[maxn]; 20 int totg; 21 int totgr; 22 int id=0; 23 int num[maxn]; 24 bool vis[maxn]; 25 int root; 26 int cnt; 27 void init() 28 { 29 memset(headg,-1,sizeof(headg)); 30 memset(headgr,-1,sizeof(headgr)); 31 totg=0; 32 totgr=0; 33 cnt=0; 34 } 35 void addg(int u,int v) 36 { 37 eg[totg].to=v; 38 eg[totg].nxt=headg[u]; 39 headg[u]=totg++; 40 } 41 void addgr(int u,int v) 42 { 43 egr[totgr].to=v; 44 egr[totgr].nxt=headgr[u]; 45 headgr[u]=totgr++; 46 } 47 vector<int> scc[maxn]; 48 void DFS(int u) 49 { 50 vis[u]=true; 51 for(int i=headg[u];i!=-1;i=eg[i].nxt) 52 { 53 int v=eg[i].to; 54 if(!vis[v]) 55 { 56 DFS(v); 57 } 58 } 59 num[++id]=u; 60 } 61 void RDFS(int u,int k) 62 { 63 vis[u]=true; 64 scc[k].push_back(u); 65 for(int i=headgr[u];i!=-1;i=egr[i].nxt) 66 { 67 int v=egr[i].to; 68 if(!vis[v]) 69 { 70 RDFS(v,k); 71 } 72 } 73 } 74 void SCC() 75 { 76 memset(vis,false,sizeof(vis)); 77 for(int i=1;i<=n;i++) 78 { 79 if(!vis[i]) 80 { 81 DFS(i); 82 } 83 } 84 memset(vis,false,sizeof(vis)); 85 for(int i=1;i<=n;i++) 86 { 87 scc[i].clear(); 88 } 89 cnt=0; 90 for(int i=id;i>=1;i--) 91 { 92 if(!vis[num[i]]) RDFS(num[i],++cnt); 93 } 94 } 95 void printSC() 96 { 97 for(int i=1;i<=cnt;i++) 98 { 99 int sz=scc[i].size(); 100 for(int j=0;j<sz;j++) 101 printf("%d ",scc[i][j]); 102 puts(""); 103 } 104 } 105 106 int main() 107 { 108 while(~scanf("%d%d",&n,&m)) 109 { 110 init(); 111 int u,v; 112 for(int i=1;i<=m;i++) 113 { 114 scanf("%d%d",&u,&v); 115 addg(u,v); 116 addgr(v,u); 117 } 118 SCC(); 119 printSC(); 120 } 121 122 return 0; 123 }
http://www.cnblogs.com/llhthinker/p/4954082.html
标签:get ems none open target think one void clu
原文地址:http://www.cnblogs.com/itcsl/p/7764451.html