-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy path1081 - DFSr - Depth Hierarchy.cpp
57 lines (47 loc) · 1.03 KB
/
1081 - DFSr - Depth Hierarchy.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
#include <stdio.h>
#include <vector>
#include <memory.h>
#include <algorithm>
using namespace std;
int n, m;
bool vis[20], blankline;
vector<vector<int> > g;
void DFS(int node, int depth) {
vis[node] = true;
for(int i = 0; i < (int)g[node].size(); ++i)
if(!vis[g[node][i]]) {
blankline = true;
for(int j = 0; j < depth; ++j)
printf(" ");
printf("%d-%d pathR(G,%d)\n", node, g[node][i], g[node][i]);
DFS(g[node][i], depth + 1);
} else {
for(int j = 0; j < depth; ++j)
printf(" ");
printf("%d-%d\n", node, g[node][i]);
}
}
int main() {
int t;
scanf("%d", &t);
for(int cnt = 1; t-- != 0; ++cnt) {
scanf("%d %d", &n, &m);
g.clear();
g.resize(n);
for(int i = 0, a, b; i < m; ++i) {
scanf("%d %d", &a, &b);
g[a].push_back(b);
}
for(int i = 0; i < n; ++i)
sort(g[i].begin(), g[i].end());
printf("Caso %d:\n", cnt);
memset(vis, false, sizeof vis);
for(int i = 0; i < n; ++i)
if(!vis[i]) {
blankline = false;
DFS(i, 1);
blankline ? puts("") : NULL;
}
}
return 0;
}