HDU 3594 Cactus(仙人掌图模板)

Cactus

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1934    Accepted Submission(s): 891

Problem Description
1. It is a Strongly Connected graph.
2. Each edge of the graph belongs to a circle and only belongs to one circle.
We call this graph as CACTUS.



There is an example as the figure above. The left one is a cactus, but the right one isn’t. Because the edge (0, 1) in the right graph belongs to two circles as (0, 1, 3) and (0, 1, 2, 3).
 

 

Input
The input consists of several test cases. The first line contains an integer T (1<=T<=10), representing the number of test cases.
For each case, the first line contains a integer n (1<=n<=20000), representing the number of points.
The following lines, each line has two numbers a and b, representing a single-way edge (a->b). Each case ends with (0 0).
Notice: The total number of edges does not exceed 50000.
 
Output
For each case, output a line contains “YES” or “NO”, representing whether this graph is a cactus or not.

Sample Input
2
4
0 1
1 2
2 0
2 3
3 2
0 0
4
0 1
1 2
2 3
3 0
1 3
0 0
 
Sample Output
YES
NO
 
仙人掌图的条件:(1)此图是强连通图;(2)此图每条边仅被一个环所包含
后面的性质1,2,3的解释在这里(
  1 #include <iostream>
  2 #include <vector> 
  3 #include <cstring>
  4 #include <stack>
  5 #define N 20010
  6 using namespace std;
  7 int n,m;
  8 bool flag;
  9 vector<int> mapp[N];
 10 stack<int> s;
 11 int dfn[N],low[N],sccno[N],ins[N],scc_cnt,indx;
 12 bool vis[N];
 13 void init(){
 14 for(int i=0;i<=n;i++){
 15         mapp[i].clear();
 16     }
 17     while(!s.empty()){
 18         s.pop();
 19     }
 20     flag=true;
 21     memset(vis,false,sizeof(vis));
 22     memset(sccno,0,sizeof(sccno));
 23     memset(low,0,sizeof(low));
 24     memset(dfn,0,sizeof(dfn));
 25     memset(ins,0,sizeof(ins));
 26 }
 27 void tarbfs(int u){
 28     indx++;
 29     dfn[u]=indx;
 30     low[u]=indx;
 31     ins[u]=1;
 32     s.push(u);
 33     int sum=0;
 34     int size=mapp[u].size();
 35     for(int i=0;i<size&&flag;i++){ 
 36         int v=mapp[u][i];
 37         if(vis[v]){//1
 38             flag=false;
 39             break;
 40         }
 41         if(dfn[v]==0){
 42             tarbfs(v);
 43             if(low[v]>dfn[u]){//2
 44                 flag=false;
 45                 break;
 46             }
 47             if(low[v]<dfn[u]){
 48                 sum++;
 49             }
 50             if(sum==2){//3
 51                 flag=false;
 52                 break;    
 53             }
 54             low[u]=min(low[u],low[v]);
 55         }
 56         else if(ins[v]){
 57             low[u]=min(low[u],dfn[v]);
 58             sum++;
 59             if(sum==2){
 60                 flag=false;
 61                 break;    
 62             }
 63         }
 64     }
 65     if(low[u]==dfn[u]&&flag){
 66         scc_cnt++;
 67         int x;
 68         do{
 69             if(s.empty()){
 70                 break;
 71             }
 72             x=s.top();
 73             s.pop();
 74             ins[x]=0;
 75             sccno[x]=scc_cnt;
 76         }while(x!=u);
 77     }
 78     vis[u]=true;
 79 }
 80 void Tarjan(){
 81     indx=0;
 82     scc_cnt=0;
 83     for(int i=1;i<=n&&flag;i++){
 84         if(!dfn[i]){
 85             tarbfs(i);
 86         }
 87     }
 88 }
 89 int main(){
 90     cin.sync_with_stdio(false);
 91     int a,b;
 92     int T;
 93     cin>>T;
 94     while(T--){
 95         cin>>n;
 96         init();
 97         while(cin>>a>>b&&(a||b)){
 98             mapp[a].push_back(b);
 99         }
100         Tarjan();
101         if(scc_cnt==1||flag){
102             cout<<"YES"<<endl;
103         }
104         else{
105             cout<<"NO"<<endl;
106         }
107     }
108     return 0;
109 }
2017-03-03 22:53:53
posted @ 2017-03-03 22:56  ガ落涙『不變』  阅读(116)  评论(0编辑  收藏  举报