标签:
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 15494 | Accepted: 4100 |
Description
Input
Output
Sample Input
1 3 3 1 2 2 3 3 1
Sample Output
Yes
Source
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 #include <vector> 6 #include <queue> 7 #include <cmath> 8 #include <set> 9 #include <stack> 10 using namespace std; 11 12 #define N 1005 13 14 int max(int x,int y){return x>y?x:y;} 15 int min(int x,int y){return x<y?x:y;} 16 int abs(int x,int y){return x<0?-x:x;} 17 18 int n, m; 19 bool in[N]; 20 int suo[N]; 21 int cnt; 22 vector<int>ve[N]; 23 int dfn[N], low[N], Time; 24 stack<int>st; 25 int father[N]; 26 27 int findroot(int u){ 28 if(father[u]!=u) father[u]=findroot(father[u]); 29 return father[u]; 30 } 31 32 void tarjan(int u){ 33 dfn[u]=low[u]=Time++; 34 st.push(u);in[u]=true; 35 int i, j, k; 36 for(i=0;i<ve[u].size();i++){ 37 int v=ve[u][i]; 38 if(dfn[v]==-1){ 39 tarjan(v); 40 low[u]=min(low[u],low[v]); 41 } 42 else if(in[v]) low[u]=min(low[u],dfn[v]); 43 } 44 if(dfn[u]==low[u]){ 45 while(1){ 46 int x=st.top(); 47 suo[x]=cnt; 48 in[x]=false; 49 st.pop(); 50 if(x==u||st.empty()) break; 51 } 52 cnt++; 53 } 54 } 55 56 main() 57 { 58 int i, j, k, u, v; 59 int t; 60 cin>>t; 61 62 while(t--){ 63 scanf("%d %d",&n,&m); 64 for(i=0;i<=n;i++) ve[i].clear(); 65 for(i=0;i<m;i++){ 66 scanf("%d %d",&u,&v); 67 ve[u].push_back(v); 68 } 69 Time=cnt=1; 70 memset(dfn,-1,sizeof(dfn)); 71 while(!st.empty()) st.pop(); 72 memset(in,false,sizeof(in)); 73 for(i=1;i<=n;i++){ 74 if(dfn[i]==-1){ 75 tarjan(i); 76 } 77 } 78 int inn[N], out[N]; 79 memset(inn,0,sizeof(inn)); 80 memset(out,0,sizeof(out)); 81 for(i=1;i<=n;i++) father[i]=i; 82 for(i=1;i<=n;i++){ 83 for(j=0;j<ve[i].size();j++){ 84 int u=suo[i], v=suo[ve[i][j]]; 85 if(u!=v){ 86 father[findroot(v)]=findroot(u); 87 inn[v]++; 88 out[u]++; 89 } 90 } 91 } 92 93 int num=0; 94 for(i=1;i<cnt;i++){ 95 if(father[i]==i) num++; 96 } 97 if(num>1) { 98 printf("No\n");continue; 99 } 100 101 int n1, n2; 102 n1=n2=0; 103 for(i=1;i<cnt;i++){ 104 if(!inn[i]) n1++; 105 if(!out[i]) n2++; 106 } 107 if(n1>1||n2>1) printf("No\n"); 108 else printf("Yes\n"); 109 } 110 }
标签:
原文地址:http://www.cnblogs.com/qq1012662902/p/4637805.html