Maze Castle
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 10908 Accepted Submission (s): 4887
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
Authorgardon
Sourcehdu 2006-4 Programming Contest
RECOMMENDLXJ | We have carefully selected several similar problems for you:1233 1116 1150 1301 1272 The template problem of the strong Unicom algorithm-test the template once again the DFS tag comes out in the order of backtracking (l Eft_time array) Then DFS is based on the order of backtracking from the point where the first backtracking occurred, testing the current connectivity situation
#include <cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<algorithm>#include<string>#include<map>#include<stack>#include<Set>#include<iostream>#include<vector>#include<queue>//Ios_base::sync_with_stdio (false);//#pragma COMMENT (linker, "/stack:1024000000,1024000000")using namespacestd;#defineRep (I, A, b) for (int i = (a); I < (b); ++i)#defineREPF (I, A, b) for (int i = (a); I <= (b); ++i)#defineREPD (I, A, b) for (int i = (a); I >= (b); i.)#defineCLR (x) memset (x,0,sizeof (x))#defineSQR (x) ((x) * (x))typedefLong LongLL;Const intINF =1000000000;Const DoubleEPS = 1e-8;Const intMAXN =10000+Ten;Const intMoD =1000000000+7;intVIS[MAXN];intLEFT_TIME[MAXN];intN,m;vector<int>G[MAXN];intMax_time;voidDfsintN) { if(Vis[n])return; Vis[n]=1; Rep (I,0, G[n].size ()) { if(Vis[g[n][i]] = =0) {DFS (g[n][i]); }} left_time[++max_time] =N; return ;}voidDfs_ans (intN) { if(Vis[n])return ; Vis[n]=1; Rep (I,0, G[n].size ()) { if(!Vis[g[n][i]]) {Dfs_ans (g[n][i]); } } return ;}intans () {intRET =0; CLR (VIS); REPF (i,1, Max_time) { if(Vis[left_time[i]] = =0) {ret++; if(Ret >1)return 1; Dfs_ans (Left_time[i]); } } return 0;}intMain () {//freopen ("In.txt", "R", stdin); while(SCANF ("%d%d", &n,&m) = =2){ if(n = =0&& m = =0) Break; Max_time=0; CLR (VIS); CLR (left_time); REPF (i,1, N) g[i].clear (); Rep (I,0, M) { intb; scanf ("%d%d",&a,&b); G[a].push_back (b); } DFS (1); intFlag =0; REPF (i,1, N)if(Vis[i] = =0) flag =1; if(Flag | | Ans ()) printf ("no\n"); Elseprintf"yes\n"); } return 0;}
Maze Castle--strong Unicom component