#include <fstream>
#include <vector>
using namespace std;
ifstream fin("date.in");
ofstream fout("date.out");
struct muchie
{
int i,j;
};
vector<int> V[50];//listele de adiacenta
int n,m;//noduri/muchii
int s; //vf de pornire
int p[50];//vizitat/nevizitat
void citire()
{
int x,y,i;
fin>>n>>m;
for(i=1;i<=m;i++)
{
fin>>x>>y;
V[x].insert(V[x].end(),y); //V[x].push_back(y);
V[y].insert(V[y].end(),x); //V[y].push_back(x);
}
fin>>s;
}
void BF(int s)
{
int st,dr,i,x[50],j;
st=dr=1;
p[s]=1;
x[1]=s;
while(st<=dr)
{
for(i=0; i<V[x[st]].size(); i++)
{
j=V[x[st]][i];
if(!p[j])
{
dr++;
x[dr]=j;
p[j]=1;
}
}
st++;
}
for(i=1;i<=dr;i++) fout<<x[i]<<" ";
}
int main()
{
citire();
BF(s);
fin.close();
fout.close();
return 0;
}
|