1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| #include <stdio.h>
int main() { int a[51][51]; int book[100] = {0}; int i, j, n, m,c,b,tail,head,cur; int que[100]; scanf_s("%d %d", &n, &m); for (i = 1; i <= m; i++) { scanf_s("%d %d", &c, &b); a[c][b] = 1; a[b][c] = 1; } tail = 1; head = 1; que[tail] = 1; tail++; book[1] = 1; while (head < tail) { cur = que[head]; for (i = 1; i <= n; i++) { if (a[cur][i]==1 && book[i] == 0) { book[i] = 1; que[tail] = i; tail++; } if (tail > n) break; } head++; } for (i = 1; i <= n; i++) printf("%d ", que[i]); getchar(); getchar(); return 0; }
|