Castle labyrinth HDU - 1269 determines whether there is a strongly connected directed graph in FIG.

In order to train small Greek sense of direction, Gardon built a large castle, inside the room there are N (N <= 10000) and M channels (M <= 100000), each channel is unidirectional, that if called a passage communicating the room a and room B, a room B can be reached only by the described rooms a through this channel, but does not indicate the room can reach a B room through it. Gardon need to ask you to write a program to confirm whether any two rooms are interconnected, namely: for any i and j, there is at least one path from room to room j i, j may also exist a path from room to the room i.

Input input comprising a plurality of sets of data, the first line of input has two numbers: N and M, the next M lines each with two numbers, a and B, may represent a channel from A to B room room. File and end with two zeroes.
Output For each set of input data, if any two rooms are interconnected, the output of "Yes", and otherwise outputs "No".
Sample Input

3 3
1 2
2 3
3 1
3 3
1 2
2 3
3 2
0 0

Sample Output

Yes
No

 

answer:

Title meaning is very clear, it is to allow you to judge this is not a directed graph strongly connected graph

Just make sure two aspects:

1, only one communication block

2, which only block a communication and only one point x low [x] == dfn [x]. Because tarjan process if it is to become a strong connected graph, then we must in addition to all the remaining points of the low x certainly want to point to other points

 

Code:

  1 #include<stdio.h>
  2 #include<string.h>
  3 #include<iostream>
  4 #include<algorithm>
  5 #include<map>
  6 #include<math.h>
  7 #include<set>
  8 #include<queue>
  9 using namespace std;
 10 typedef long long ll;
 11 const int maxn=10005;
 12 const int mod=26;
 13 const int INF=0x3f3f3f3f;
 14 const int block=300;
 15 struct edge
 16 {
 17     int v,next;
 18 } e[100005];
 19 int dfn[maxn],low[maxn],stacks[maxn];
 20 int head[maxn],visit[maxn],cnt,tot,index;
 21 void add_edge(int x,int y)
 22 {
 23     e[cnt].v=y;
 24     e[cnt].next=head[x];
 25     head[x]=cnt++;
 26 }
 27 int tarjan(int x)
 28 {
 29     dfn[x]=low[x]=++tot;
 30     stacks[++index]=x;
 31     visit[x]=1;
 32     for(int i=head[x]; i!=-1; i=e[i].next)
 33     {
 34         if(!dfn[e[i].v])
 35         {
 36             tarjan(e[i].v);
 37             low[x]=min(low[x],low[e[i].v]);
 38         }
 39         else if(visit[e[i].v])
 40         {
 41             low[x]=min(low[x],dfn[e[i].v]);
 42         }
 43     }
 44     if(dfn[x]==low[x])
 45     {
 46         do{
 47             visit[stacks[index]]=0;
 48             index--;
 49         }while(x!=stacks[index+1]);
 50     }
 51 }
 52 int main()
 53 {
 54     int n,m;
 55     while(~scanf("%d%d",&n,&m))
 56     {
 57         if(!n && !m) break;
 58         cnt=0;
 59         for(int i=1;i<=n;++i)
 60         {
 61             head[i]=-1;
 62             visit[i]=0;
 63             dfn[i]=0;
 64             low[i]=0;
 65         }
 66         while(m--)
 67         {
 68             int x,y;
 69             scanf("%d%d",&x,&y);
 70             add_edge(x,y);
 71         }
 72         int flag=0;
 73         for(int i=1; i<=n; ++i)
 74         {
 75             if(!dfn[i])
 76             {
 77                 tarjan(i);
 78                 if(flag)
 79                 {
 80                     flag=2;
 81                     break;
 82                 }
 83                 else flag=1;
 84             }
 85             int ans=0;
 86             for(int i=1;i<=n;++i)
 87             {
 88                 if(dfn[i] && dfn[i]==low[i])
 89                 {
 90                     ans++;
 91                 }
 92                 if(ans>1)
 93                 {
 94                     flag=2;
 95                     break;
 96                 }
 97             }
 98         }
 99         if(flag==1)
100         {
101             printf("Yes\n");
102         }
103         else printf("No\n");
104     }
105     return 0;
106 }
View Code

 

Guess you like

Origin www.cnblogs.com/kongbursi-2292702937/p/11666602.html