Q1 (Problem Source:sdut 2141):
The topic describes the given undirected connectivity graph, vertex numbering from 0 to n-1, with breadth-first search (BFS) traversal, and the output of a traversal sequence from a vertex. (The same node of the same-level adjacency point, nodes with a small number of priority traversal) input input first behavior integer n (0< n <100), which represents the number of groups of data. For each set of data, the first line is three integers k,m,t (0<k<100,0<m< (k-1) *k/2,0< t<k), which indicates that there are m edges, k vertices, and T is the starting vertex of the traversal. The following M-line, each line is a space-delimited two integer u,v, representing a non-forward edge that joins the U,v vertex. The output output has n rows, corresponding to the n set of outputs, each of which is separated by a space of k integers, corresponding to a set of data, indicating the results of BFS traversal. Analysis: A very basic problem with wide-search output traversal sequences based on adjacency tables.
#include <cstdio>#include<cstring>#include<queue>using namespacestd;Const intMAXN = -;intG[MAXN][MAXN];intVISIT[MAXN];voidBFsintMintstar) {printf ("%d", star); Visit[star]=1; Queue<int>Q; Q.push (Star); while(!Q.empty ()) { intnow =Q.front (); Q.pop (); for(inti =0; I < m;i++) { if(!visit[i] && g[now][i] = =1) {printf ("%d", i); Q.push (i); Visit[i]=1; }}} printf ("\ n");}intMain () {intN; intK, T, M; scanf ("%d",&N); while(n--) {memset (G,0,sizeof(G)); memset (Visit,0,sizeof(visit)); intx, y; scanf (" %d%d%d", &k, &m, &t); for(inti =1; I <= m;i++) {scanf ("%d%d",&x,&y); G[x][y]=1; G[Y][X]=1; } BFS (M, t); }}
Q2 (Problem Source:sdut 2142):
The topic describes the given undirected connectivity graph, vertex numbering from 0 to n-1, with breadth-first search (BFS) traversal, and the output of a traversal sequence from a vertex. (The same node of the same-level adjacency point, nodes with a small number of priority traversal) input input first behavior integer n (0< n <100), which represents the number of groups of data. For each set of data, the first line is three integers k,m,t (0<k<100,0<m< (k-1) *k/2,0< t<k), which indicates that there are m edges, k vertices, and T is the starting vertex of the traversal. The following M-line, each line is a space-delimited two integer u,v, representing a non-forward edge that joins the U,v vertex. The output output has n rows, corresponding to the n set of outputs, each of which is separated by a space of k integers, corresponding to a set of data, indicating the results of BFS traversal. Analysis: The adjacency table saves time and space when the relative adjacency matrix is stored, but the price is slightly higher than the previous difficulty when coding. Here to fully understand the meaning of the critical table <vector>g[i], g[now][i] is the node that is connected to now and I is not.
The simple reference code is as follows:
#include <cstdio>#include<cstring>#include<queue>#include<vector>using namespacestd;Const intMAXN = -; Vector<int>G[MAXN];intVISIT[MAXN];voidBFsintstar) {printf ("%d", star); Visit[star]=1; Queue<int>Q; Q.push (Star); while(!Q.empty ()) { intnow =Q.front (); intNext; Q.pop (); for(inti =0; i < g[now].size (); i++) {Next=G[now][i]; if(!Visit[next]) {printf ("%d", next); Q.push (next); Visit[next]=1; }}} printf ("\ n");}intMain () {intN; intK, T, M; scanf ("%d",&N); while(n--) { intx, y; scanf (" %d%d%d", &k, &m, &t); for(inti =0; I < k;i++) g[i].clear (); memset (Visit,0,sizeof(visit)); for(inti =1; I <= m;i++) {scanf ("%d%d",&x,&y); G[x].push_back (y); G[y].push_back (x); }; BFS (t); }}
2016qut Summer Camp--a review of the plan