For a given undirected graph with N vertices and E edges, please list all the connected components by both DFS and BFS. Assume that all the vertices are numbered from 0 to N-1. While searching, assume that we always start from the vertex with the smallest index, and visit its adjacent vertices in ascending order of their indices.
Input Specification:
Each input file contains one test case. For each case, the first line gives two integers N (0<N<=10) and E, which are the number of vertices and the number of edges, respectively. Then E lines follow, each described an edge by giving the two ends. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print in each line a connected component in the format “{ v1 v2 … vk }”. First print the result obtained by DFS, then by BFS.
Sample Input:1
2
3
4
5
6
78 6
0 7
0 1
2 0
4 1
2 4
3 5
Sample Output:1
2
3
4
5
6{ 0 1 4 2 7 }
{ 3 5 }
{ 6 }
{ 0 1 2 7 4 }
{ 3 5 }
{ 6 }
代码如下:
1 | #include<iostream> |
本题比较简单,测试结果如下:
| 测试点 | 结果 | 用时(ms) | 内存(kB) | 得分/满分 |
|---|---|---|---|---|
| 0 | 答案正确 | 1 | 308 | 15/15 |
| 1 | 答案正确 | 1 | 184 | 8/8 |
| 2 | 答案正确 | 1 | 308 | 2/2 |