标签:
#include <iostream>
#define MAX 9999
using namespace std;
int main()
{
int i,j,n,m,cur,head,tail,e[101][101],q[100001],book[101]={0};
cin>>n>>m;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
if(i==j)
e[i][j]=0;
else
e[i][j]=MAX;
}
int a,b;
for(i=1;i<=m;i++)
{
cin>>a>>b;
e[a][b]=1;
e[b][a]=1;
}
head=1;
tail=1;
q[tail]=1;
tail++;
book[1]++;
while(head<tail)
{
cur=q[head];
for(i=1;i<=n;i++)
{
if(e[cur][i]==1&&book[i]==0)
{
q[tail]=i;
tail++;
book[i]=1;
}
if(tail>n)
break;
}
head++;
}
for(int i=1;i<tail;i++)
cout<<q[i]<<endl;
return 0;
}
标签:
原文地址:http://www.cnblogs.com/biong-blog/p/4182724.html