Maze Castle
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 10156 Accepted Submission (s): 4570
Problem description in order to train the sense of direction, Gardon built a large castle, there are N rooms (n<=10000) and M-channel (m<=100000), each channel is one-way, That is, if a channel is said to be connected to room A and room B, only the room B can be reached by this passage, but it does not mean that it can be reached by Room B. Gardon need to ask you to write a procedure to confirm whether any two rooms are interconnected, namely: for arbitrary I and J, there is at least one path can be from room I to room J, there is a path can be from room J to room I.
The input input contains more than one set of data, the first line of inputs has two numbers: N and M, and the next m row has two numbers a and B, indicating that a passage can come from room A to room B. The file ends with two 0.
Output for each set of data entered, if any two rooms are connected to each other, outputs "Yes", otherwise output "No".
Sample Input3 31 22 33 13 31 22 33 20 0
Sample Outputyesno
Looked for two days still did not understand but I desperately want a problem, look at others write, tomorrow continue to see
#include <stdio.h> #include <string.h> #include <stack> #include <algorithm> #define MAX 10010# Define MAXM 100010using namespace Std;int ans;int n,m;int low[max],dfn[max];int instack[max],scccnt;int clock;stack< int>s;struct node {int beg,end,next;} Edge[maxm];int head[max];void Add (int beg,int end) {Edge[ans].beg=beg;edge[ans].end=end;edge[ans].next=head[beg]; head[beg]=ans++;} void Init () {memset (head,-1,sizeof (head)); ans=0;} void Tarjan (int u,int fa) {int v;low[u]=dfn[u]=++clock;s.push (u); instack[u]=1;for (int i=head[u];i!=-1;i=edge[i].next {v=edge[i].end;if (!dfn[v]) {Tarjan (v,u); Low[u]=min (Low[u],low[v]);} else if (Instack[v]) low[u]=min (Low[u],dfn[v]);} if (Low[u]==dfn[u]) {scccnt++;while (1) {v=s.top (); S.pop (); instack[v]=0;if (v==u) Break;}}} void Solve (int l,int r) {memset (instack,0,sizeof (Instack)); Memset (low,0,sizeof (Low)); Memset (Dfn,0,sizeof (DFN)); scccnt=clock=0;for (int i=l;i<=r;i++) if (!dfn[i]) Tarjan (i,-1), if (scccnt==1) printf ("yes\n"); else printF ("no\n");} int main () {int i,j,a,b;while (scanf ("%d%d", &n,&m), n|m) {init (); while (m--) {scanf ("%d%d", &a,&b); Add ( b);} Solve (1,n);} return 0;}
Hdoj 1269 Maze Castle "SCC Basics Topic"