[PTA-tianyao training] lists connected sets, and pta-tianyao lists connected sets.
Given a number with 1. When searching, we assume that we always start from the vertex with the smallest number and access the adjacent contacts in the ascending order of numbers.
Input Format:
The input row contains two integers, N (0 <N ≤ 10) and E, respectively, the number of vertices and edges of the graph. Then, Row E provides two endpoints of an edge. Numbers in each row are separated by 1 space.
Output Format:
Output A connection set for each row in the format of {v1v2... vk. First output the DFS result and then the BFS result.
Input example:
8 60 70 12 04 12 43 5
Output example:
{ 0 1 4 2 7 }{ 3 5 }{ 6 }{ 0 1 2 7 4 }{ 3 5 }
# Include <bits/stdc ++. h> using namespace std; int G [15] [15], vis [15]; int n, m; void dfs (int u) {int I; printf ("% d", u); for (I = 0; I <n; I ++) if (! Vis [I] & G [u] [I]) // points that have not been accessed and the map is not marked as 0 {vis [I] = 1; // the access is complete, mark as 1 dfs (I); // continue Deep Search} void bfs (int u) {int I; queue <int> q; q. push (u); // Add u to queue q while (! Q. empty () // If the queue is not empty {u = q. front (); // The element q. pop (); // the first element of the team leaves printf ("% d", u); for (I = 0; I <n; I ++) if (! Vis [I] & G [u] [I]) // points that have not been accessed and the map is not marked as 0 {vis [I] = 1; // the access is complete, mark as 1 q. push (I); // Add I to the queue q }}} int main () {int I, u, v; scanf ("% d", & n, & m); // n vertices, m edge for (I = 0; I <m; I ++) {scanf ("% d", & u, & v); // two vertices of each edge, u, v G [u] [v] = G [v] [u] = 1; // u-v and v-u are marked as 1} memset (vis, 0, sizeof (vis); for (I = 0; I <n; I ++) if (! Vis [I]) {printf ("{"); vis [I] = 1; // After access, mark it as 1 dfs (I ); // deep search I printf ("} \ n");} memset (vis, 0, sizeof (vis); for (I = 0; I <n; I ++) if (! Vis [I]) {printf ("{"); vis [I] = 1; // After access, mark it as 1 bfs (I ); // search I printf ("} \ n");} return 0 ;}