05-Figure 1. List Components (25)

Source: Internet
Author: User

05-Figure 1. List components (25) time limit MS
Memory Limit 65536 KB
Code length limit 8000 B
Procedures for the award of questions StandardAuthor Chen, Yue

For a given undirected graph with N vertices and E edges, please list all of the connected components by both DFS and BFS. Assume that all the vertices is numbered from 0 to N-1. While searching, assume. We always start from the vertex with the smallest index, and visit it adjacent vertices in a Scending order of their indices.

Input Specification:

Each input file contains the one test case. For each case, the first line gives integers N (0<n<=10) and E, which is the number of vertices and the number of edges, respectively. Then E-lines follow, each described a edge by giving the both ends. All the numbers in a line is 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:
8 60 70 12 04 12 43 5
Sample Output:
{0 1 4 2 7} {3 5} {6} {0 1 2 7 4} {3 5} {6}
#include <stdio.h>void DFS (int graph[][10], int *visited, int v, int n, int *ret) {Visited[v] = 1;ret[++ret[0]] = v; for (int i = 0; i < n; ++i) {if (v! = i && graph[v][i] &&!visited[i]) {DFS (graph, visited, I, N, ret); }}}void BFS (int graph[][10], int *visited, int v, int n) {int queque[20] = {};int head = 0, rear = 0;queque[rear++] = v;// Queue Visited[v] = 1;printf ("{"), while (Rear > head) {int curr = queque[head++];//out of the queue printf ("%d", Curr); for (int i = 0; I < n; ++i)//queue each visited adjacency node if (Graph[curr][i] &&!visited[i]) {visited[i] = 1;queque[rear++] = i;}} printf ("}\n");}  int main () {//freopen ("test.txt", "R", stdin); int n, edge;scanf ("%d%d", &n, &edge); int graph[10][10] = {};for (int i = 0; I < edge; ++i) {//adjacency matrix way construct diagram int v1, v2;scanf ("%d%d", &v1, &v2); Graph[v1][v2] = 1;graph[v2][v1] = 1;}  int visited[10] = {};for (int i = 0; i < n; ++i) {int ret[11] = {};//save recursive Traversal to node if (!visited[i]) {DFS (graph, visited, I, N, ret);p rintf ("{"); for (int j = 1; J <= Ret[0]; ++J) {printf ("%d", Ret[j]);} printf ("}\n");}} for (int i = 0; i < n; ++i)//reset visited tag visited[i] = 0;for (int i = 0; i < n; ++i) {//bfsif (!visited[i]) {BFS (graph, VI Sited, I, n);}} return 0;}


Title Link: http://www.patest.cn/contests/mooc-ds/05-%E5%9B%BE1

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

05-Figure 1. List Components (25)

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.