PTA Title Set data structures and algorithms (Chinese) 7-6

PTA Title Set data structures and algorithms (Chinese) 7-6

7-6 are listed the communication set (25 points)
 

Given a N vertices and no edges E of the chart, with the DFS and BFS which communicates a list of all sets, respectively. Assume that the vertices from 0 to N - . 1 ID. When you search, suppose we always start from the smallest number of vertices, adjacent access point ascending numerical order.

Input formats:

The first input line is given two integers N ( 0) and E, respectively, the number of vertices and edges in FIG. Then E lines of a given side of the two endpoints. 1 are separated by spaces between each line number.

Output formats:

According to "{  V . 1 V 2 ...  V K }" format, each line of output set of a communication. First output DFS, and then output the results of the BFS.

Sample input:

8 6
0 7
0 1
2 0
4 1
2 4
3 5

Sample output:

{ 0 1 4 2 7 }
{ 3 5 }
{ 6 }
{ 0 1 2 7 4 }
{ 3 5 }
{ 6 }

Topic analysis: the basic functions of the realization of an implementation study depth-first traversal and breadth-first traversal of attention because it is not connected to any point while traversing all graph nodes to see if there has not been included
depth-first traversal using the breadth-first traversal use recursion access queue to visit
  1 #define _CRT_SECURE_NO_WARNINGS   
  2 #include<stdio.h>
  3 #include<stdlib.h>
  4 #include<malloc.h>
  5 #define MaxVertexNum 100
  6 
  7 typedef struct ENode* Edge;
  8 struct ENode
  9 {
 10     int V1, V2;
 11 };
 12 typedef struct GNode* Graph;
 13 struct GNode
 14 {
 15     int Nv;
 16     int Ne;
 17     int G[MaxVertexNum][MaxVertexNum];
 18 };
 19 
 20 Graph BuildGraph(int VertexNum)
 21 {
 22     Graph Gra;
 23     Gra = (Graph)malloc(sizeof(struct GNode));
 24     Gra->Ne = 0;
 25     Gra->Nv = VertexNum;
 26     for (int i = 0; i < VertexNum; i++)
 27         for (int j = 0; j < VertexNum; j++)
 28             Gra->G[i][j] = 0;
 29     return Gra;
 30 }
 31 void InsertEdge(Edge E, Graph Gra)
 32 {
 33     Gra->G[E->V1][E->V2] = 1;
 34     Gra->G[E->V2][E->V1] = 1;
 35 }
 36 Graph CreatGraph()
 37 {
 38     Edge E;
 39     int N, M;
 40     scanf("%d%d", &N, &M);
 41     Graph G = BuildGraph(N);
 42     G->Ne = M;
 43     for (int i = 0; i < M; i++)
 44     {
 45         E = (Edge)malloc(sizeof(struct ENode));
 46         int V1, V2;
 47         scanf("%d%d", &V1, &V2);
 48         E->V1 = V1;
 49         E->V2 = V2;
 50         InsertEdge(E, G);
 51     }
 52     return G;
 53 }
 54 int IsEdge(Graph G, int V1, int V2)
 55 {
 56     return G->G[V1][V2] == 1;
 57 }
 58 int Collected[MaxVertexNum];
 59 void Initialize()
 60 {
 61     for (int i = 0; i < MaxVertexNum; i++)
 62         Collected[i] = 0;
 63 }
 64 void DFS(Graph G,int i)
 65 {
 66     printf("%d ", i);
 67     Collected[i] = 1;
 68     for (int j = 0; j < G->Nv; j++)
 69     {
 70         if (!Collected[j]&&IsEdge(G,i,j))
 71             DFS(G, j);
 72     }
 73 }
 74 
 75 int Queue[10];
 76 int Front = 1;
 77 int Rear = 0;
 78 int Size = 0;
 79 int Succ(int num)
 80 {
 81     if (num == 10)
 82         return 0;
 83     else
 84         return num;
 85 }
 86 void EnQueue(int num)
 87 {
 88     Rear = Succ(Rear + 1);
 89     Queue[Rear] = num;
 90     Size++;
 91 }
 92 int DeQueue()
 93 {
 94     int Value = Queue[Front];
 95     Front = Succ(Front + 1);
 96     Size--;
 97     return Value;
 98 }
 99 int IsEmpty()
100 {
101     return Size == 0;
102 }
103 void BFS(Graph G,int i)
104 {
105     EnQueue(i);
106     Collected[i] = 1;
107     while (!IsEmpty())
108     {
109         int Tmp = DeQueue();
110         printf("%d ", Tmp);
111         for (int j = 0; j < G->Nv; j++)
112         {
113             if (!Collected[j] && IsEdge(G, Tmp, j))
114             {
115                 EnQueue(j);
116                 Collected[j] = 1;
117             }
118         }
119     }
120 }
121 int main()
122 {
123     Graph G=CreatGraph();
124     for (int i = 0; i < G->Nv; i++)
125     {
126         if (!Collected[i])
127         {
128             printf("{ ");
129             DFS(G, i);    
130             printf("}");
131             printf("\n");
132         }
133     }
134     Initialize();
135     for (int i = 0; i < G->Nv; i++)
136     {
137         if (!Collected[i])
138         {
139             printf("{ ");
140             BFS(G, i);
141             printf("}");
142             printf("\n");
143         }
144     }
145     return 0;
146 }
View Code

 

Guess you like

Origin www.cnblogs.com/57one/p/11584957.html