http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2141&cid=1186
1 #include<cstdio> 2 #include<cstring> 3 #include<cstdlib> 4 #include<iostream> 5 #include<queue> 6 using namespace std ; 7 int map[110][110] ; 8 int vis[110] ; 9 int ans[110] ; 10 int i,j ,u,v; 11 int dingdian,n,bian ,h; 12 int mark=1; 13 queue<int>q ; 14 void bfs(int h) 15 { 16 memset(ans,0,sizeof(ans)) ; 17 memset(vis,0,sizeof(vis)) ; 18 q.push(h) ; 19 vis[h] = 1 ; 20 while(!q.empty()) 21 { 22 int t = q.front() ; 23 q.pop() ; 24 if(mark) 25 { 26 printf("%d",t); 27 mark=0; 28 } 29 else printf(" %d",t); 30 for(i = 0 ; i < dingdian ; i++) 31 { 32 if(!vis[i]&&map[t][i]) 33 { 34 q.push(i) ; 35 vis[i] = 1 ; 36 } 37 } 38 } 39 } 40 int main() 41 { 42 cin>>n ; 43 queue<int>q ; 44 memset(map,0,sizeof(map)) ; 45 for(i = 1 ; i <= n ; i++) 46 { 47 scanf("%d%d%d",&dingdian,&bian,&h) ; 48 int j ; 49 for(j = 0 ; j <bian ; j++) 50 { 51 scanf("%d%d",&u,&v) ; 52 53 map[u][v] = map[v][u] = 1 ; 54 } 55 bfs(h) ; 56 cout<<endl ; 57 } 58 return 0 ; 59 }