http://10.1.5.253/acmhome/problemdetail.do?&method=showdetail&id=1502
//1502 #include <stdio.h> #include <string.h> #include <queue> using namespace std; #define N 10 int g[N][N],bz[N],n,m,t; queue <int> q; void BFS(int cur) { int j; bz[cur]=1; q.push(cur); while (!q.empty()) { cur=q.front(); printf("%d", cur);t++; if(t==n)printf(" "); else printf(" "); q.pop(); for (j=1;j<=N;j++) if (bz[j]==0 && g[cur][j]==1) { q.push(j); bz[j]=1; } } } void input() { int i,j,f; scanf("%d%d",&n,&m); for (int i=1; i<=m; i++) { scanf("%d%d",&f,&t); g[f][t]=g[t][f]=1; } } int main() { memset(g,0,sizeof(g)); memset(bz,0,sizeof(bz)); input(); BFS(0); } ************************************************************************************************************** //1502 #include <iostream> #include <cstring> #include <queue> #define N 100 using namespace std; int map[N][N],bz[N]; int n,m,flag; queue <int> my; void bfs(int s) { int t,i; bz[s]=1; my.push(s); while (!my.empty()) { t=my.front(); my.pop(); if (flag==0) { cout<<t; flag=1;} else cout<<" "<<t; for (i=0; i<n; i++) { if (map[t][i]==1 && bz[i]==0) { bz[i]=1; my.push(i); } } } } int main() { int x,y; cin>>n>>m; memset(map,0,sizeof(map)); memset(bz,0,sizeof(bz)); while (m--) { cin>>x>>y; map[x][y]=map[y][x]=1; } flag=0; bfs(0); cout<<endl; return 0; } ******************************************************************************************************* //1502 #include <iostream> #include <queue> using namespace std; queue <int> T; int a,b,tu[100][100],bz[100],v=0; void BFS(int k) { int t,i; T.push(k); bz[k]=1; while(!T.empty()) { t=T.front(); v++; T.pop(); if(v!=a)cout<<t<<" "; else cout<<t<<endl; for(i=0;i<a;i++) if(tu[t][i]==1&&bz[i]==0) { bz[i]=1; T.push(i); } } } int main() { int i=0,b,x,y,ans; cin>>a>>b; while (i<b) { cin>>x>>y; tu[x][y]=tu[y][x]=1; i++; } BFS(0); return 0; }