Find Cycle

Find Cycle

A graph is a type of data structure that consists of nodes and edges that connect the nodes. An edge has a start node and end node, and we will only consider directed edges.
The figure below is an example of a graph with 8 nodes (labeled '1' through '8') and 9 edges. A path in a graph is a sequence of nodes that are connected by edges.
'6->3->2->1' is an example of path in the graph below.



A "Cycle" is a special case of path; it has at least 2 nodes, and the start node and the end node of the path are the same.
For example, '1->5->2->1','7->8->7','5->2->1->5', and '8->7->8' are examples of cycle you can find in the graph above.



Given a graph, write a program that finds a cycle of the given graph.
If there's a cycle, write node numbers in ascending order, and write '0'. If there is more than one cycle, choose and print out one cycle.         

[Constraints]
N, the number of nodes satisfies 5<=N<=100, and M, the number of edges satisfies 1<=M<=1000.



[Input]
Several test cases can be included in the inputs. T, the number of cases is given in the first row of the inputs. 
After that, the test cases as many as T (T <= 10) are given. The first line of each test case has N, the number of nodes, and M, the number of edges. The second line has M pairs of start node and end node. The nodes are labeled form '1' to 'N'.
No edge has the same start and end node. All integers in the input are separated by a space.

We call the number of edges that come out of a node "degree".
For test cases #1-#3, the degree of all nodes is 1; for test cases #4-#6, the degree is 2 or less; for test cases #7-#10, there's no limit in degree.
Here, "degree of the node is 1" means that all nodes simply have only one edge coming out of the node.
(In the left graph above, from node 2, you can go to either node 1 or node 4 so the degree is not 1. In the right graph, on the contrary, the degree is 1.)

[Output]
Print answers for each of 10 test cases in 10 lines. Start each line with '#x' where x is the test case number, leave a space and print your answer.
When a cycle exists, enumerate numbers of the nodes in the cycle in ascending order. For example, the cycle '1->5->2->1' is written as '1 2 5'.
If there is no cycle, just write '0'.

[Sample Input/Output]
Input (Input consists of several lines, but the example below shows input for only 4 test cases represented through 9 lines to help you understand the format.)
4 -->T: the number of test cases
5 5 -->case 1
4 3 2 4 3 5 3 2 1 4
5 5 -->case 2
4 3 2 4 3 5 2 3 1 4
6 5 -->case 3
1 5 6 4 3 1 5 3 4 6
8 9 -->case 4
5 2 3 2 6 3 8 7 2 1 6 4 2 4 1 5 7 8

Output (Output is made up of 10 lines, but the example below shows output for only 4 test cases to help you understand the format.)
Case #1 
2 3 4
Case #2 
0
Case #3 
1 3 5
Case #4 
7 8 

Note, for "Case 4" we can also output "1 2 5".

题目的意思比较简单,大意是从给出的线路中找出一个回环,然后将回环结果由小到大输出即可。

解题思路:首先构造一个N*N的矩阵,并初始化为全0,通过输入将有线路的矩阵标记为1,然后用深度递归的方法找到回环;在寻找的过程中需要维护两张表,Table表按顺序保存已经遍历过的非0节点,Hash表保存Table表中非0节点出现的顺序,也就是遍历的顺序。

 

Table[i]

1

2

3

4

5

6

7

8

Value

1

3

4

2

6

0

0

0

 

Hash[i]

1

2

3

4

5

6

7

8

Value

1

4

2

3

6

0

0

0

 

/*

You should use the statndard input/output

 

in order to receive a score properly.

 

Do not use file input and output

 

Please be very careful.

*/

 

#include <stdio.h>

 

#define MAX_M  1001

#define MAX_N  101

 

int Array[MAX_N][MAX_N];// 构造一个N*N的矩阵,用来保存输入的边数

int N,M;

int Answer;

int num = 0 ;

 

int Table[MAX_N];

int Hash[MAX_N];

int top ;//遍历过的非0点个数

int start ;//标志位

int temp;

 

//递归算法

int Cacluate(int node)

{

         int i;

         //int ret; 

         if (start !=-1)

                   return 0;

                  

         if (Hash[node] != 0)

         {

                   //Table : 100 200 800 0 2 5 7 9 6 4 5

                   //Hash  : 1 0 1 0 7

                   //loop;

                   start = Hash[node];

                   //top--;

                   return 0;

         }

         //retr

         top++;

         Table[top] = node;  

         Hash[node] = top;   

        

        

         //if(Hash[node] == 1)

         //      return node;

        

         //Hash[node] = 1;

        

         for(i=1;i<=N;i++)

         {

                   if(Array[node][i] != 0  && node !=i)

                   {

                            Cacluate(i);

                            /*ret = Cacluate(i);

                            if(ret != 0)

                            {

                                     return ret;

                            }*/

                            if (start !=-1)

                                     return 0;

                   }

         }

        

         Hash[node] = 0;

         top--;

         return 0;

}

 

int main(void)

{

         int T, test_case;

         /*

            The freopen function below opens input.txt file in read only mode, and afterward,

            the program will read from input.txt file instead of standard(keyboard) input.

            To test your program, you may save input data in input.txt file,

            and use freopen function to read from the file when using scanf function.

            You may remove the comment symbols(//) in the below statement and use it.

            But before submission, you must remove the freopen function or rewrite comment symbols(//).

          */

         freopen("input.txt", "r", stdin);

 

         /*

            If you remove the statement below, your program's output may not be rocorded

            when your program is terminated after the time limit.

            For safety, please use setbuf(stdout, NULL); statement.

          */

         setbuf(stdout, NULL);

 

         scanf("%d", &T);

         for(test_case = 0; test_case < T; test_case++)

         {

 

                   int i,j;

                   int t1,t2;

                   int loop = 0 ;

                   top = 0;

        start = -1;

                  

                   scanf("%d",&N);

                   scanf("%d",&M);

 

                   //初始化Array数组,全部初始化为0

                   for(i=1;i<=N;i++)

                   {

                            for(j=1;j<=N;j++)

                            {

                                     Array[i][j] = 0 ;

                            }

                   }

                   //保存输入的边数,将有线路的边置为1

                   for(i=0;i<M;i++)

                   {                          

                            scanf("%d %d" , &t1,&t2);

//保存t1->t2的边之前先判断是否有t2->t1的边,如果有,直接按照大小顺序输出t1t2

                            if (Array[t2][t1]!=0)

                            {

                                     if(t1<=t2)

                                         printf("Case #%d\n%d %d\n",test_case+1,t1,t2);

                                     else

                                               printf("Case #%d\n%d %d\n",test_case+1,t2,t1);

                                     loop = 1;

                            }

                            Array[t1][t2] = 1 ;     //将有线路的边置为1  

                                    

                   }

                   if (loop!=0)

        {

            continue;//结束本次case循环,之后的代码不执行,进入下一个case

        }

                  

                   for(i=0;i<=N;i++)//初始化两张表

                   {

                            Table[i] = 0;

            Hash[i] = 0;

                   }

                   //start = -1;              

                   for(i=1;i<=N;i++)

                   {

                            for(j=1;j<=N;j++)

                            {

                                     if (Array[i][j] != 0)//如果不为0则循环遍历数据表

                                     {

                                               Cacluate(i);                                           

                                               if (start !=-1)

                                                        break;//跳出第一重for循环

                                     }

                                    

                            }

                            if (start !=-1)

                                     break;// 跳出第二重for循环,即结束遍历

                   }

 

          printf("Case #%d\n", test_case+1);

 

//将得到的结果进行排序

     if(start !=-1){

                   for(i=start;i<=top;i++)

                   {

                         for( j = start;j < top +start- i;j++)

                                     {

                                               if(Table[j] > Table[j+1])

                                               {

                                                        temp = Table[j] ;

                                                        Table[j] = Table[j+1] ;

                                                        Table[j+1] = temp ;

                                               }

                                     }

                   }

                  

//输出结果

                   for(i=start;i<top;i++)

                   {

                            printf("%d ",Table[i]);

                   }

                   printf("%d",Table[top]);

          }

 

          else  if(start ==-1)

        printf("%d",Table[0]);

 

         printf("\n");

                           

                  

                  

                   /

                   /*

                      Implement your algorithm here.

                      The answer to the case will be stored in variable Answer.

                    */

                   /

                   //Answer = 0;

 

                   // Print the answer to standard output(screen).

                  

                   //

                   //printf("%d\n", Answer);

         }

         return 0;//Your program should return 0 on normal termination.

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值