-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbf_df.cpp
84 lines (65 loc) · 1.12 KB
/
bf_df.cpp
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <fstream>
using namespace std;
int c[100], viz[100], n, a[100][100];
void bf(int x){
c[0] = x;
viz[x] = 1;
int p = 0;
int u = 0;
while(p<=u){
int k = c[p];
for(int i=1; i<=n; i++){
if(1 == a[k][i] && 0 == viz[i]){
u++;
c[u] = i;
viz[i] = 1;
}
}
p++;
}
}
void bf_tip(){
int i=0;
while(c[i]){
cout<<c[i++]<<' ';
}
}
void init(){
for(int i=0; i<=n; i++){
c[i] = 0;
viz[i] = 0;
}
}
void df(int x){
viz[x] = 1;
cout<<x<<' ';
for(int i=1; i<=n; i++){
if(a[x][i] && !viz[i]){
df(i);
}
}
}
int main(){
int m, x, y;
ifstream f("bf_df.in");
f>>n>>m;
for(int i=0; i<=m; i++){
f>>x>>y;
a[x][y] = a[y][x] = 1;
}
cout<<"BF:\n";
for(int i=1; i<=n; i++){
bf(i);
bf_tip();
init();
cout<<"\n";
}
cout<<"DF:\n";
for(int i=1; i<=n; i++){
df(i);
init();
cout<<"\n";
}
f.close();
}