标签:title scanf eve special idt test chm pen next
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1060 Accepted Submission(s): 506
3 5 5 1 2 2 3 2 4 3 5 4 5 3 3 1 2 2 3 3 1 5 5 1 2 2 3 3 1 3 4 4 5
Light my fire! I love you my love and our love save us! I love you my love and our love save us!
1 #include <iostream> 2 #include <bits/stdc++.h> 3 using namespace std; 4 const int MAXN=1010; 5 const int MAXM=6010; 6 struct Edge{ 7 int to,next; 8 }edge[MAXM],edge2[MAXM]; 9 int head[MAXN],head2[MAXN],tot,tot2; 10 int Low[MAXN],DFN[MAXN],Stack[MAXN],Belong[MAXN]; 11 int Index,top; 12 int scc; 13 bool Instack[MAXN]; 14 int num[MAXN]; 15 int in[MAXN],out[MAXN]; 16 void addedge(int u,int v){ 17 edge[tot].to=v;edge[tot].next=head[u];head[u]=tot++; 18 } 19 void addedge2(int u,int v){ 20 edge2[tot2].to=v;edge2[tot2].next=head2[u];head2[u]=tot2++; 21 } 22 void Tarjan(int u){ 23 int v; 24 Low[u]=DFN[u]=++Index; 25 Stack[top++]=u; 26 Instack[u]=true; 27 for(int i=head[u];i!=-1;i=edge[i].next){ 28 v=edge[i].to; 29 if(!DFN[v]){ 30 Tarjan(v); 31 if(Low[u]>Low[v])Low[u]=Low[v]; 32 } 33 else if(Instack[v]&&Low[u]>DFN[v]) 34 Low[u]=DFN[v]; 35 } 36 if(Low[u]==DFN[u]){ 37 scc++; 38 do{ 39 v=Stack[--top]; 40 Instack[v]=false; 41 Belong[v]=scc; 42 num[scc]++; 43 } 44 while(v!=u); 45 } 46 } 47 void solve(int N){ 48 memset(DFN,0,sizeof(DFN)); 49 memset(Instack,false,sizeof(Instack)); 50 memset(num,0,sizeof(num)); 51 Index=scc=top=0; 52 for(int i=1;i<=N;i++){ 53 if(!DFN[i]) 54 Tarjan(i); 55 } 56 } 57 58 bool map2[MAXN][MAXN]; 59 void build(int n){ 60 memset(map2,false,sizeof(map2)); 61 memset(in,0,sizeof(in)); 62 memset(out,0,sizeof(out)); 63 memset(head2,-1,sizeof(head2));tot2=0; 64 for(int i=1;i<=n;i++){ 65 for(int j=head[i];j!=-1;j=edge[j].next){ 66 int v=edge[j].to; 67 int a=Belong[i]; 68 int b=Belong[v]; 69 if(a==b)continue; 70 if(!map2[a][b]){ 71 addedge2(a,b); 72 map2[a][b]=true; 73 in[b]++;out[a]++; 74 } 75 } 76 } 77 } 78 79 void init(){ 80 tot=0; 81 memset(head,-1,sizeof(head)); 82 } 83 84 bool Top(){ 85 queue<int >q; 86 while(!q.empty())q.pop(); 87 for(int i=1;i<=scc;i++){ 88 if(in[i]==0)q.push(i); 89 } 90 91 while(!q.empty()){ 92 if(q.size()!=1)return false; 93 int u=q.front(); 94 q.pop(); 95 for(int i=1;i<=scc;i++){ 96 if(map2[u][i]==true) { 97 in[i]--; 98 if(in[i]==0)q.push(i); 99 } 100 } 101 } 102 return true; 103 } 104 105 int n,m; 106 int main() 107 { 108 int T; 109 scanf("%d",&T); 110 while(T--){ 111 scanf("%d%d",&n,&m); 112 init(); 113 for(int i=0;i<m;i++){ 114 int u,v; 115 scanf("%d%d",&u,&v); 116 addedge(u,v); 117 } 118 solve(n); 119 build(n); 120 121 if(!Top()){printf("Light my fire!\n");} 122 else printf("I love you my love and our love save us!\n"); 123 } 124 125 return 0; 126 }
2017 Multi-University Training Contest - Team 9 1005&&HDU 6165 FFF at Valentine【强联通缩点+拓扑排序】
标签:title scanf eve special idt test chm pen next
原文地址:http://www.cnblogs.com/ECJTUACM-873284962/p/7467299.html