-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDFSr - Depth Hierarchy.cpp
More file actions
85 lines (67 loc) · 1.44 KB
/
DFSr - Depth Hierarchy.cpp
File metadata and controls
85 lines (67 loc) · 1.44 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define MAX 20
#define sc(a) scanf("%d", &a);
#define sc2(a, b) scanf("%d%d", &a, &b);
bool disc[MAX];
int graph[MAX][MAX];
void clean(int v) {
int i, j;
for (i = 0; i < v; i++) {
for (j = 0; j < v; j++)
graph[i][j] = 0;
disc[i] = false;
}
}
bool dfs(int v, int n, int s) {
int i;
bool path = false;
disc[v] = true;
for (i = 0; i < n; i++) {
if (graph[v][i] == 1) {
path = true;
if (!disc[i]) {
cout << string(s, ' ');
printf("%d-%d pathR(G,%d)\n", v, i, i);
dfs(i, n, s + 2);
} else {
cout << string(s, ' ');
printf("%d-%d\n", v, i);
}
}
}
return path;
}
void dfs_runner(int v) {
int i, ind = 0;
while (1) {
if (dfs(ind, v, 2))
puts("");
ind = -1;
for (i = 0; i < v; i++) {
if (!disc[i]) {
ind = i;
break;
}
}
if (ind == -1)
break;
}
}
int main(int argc, char const *argv[]) {
int n, v, e, o, d, c = 1;
sc(n);
while(n--) {
sc2(v, e);
clean(v);
while(e--) {
sc2(o, d);
graph[o][d] = 1;
}
printf("Caso %d:\n", c++);
dfs_runner(v);
}
return 0;
}