标签:问题 tput 链接 roo case lin esc 题意 else
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 17089 | Accepted: 4590 |
Description
Input
Output
Sample Input
1 3 3 1 2 2 3 3 1
Sample Output
Yes
Source
1 #include<stdio.h> 2 #include<string.h> 3 #include<queue> 4 using namespace std; 5 queue<int> q; 6 struct list 7 { 8 int v; 9 list *next; 10 }; 11 list *head[10010],*rear[10010],*map_head[10010],*map_rear[10010]; 12 int stack[10010],s[10010],dfn[10010],low[10010],indegree[10010]; 13 bool instack[10010]; 14 int top,cnt,times; 15 void tarjian(int v) 16 { 17 dfn[v]=low[v]=++times; 18 stack[top++]=v; 19 instack[v]=true; 20 for(list *p=head[v];p!=NULL;p=p->next) 21 if(!dfn[p->v]) 22 { 23 tarjian(p->v); 24 if(low[p->v]<low[v]) low[v]=low[p->v]; 25 } else if(low[p->v]<low[v]&&instack[p->v]) low[v]=low[p->v]; 26 if(dfn[v]==low[v]) 27 { 28 ++cnt; 29 do 30 { 31 v=stack[--top]; 32 instack[v]=false; 33 s[v]=cnt; 34 }while(dfn[v]!=low[v]); 35 } 36 return; 37 } 38 bool topsort() 39 { 40 int count=0,i; 41 while(!q.empty()) q.pop(); 42 for(i=1;i<=cnt;++i) 43 { 44 if(!indegree[i]) 45 { 46 ++count; 47 q.push(i); 48 } 49 } 50 if(count>1) return false; 51 while(!q.empty()) 52 { 53 int u=q.front(); 54 q.pop(); 55 count=0; 56 for(list *p=map_head[u];p!=NULL;p=p->next) 57 { 58 --indegree[p->v]; 59 if(!indegree[p->v]) 60 { 61 ++count; 62 q.push(p->v); 63 } 64 } 65 if(count>1) return false; 66 } 67 return true; 68 } 69 int main() 70 { 71 int T,n,m,i,u,v,a,b,num; 72 scanf("%d",&T); 73 while(T--) 74 { 75 memset(head,0,sizeof(head)); 76 memset(rear,0,sizeof(rear)); 77 scanf("%d%d",&n,&m); 78 for(i=0;i<m;++i) 79 { 80 scanf("%d%d",&u,&v); 81 if(rear[u]!=NULL) 82 { 83 rear[u]->next=new list; 84 rear[u]=rear[u]->next; 85 }else head[u]=rear[u]=new list; 86 rear[u]->v=v; 87 rear[u]->next=NULL; 88 } 89 top=times=cnt=0; 90 memset(dfn,0,sizeof(dfn)); 91 memset(low,0,sizeof(low)); 92 memset(stack,0,sizeof(stack)); 93 memset(instack,false,sizeof(instack)); 94 memset(s,0,sizeof(s)); 95 for(i=1;i<=n;++i) if(!dfn[i]) tarjian(i); 96 memset(map_head,0,sizeof(map_head)); 97 memset(map_rear,0,sizeof(map_rear)); 98 memset(indegree,0,sizeof(indegree)); 99 for(i=1;i<=n;++i) 100 for(list *p=head[i];p!=NULL;p=p->next) 101 if(s[i]!=s[p->v]) 102 { 103 ++indegree[s[p->v]]; 104 if(map_rear[s[i]]!=NULL) 105 { 106 map_rear[s[i]]->next=new list; 107 map_rear[s[i]]=map_rear[s[i]]->next; 108 } else map_head[s[i]]=map_rear[s[i]]=new list; 109 map_rear[s[i]]->v=s[p->v]; 110 map_rear[s[i]]->next=NULL; 111 } 112 if(topsort()) printf("Yes\n"); 113 else printf("No\n"); 114 } 115 return 0; 116 }
[强连通分量] POJ 2762 Going from u to v or from v to u?
标签:问题 tput 链接 roo case lin esc 题意 else
原文地址:http://www.cnblogs.com/fuermowei-sw/p/6143295.html