Original
Title: Discovery Ring
Xiao Ming's lab has N computers, numbered 1~n. Originally, there were N-1 links between the n computers, which constituted a tree-shaped network. On a tree-shaped network, any two
There is a unique path between the computers connected.
However, during the last maintenance of the network, the administrator mistakenly operated to add a data link between the two computers, so there was a loop in the network. The computer on the loop is due to two
There is no longer a single path between the two, causing a bug in the data transfer on these computers.
In order to resume normal transmission. Xiaoming needs to find all the computers on the loop, can you help him?
Input
-----
The first line contains an integer n.
The following n rows are two integers a and b per line, indicating that a data link is connected between a and B.
For 30% of data, 1 <= N <= 1000
For 100% data, 1 <= n <= 100000, 1 <= A, b <= N
The input is guaranteed to be legal.
Output
----
The number of the computer that is printed on the loop in order from small to large, separated by a space.
Sample input:
5
1 2
3 1
2 4
2 5
5 3
Sample output:
1 2 3 5
Resource contract:
Peak memory Consumption < 256M
CPU Consumption < 1000ms
Please strictly according to the requirements of the output, do not use the superfluous printing similar: "Please enter ..." Redundant content.
All the code is placed in the same source file, after debugging passed, the copy is submitted to the source.
Note: The main function needs to return 0
Note: Use only ANSI c/ansi C + + standards, and do not invoke special functions that depend on the compilation environment or operating system.
Note: All dependent functions must explicitly #include <xxx> in the source file, and the common header files cannot be omitted from the project settings.
When committing, be careful to choose the type of compiler you expect.
My problem-solving ideas:
The main use of DFS; The purpose is to find a loop in a tree; Assuming that a node in the loop is selected as the starting point of the loop, a node can also be used as a loop
The end of the road, because I do not know which node in the loop, to enumerate the nodes in turn 1~n;
Assume that the data entered by the user is:
7
1 2 (0)
1 3 (0)
2 4 (0)
2 5 (0)
5 6 (0)
5 7 (0)
6 7 (0)
The significance of the third column (0/1/-1) Digital:
0: This edge is not traversed
-1: This side is judged not on the ring
1: This side is on the ring
In the 5-6-7 constituent ring, now iterates through each edge from node 1 (with node 1 as start and end) (the traversal order of the edges is related to the order of the user-entered edges);
The traversal ends only when you return to the starting point during the traversal.
Search the user input n rows of data, find a line with a digital 1 (in the case of the 1th row), the first row of the third column of the digital 1 (for the traversal), and then on this line of another
a node (2 in the example) is a starting point, search n rows of data in the digital bit 0 of the row data (1 for traversal), stay in the third row, the third column of digital position 1, and then
continue the search at 4, at which point 4 is the leaf node and the search data is not Return the 2 4 (1)-2 4 (-1) to indicate that the edge is not in the ring;
continue to search for n rows of data at 2, stay at 2 5 (1), and continue searching at 5 5 (6). continue search stay at 6 7 (1) When Stop
Stay at 5 7 (1), although the ring 5-6-7, but not satisfied with 1 as the end point, so the search failed (and then the beginning of the search for the result of 7);
5 7 (-1); 6 7 (-1); 5 6 (-1); 2 5 (-1); Continue to search with 1 as the starting point ... So when you search for the first starting point with 5, the search succeeds.
#include <stdio.h>#include<stdlib.h>intarr[100000][3]={0};//Assigning 3 Columnsintn=0;intflag=0;//flag==1 means to find the starting point and returnintunaccess=0;voidDfsintNumintvalue) { intI=0; intj=0; for(i=0; i<=n-1; i++){ if(flag==1){ Break; } for(j=0; j<=1; j + +){ //*************** if(i==n-1&& j==1&& arr[i][j]!=num) {//leaf knot Pointunaccess=1; Break; } if(i==n-1&& j==1&& arr[i][j]==num) { if(arr[i][2]==1){//The leaf node is in line N, and this edge has been traversed.unaccess=1; Break; } } //*************** if(Arr[i][j]==num && arr[i][2]==0) {arr[i][2]=1; //*************** if(j==0){ if(arr[i][1]==value) {//Judging is not the starting pointflag=1; Break; } } Else{ if(arr[i][0]==value) {Flag=1; Break; } } //*************** if(j==0) {DFS (arr[i][1],value); if(flag==1){ Break; } if(unaccess==1) {arr[i][2]=-1; } unaccess=0; } Else{DFS (arr[i][0],value); if(flag==1){ Break; } if(unaccess==1) {arr[i][2]=-1; } unaccess=0; } } } }}intMain () {scanf ("%d",&N); intI=0; intj=0; for(i=0; i<=n-1; i++){ for(j=0; j<=1; j + +) {scanf ("%d",&Arr[i][j]); } } for(i=0; i<=n-1; i++) {//the last column, 0, means this side can go.arr[i][2]=0; } intnum=0; for(num=1; num<=n;num++) {//num stands for start and endDFS (Num,num); if(flag==1){ Break; } Else{ for(intz=0; z<=n-1; z++) {//continue DFS with new beginningsarr[z][2]=0; } } } int*rt;//This array is used to hold the nodes in the loop.Rt= (int*)malloc(sizeof(int) * (n+1)); for(i=1; i<=n;i++) {Rt[i]=0; } for(i=0; i<=n-1; i++){ if(arr[i][2]==1) {rt[arr[i][0]]=1; rt[arr[i][1]]=1; } } for(i=1; i<=n;i++){ if(rt[i]==1) {printf ("%d", i); } } Free(RT); return 0;}
There are errors or improvements that are welcome to be made.
12:49:48
2018-05-10
Discovery Ring--eighth annual Blue Bridge Cup C Language B group (National race) fourth question