-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDBGcpp.cpp
77 lines (72 loc) · 1.67 KB
/
DBGcpp.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
#include <cstdio>
#include <cstdlib>
#include <vector>
using namespace std;
FILE *f_g, *f_s, *f_r;
int N, maxd = 90000;
vector<vector<int> > g;
vector<bool> visited, visited_back;
vector<int> maxto, maxto_back;
vector<int> S;
int dfs(int u, int dep = 0) {
visited[u] = true;
int maxdeepu = 1;
if (dep == maxd) return maxdeepu;
for (int v : g[u]) {
if (visited[v]) continue;
int maxdeepv = dfs(v, dep + 1);
if (maxdeepv + 1 > maxdeepu) {
maxdeepu = maxdeepv + 1;
maxto[u] = v;
}
}
return maxdeepu;
}
int main() {
// open file
f_g = fopen("Graph.txt", "r");
assert(f_g != NULL);
f_s = fopen("Start.txt", "r");
assert(f_s != NULL);
f_r = fopen("Result.txt", "w");
assert(f_r != NULL);
// read data
fscanf(f_g, "%d", &N);
int tote = 0;
for (int i = 0; i < N; i++) {
vector<int> now;
int totnow, v;
fscanf(f_g, "%d", &totnow);
tote += totnow;
for (int j = 0; j < totnow; j++) {
fscanf(f_g, "%d", &v);
now.push_back(v);
}
g.push_back(now);
maxto_back.push_back(-1);
visited_back.push_back(false);
}
fclose(f_g);
// read st
int tots;
fscanf(f_s, "%d", &tots);
for (int i = 0, j; i < tots; i++) {
fscanf(f_s, "%d", &j);
S.push_back(j);
}
fclose(f_s);
// try dfs
for (int s : S) {
visited = visited_back;
maxto = maxto_back;
dfs(s);
s = maxto[s];
while (s != -1) {
fprintf(f_r, "%d ", s);
s = maxto[s];
}
fprintf(f_r, "\n");
}
fclose(f_r);
return 0;
}