我以前的Tarjan模版,全都敲错了。。。一个v写成了u,而且都AC了。。。
1 /* 2 ID: cuizhe 3 LANG: C++ 4 TASK: schlnet 5 */ 6 #include <iostream> 7 #include <cstring> 8 #include <cstdio> 9 #include <cstdlib> 10 using namespace std; 11 #define N 10001 12 #define M 500001 13 struct node 14 { 15 int u,v,next; 16 } edge[M+10]; 17 int t,top,scnt; 18 int DFN[N+1],Low[N+1],Belong[N+1],stack[N+1],first[N+1],count[N+1]; 19 int in[N+1],input[N+1],output[N+1]; 20 int quex[M+1],quey[M+1]; 21 void CL() 22 { 23 t = 0; 24 t = scnt = top = 0; 25 memset(first,-1,sizeof(first)); 26 memset(DFN,0,sizeof(DFN)); 27 memset(input,0,sizeof(input)); 28 memset(output,0,sizeof(output)); 29 memset(count,0,sizeof(count)); 30 } 31 void add(int u,int v) 32 { 33 edge[t].u = u; 34 edge[t].v = v; 35 edge[t].next = first[u]; 36 first[u] = t ++; 37 } 38 void Tarjan(int u) 39 { 40 int v,i; 41 DFN[u] = Low[u] = ++ t; 42 in[u] = 1; 43 stack[top++] = u; 44 for(i = first[u];i != -1;i = edge[i].next) 45 { 46 v = edge[i].v; 47 if(!DFN[v]) 48 { 49 Tarjan(v); 50 if(Low[u] > Low[v]) 51 { 52 Low[u] = Low[v]; 53 } 54 } 55 else if(in[v] && Low[u] > DFN[v]) 56 { 57 Low[u] = DFN[v]; 58 } 59 } 60 if(DFN[u] == Low[u]) 61 { 62 scnt ++; 63 do 64 { 65 v = stack[--top]; 66 in[v] = 0; 67 Belong[v] = scnt; 68 }while(u != v); 69 } 70 } 71 int main() 72 { 73 int n,ev,i,tp; 74 freopen("schlnet.in","r",stdin); 75 freopen("schlnet.out","w",stdout); 76 scanf("%d",&n); 77 CL(); 78 tp = 0; 79 for(i = 1; i <= n; i ++) 80 { 81 for(;;) 82 { 83 scanf("%d",&ev); 84 if(ev == 0) break; 85 quex[tp] = i; 86 quey[tp] = ev; 87 tp ++; 88 add(i,ev); 89 } 90 } 91 for(i = 1; i <= n; i ++) 92 { 93 if(!DFN[i]) 94 { 95 Tarjan(i); 96 } 97 } 98 for(i = 0; i < tp; i ++) 99 { 100 if(Belong[quex[i]] != Belong[quey[i]]) 101 { 102 int x = Belong[quex[i]]; 103 int y = Belong[quey[i]]; 104 output[x] = 1; 105 input[y] = 1; 106 } 107 } 108 int num1,num2; 109 num1 = num2 = 0; 110 if(scnt == 1) 111 { 112 printf("1\n0\n"); 113 return 0; 114 } 115 for(i = 1; i <= scnt; i ++) 116 { 117 if(input[i] == 0) 118 { 119 num1 ++; 120 } 121 if(output[i] == 0) 122 { 123 num2 ++; 124 } 125 } 126 printf("%d\n",num1); 127 printf("%d\n",max(num1,num2)); 128 return 0; 129 }