1 #include<stdio.h>
2 #include<string.h>
3 #include<stdlib.h>
4 #include<iostream>
5 #include<queue>
6
7 using namespace std;
8 int vis[101];
9 int n,m,k;
10 queue<int>q;
11
12 struct node
13 {
14 int u,v;
15 struct node *next;
16 }*head[110];
17
18 void add(int u, int v)
19 {
20 struct node *p = (struct node*)malloc(sizeof(struct node));
21 p->u = u;
22 p->v = v;
23 p->next = head[u];
24 head[u] = p;
25 };
26
27 int cmp(const void *a,const void *b)
28 {
29 return *(int *)a-*(int *)b;
30 }
31
32 void bfs(int t)
33 {
34 int i,x,a[110],j,b[110],y;
35 q.push(t);//元素t入队列
36 vis[t]=1; j=0;
37 while(!q.empty())//如果队列非空
38 {
39 x=q.front();
40 a[++j]=x;
41 q.pop();//删除队首元素
42 y=0;
43 for(struct node *p=head[x]; p!=NULL; p=p->next)
44 {
45 if(vis[p->v]==0)
46 {
47 b[y++]=p->v;
48 vis[p->v]=1;
49 }
50 }
51 if(y>=1)
52 qsort(b,y,sizeof(b[0]),cmp);//快速排序,使得同一层次的节点按照从小到大排序
53 for(i=0; i<=y-1; i++)
54 q.push(b[i]);//让b数组中的元素依次由小到达入队列
55 }
56 for(i=1; i<=j-1; i++)
57 printf("%d ",a[i]);
58 printf("%d\n",a[i]);
59 };
60
61 int main()
62 {
63 int t,i,u,v;
64 scanf("%d",&n);
65 while(n--)
66 {
67 memset(head,NULL,sizeof(head));
68 memset(vis,0,sizeof(vis));
69 cin>>k>>m>>t;
70 for(i=0; i<m; i++)
71 {
72 cin>>u>>v;
73 add(u,v);
74 add(v,u);
75 }
76 bfs(t);
77 }
78 }