-->
当前位置:首页 > 题库

PROGRAMMING:Depth traversal of undirected graph

Luz5年前 (2021-05-10)题库411
The algorithm is designed to realize the depth traversal of undirected graph G, which requires that the vertices in each connected component be output in the form of a table. For example, the output results of the following figure are: (1, 3) (2, 6, 7, 4, 5, 8) (9, 10) [test case: Chen Xiao of IOT engineering level 16]
![ wuxiangtu.png](~/f52a9237-0972-470c-9e2e-8afb910e192a.png)
###Input format:
The first line contains two integers n and m, indicating that the graph has n nodes (numbered 1-N) and M undirected edges( N<=30,M<= 200)
(hint, pay attention to the data size.
Next, each row of M contains three integers Xi and Yi, which means that there is one undirected edge connecting nodes Xi and Yi
###Output format:
The connected component outputs one per line.
One connected component of each row is arranged in the increasing order of the size of the first element of the row.
The points contained in the connected components of each row should also be arranged incrementally according to the size of the elements.
Note that each connected component is represented by a set of connected components, and each of the two elements is separated by a (English state).
###Input example:
Here is a set of inputs. For example:
```in
4 5
1 2
1 3
1 4
2 3
3 4
```
###Output example:
The corresponding output is given here. For example:
```out
(1,2,3,4)
```







answer:If there is no answer, please comment