标签:other his NPU ali acm mit follow pos add
http://acm.hdu.edu.cn/showproblem.php?pid=2767
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8605 Accepted Submission(s): 3063
1 //Wannafly挑战赛14 C https://www.nowcoder.com/acm/contest/81/C 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 #include<queue> 6 #include<stack> 7 #include<vector> 8 using namespace std; 9 const int maxn=100005; 10 struct edge{ 11 int from; 12 int to; 13 int next; 14 }EDGE[maxn]; 15 vector<int>vc[maxn]; 16 int head[maxn],dfn[maxn],vis[maxn],low[maxn],col[maxn],in[maxn],out[maxn],en[maxn],stk[maxn];//各个变量的意义可参照上篇博客 17 int edge_cnt=1,tot1=0,tot2=0,scc_cnt=0,tot0=0; 18 void add(int x,int y) 19 { 20 EDGE[edge_cnt].from=x; 21 EDGE[edge_cnt].to=y; 22 EDGE[edge_cnt].next=head[x]; 23 head[x]=edge_cnt++; 24 } 25 void Tarjan(int u) 26 { 27 low[u]=dfn[u]=++tot1;//注意tot1的初值必须是1【因为dfn必须为正数】,所以这里使用++tot1而不用tot1++; 28 vis[u]=1; 29 stk[++tot2]=u; 30 for(int i = head[u]; i != -1 ; i = EDGE[i].next) 31 { 32 if(!dfn[EDGE[i].to]){ 33 Tarjan(EDGE[i].to); 34 low[u]=min(low[u],low[EDGE[i].to]); 35 } 36 else if(vis[EDGE[i].to]){ 37 low[u]=min(low[u],low[EDGE[i].to]); 38 } 39 } 40 if(low[u]==dfn[u]){ 41 int xx; 42 scc_cnt++; 43 do{ 44 xx=stk[tot2--]; 45 vc[scc_cnt].push_back(xx); 46 col[xx]=scc_cnt; 47 vis[xx]=0; 48 }while(xx!=u); 49 } 50 } 51 int main() 52 { 53 int t; 54 scanf("%d",&t); 55 while(t--) 56 { 57 edge_cnt=0,tot1=0,tot2=0,scc_cnt=0,tot0=0; 58 scc_cnt=0; 59 int n,m; 60 scanf("%d%d",&n,&m); 61 memset(head,-1,sizeof(head)); 62 memset(stk,0,sizeof(stk)); 63 memset(in,0,sizeof(in)); 64 memset(out,0,sizeof(out)); 65 memset(dfn,0,sizeof(dfn)); 66 memset(low,0,sizeof(low)); 67 memset(col,0,sizeof(col)); 68 while(m--) 69 { 70 int a,b; 71 scanf("%d%d",&a,&b); 72 add(a,b); 73 } 74 for(int i = 1 ; i <= n; i++) 75 { 76 if(!dfn[i])Tarjan(i); 77 } 78 for(int i = 0 ; i < edge_cnt ; i++) 79 { 80 if(col[EDGE[i].from]!=col[EDGE[i].to]) 81 { 82 in[col[EDGE[i].to]]++;//缩点 83 out[col[EDGE[i].from]]++; 84 } 85 } 86 int sum1=0,sum2=0; 87 for(int i = 1 ; i <= scc_cnt ; i++) 88 { 89 if(!in[i]) 90 sum1++; 91 if(!out[i]) 92 sum2++; 93 } 94 int mmax=max(sum1,sum2); 95 if(scc_cnt!=1) 96 cout << mmax << endl; 97 else 98 cout << "0" <<endl; 99 for(int i = 1 ; i <= scc_cnt ; i++) 100 vc[i].clear(); 101 } 102 return 0; 103 } 104 /*4 5 105 1 3 106 2 4 107 4 2 108 1 4 109 2 1*/
标签:other his NPU ali acm mit follow pos add
原文地址:https://www.cnblogs.com/MekakuCityActor/p/9021775.html