提示
#include<bits/stdc++.h> using namespace std; queue<int>q; struct node { int adj; struct node *next; }; typedef struct vnode { int data; struct node *first; }adjlist[110]; typedef struct { adjlist a; int vn,an; }list1; int k,i,j; int creat(list1 &g) { int v1,v2; struct node *p; cin>>g.vn>>g.an>>k; for(i=0;i<g.vn;i++) { g.a[i].first=NULL; } for(i=0;i<g.an;i++) { cin>>v1>>v2; p=new node; p->adj=v2; p->next=g.a[v1].first; g.a[v1].first=p; p=new node; p->adj=v1; p->next=g.a[v2].first; g.a[v2].first=p; } return 1; } int v[110]; void bfs(list1 &g,int k) { struct node *p; memset(v,0,sizeof(v)); v[k]=1; q.push(k); cout<<k; while(!q.empty()) { i=q.front(); q.pop(); p=g.a[i].first; { while(p) { if(!v[p->adj]) { v[p->adj]=1; q.push(p->adj); cout<<" "<<p->adj; } p=p->next; } } } } void Swap(list1 &g) { int t; struct node *p,*q; for(i=0; i<g.vn; i++) for(p = g.a[i].first; p; p=p->next) for(q = p->next; q; q=q->next) if(p->adj > q->adj) { t = p->adj; p->adj = q->adj; q->adj = t; } } int main() { int t; list1 g; cin>>t; while(t--) { creat(g); Swap(g); bfs(g,k); cout<<endl; } return 0; }
