#include<iostream> #include<queue> using namespace std; int father[100]; int d[100]; int color[100]; const int MAX=1000; struct node{ int m; node* next; node(int n,node* p){ m=n; next=p; } }; node* p; struct head{ int n; node* adj; }Head[100]; void Create(int a,int b) { p=Head[a].adj; if(Head[a].adj==NULL) Head[a].adj=new node(b,NULL); else { while(p->next!=NULL) { p=p->next; } p->next=new node(b,NULL); } p=Head[b].adj; if(Head[b].adj==NULL) Head[b].adj=new node(a,NULL); else { while(p->next!=NULL) { p=p->next; } p->next=new node(a,NULL); } } void BFS(int n,int s){ for(int i=1;i<=n;i++) { color[i]=0; d[i]=MAX; father[i]=-1; } queue<head> qu; color[s]=1; d[s]=0; father[s]=-1; qu.push(Head[s]); while(!qu.empty()) { int u=qu.front().n; qu.pop(); node* p1=Head[u].adj; while(p1!=NULL) { int k=p1->m; if(color[k]==0)//表示点未被访问过 { qu.push(Head[k]); d[k]=d[u]+1; color[k]=1; father[k]=u; } p1=p1->next; } color[u]=2; } } void Display(int s,int v) { if(s==v) cout<<s<<endl; else if(father[v]==-1) cout<<"No path from "<<s<<" to "<<v<<endl; else { Display(s,father[v]); cout<<v<<endl; } } int main() { int n,l,a,b; while(cin>>n>>l){//n,l表示图中顶点和边的个数 for(int i=1;i<=n;i++) { Head[i].n=i; Head[i].adj=NULL; }//邻接表初始化 for(int i=1;i<=l;i++) { cin>>a>>b; Create(a,b); } int s;//广搜开始点 cin>>s; BFS(n,s); Display(s,n); for(int i=1;i<=n;i++) cout<<color[i]<<" "<<father[i]<<endl; } return 0; }