标签:logs continue blog head struct href net string targa
传送门:http://poj.org/problem?id=1236
实现代码:
1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 #include <map> 5 using namespace std; 6 7 const int MAXN=10010; 8 const int MAXM=50010; 9 10 struct Edge{ 11 int to; 12 int next; 13 }edge[MAXM]; 14 15 int head[MAXN]; 16 int tot; 17 18 void addEdge(int u,int v){ 19 edge[tot].to=v; 20 edge[tot].next=head[u]; 21 head[u]=tot++; 22 } 23 24 void init(){ 25 memset(head,-1,sizeof(head)); 26 tot=0; 27 } 28 29 int LOW[MAXN],DFS[MAXN],Stack[MAXN],Num[MAXN],Belong[MAXN]; 30 bool Instack[MAXN]; 31 int Index; 32 int scc; 33 int top; 34 35 void Targan(int u){ 36 LOW[u]=DFS[u]=++Index; 37 Stack[top++]=u; 38 Instack[u]=true; 39 40 int v; 41 for(int i=head[u];i!=-1;i=edge[i].next){ 42 v=edge[i].to; 43 if(!DFS[v]){ 44 Targan(v); 45 if(LOW[u]>LOW[v]) 46 LOW[u]=LOW[v]; 47 }else if(Instack[v]&&LOW[u]>DFS[v]){ 48 LOW[u]=DFS[v]; 49 } 50 } 51 52 if(LOW[u]==DFS[u]){ 53 scc++; 54 55 do{ 56 v=Stack[--top]; 57 Instack[v]=false; 58 Belong[v]=scc; 59 Num[scc]++; 60 }while(v!=u); 61 } 62 } 63 64 void Solve(int n){ 65 memset(Instack,false,sizeof(Instack)); 66 memset(DFS,0,sizeof(DFS)); 67 memset(Num,0,sizeof(Num)); 68 69 Index=scc=top=0; 70 for(int i=1;i<=n;i++) 71 if(!DFS[i]) 72 Targan(i); 73 } 74 75 map<int,int>mp[MAXN]; 76 int out[MAXN],in[MAXN]; 77 int main(){ 78 int n; 79 while(scanf("%d",&n)!=EOF){ 80 init(); 81 82 for(int i=1;i<=n;i++){ 83 int v; 84 while(scanf("%d",&v)&&v){ 85 addEdge(i,v); 86 mp[i][v]=1; 87 } 88 } 89 Solve(n); 90 if(scc==1){ 91 printf("1\n0\n"); 92 continue; 93 } 94 95 for(int i=1;i<=n;i++) 96 for(int j=1;j<=n;j++){ 97 if(mp[i][j]&&Belong[i]!=Belong[j]){ 98 out[Belong[i]]++; 99 in[Belong[j]]++; 100 } 101 } 102 103 int ans1=0,ans2=0; 104 for(int i=1;i<=scc;i++){ 105 if(in[i]==0) ans1++; 106 if(out[i]==0) ans2++; 107 } 108 109 printf("%d\n%d\n",ans1,max(ans1,ans2)); 110 111 112 } 113 }
标签:logs continue blog head struct href net string targa
原文地址:http://www.cnblogs.com/IKnowYou0/p/6526916.html