Maze Castle
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 12778 Accepted Submission (s): 5698
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
Maze Castle time Limit: -/ +MS (java/others) Memory Limit:65536/32768K (java/Others) Total submission (s):12778Accepted Submission (s):5698problem description to train her sense of direction, Gardon built a large castle with 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 that the passage from room A to room B, but does not mean that through it can be from room B to room A. 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, the outputs"Yes", otherwise the output"No". Sample Input3 31 22 33 13 31 22 33 20 0Sample outputyesno Authorgardon sourcehdu2006-4Programming Contest RECOMMENDLXJ
View Code
Through two Dfs, one time to search the original image, one time to search its transpose graph, the last to get the value of K is the number of strong connected branches of the graph.
This algorithm is very magical. To prove that I am not. But it is clear from the example that this method is correct.
HDU 1269 Labyrinth Castle Strong connected components