标签:
Q1(Problem source : sdut 2141):
#include<cstdio> #include<cstring> #include<queue> using namespace std; const int maxn = 120; int G[maxn][maxn]; int visit[maxn]; void bfs(int m ,int star) { printf("%d",star); visit[star] = 1; queue<int> q; q.push(star); while(!q.empty()) { int now = q.front(); q.pop(); for(int i = 0;i < m;i++) { if(!visit[i] && G[now][i] == 1) { printf(" %d",i); q.push(i); visit[i] = 1; } } } printf("\n"); } int main() { int n; int k , t , m; scanf("%d",&n); while(n--) { memset(G , 0 , sizeof(G)); memset(visit , 0 , sizeof(visit)); int x , y; scanf("%d %d %d",&k , &m , &t); for(int i = 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):
#include<cstdio> #include<cstring> #include<queue> #include<vector> using namespace std; const int maxn = 120; vector<int> G[maxn]; int visit[maxn]; void bfs(int star) { printf("%d",star); visit[star] = 1; queue<int> q; q.push(star); while(!q.empty()) { int now = q.front(); int next; q.pop(); for(int i = 0;i < G[now].size();i++) { next = G[now][i]; if(!visit[next]) { printf(" %d",next); q.push(next); visit[next] = 1; } } } printf("\n"); } int main() { int n; int k , t , m; scanf("%d",&n); while(n--) { int x , y; scanf("%d %d %d",&k , &m , &t); for(int i = 0;i < k;i++) G[i].clear(); memset(visit , 0 , sizeof(visit)); for(int i = 1;i <= m;i++) { scanf("%d%d",&x,&y); G[x].push_back(y); G[y].push_back(x); }; bfs(t); } }
标签:
原文地址:http://www.cnblogs.com/rhythmic/p/5732491.html