题目:http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2142&cid=1186
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
#include<queue>
using namespace std;
int vis[];
int n,m,k;
queue<int>q;
struct node
{
int u,v;
struct node *next;
}*head[];
void add(int u, int v)
{
struct node *p = (struct node*)malloc(sizeof(struct node));
p->u = u;
p->v = v;
p->next = head[u];
head[u] = p;
};
int cmp(const void *a,const void *b)
{
return *(int *)a-*(int *)b;
}
void bfs(int t)
{
int i,x,a[],j,b[],y;
q.push(t);
vis[t]=; j=;
while(!q.empty())
{
x=q.front();
a[++j]=x;
q.pop();
y=;
for(struct node *p=head[x]; p!=NULL; p=p->next)
{
if(vis[p->v]==)
{
b[y++]=p->v;
vis[p->v]=;
}
}
if(y>=)
qsort(b,y,sizeof(b[]),cmp);
for(i=; i<=y-; i++)
q.push(b[i]);
}
for(i=; i<=j-; i++)
printf("%d ",a[i]);
printf("%d\n",a[i]);
};
int main()
{
int t,i,u,v;
scanf("%d",&n);
while(n--)
{
memset(head,NULL,sizeof(head));
memset(vis,,sizeof(vis));
cin>>k>>m>>t;
for(i=; i<m; i++)
{
cin>>u>>v;
add(u,v);
add(v,u);
}
bfs(t);
}
}