-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph2.c
More file actions
61 lines (47 loc) · 1.11 KB
/
Copy pathgraph2.c
File metadata and controls
61 lines (47 loc) · 1.11 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <stdio.h>
int main()
{
int i,j,n,m,a,b,cur,book[101]={0},e[101][101];
int que[10001], head, tail;
scanf("%d %d", &n, &m);
//邻接矩阵初始化
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
if(i == j) e[i][j] = 0;
else e[i][j] = 99999999;
for(i=1; i<=m; i++)
{
scanf("%d %d", &a, &b);
e[a][b] = 1;
e[b][a] = 1;
}
head=1;
tail=1;
que[tail] = 1;
tail++;
// 千万记得要对第一个节点标记哟!
book[1] = 1;
while(head<tail)
{
cur = que[head];
printf("%d ", cur);
for(i=1; i<=n; i++)
{
if(e[cur][i] == 1 && book[i] == 0)
{
book[i] == 1;
que[tail] = i;
tail++;
}
// 这一步可以少运行几次循环
if(tail > n)
break;
}
head++;
}
printf("\n");
for(i=1; i<=n; i++)
printf("%d ", que[i]);
// getchar();getchar();
return 0;
}