[PTA-tianyao training] lists connected sets, and pta-tianyao lists connected sets.

Source: Internet
Author: User

[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 ;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.