#include <stdio.h> #include <stdlib.h> #include <string.h>struct node{int date; struct node *next;} *h[1000],*head[1000];int v[10000],vi[10000];int i,j;int t,n,m,k;void chu (int z) {for (i=0;i<z;i++) {h[i]= (struct n ode*) malloc (sizeof (struct node)); h[i]->next=null; Head[i]=h[i]; }}void chu1 (int z) {for (i=0;i<z;i++) {head[i]=head[i]->next; }}void BFS (int K) {int jin=0,chu=0; CHU1 (n); int W; V[jin++]=k; vi[k]=2; printf ("%d", K); while (Chu<jin) {w=v[chu++]; if (chu!=1&&vi[w]==1) {printf ("%d", W); vi[w]=2;} while (Head[w]!=null) {if (vi[head[w]->date]==0) {V[jin++]=head[w]->date;vi[head [w]->date]=1; } head[w]=head[w]->next; }} printf ("\ n");} int main () {node *p,*q; scanf ("%d", &t); for (i=0;i<t;i++) {scanf ("%d%d%d", &n,&m,&k); Chu (n); Memset(Vi,0,sizeof (vi)); for (j=0;j<m;j++) {int A, B; scanf ("%d%d", &a,&b); p= (struct node*) malloc (sizeof (struct node)); p->next=null; p->date=b; h[a]->next=p; H[a]=p; p= (struct node*) malloc (sizeof (struct node)); p->next=null; p->date=a; h[b]->next=p; H[b]=p; } BFS (k); }}
BFS adjacency Table