-
Notifications
You must be signed in to change notification settings - Fork 0
/
1330_dfs.cpp
58 lines (50 loc) · 1.25 KB
/
1330_dfs.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
#include <iostream>
#include <cstdio>
#include <vector>
#include <string.h>
using namespace std;
int head[10001];
int next[10001];
int node[10001];
int father[10001];
int color[10001];
int T, N, pos, x, y;
int get_father(int x) {
if (x == father[x]) return x;
else return father[x] = get_father(father[x]);
}
void dfs(int u) {
father[u] = u;
for (int i = head[u]; i != -1; i = next[i]) {
dfs(node[i]);
father[node[i]] = u;
}
color[u] = 1;
if (color[x] == 1 && color[y] == 1) {
printf("%d\n",get_father(x));
}
}
int main() {
scanf("%d", &T);
while (T--) {
memset(head, -1, sizeof(head));
memset(next, -1, sizeof(next));
memset(node, -1, sizeof(node));
memset(father, -1, sizeof(father));
memset(color, -1, sizeof(color));
vector<int> is_head(10001,1);
scanf("%d", &N);
for (int i = 0; i < N - 1; ++i) {
int s, e;
scanf("%d %d", &s, &e);
node[pos] = e;
next[s] = head[s];
head[s] = pos++;
is_head[e] = 0;
}
scanf("%d %d", &x, &y);
for (int i = 1; i < N; ++i)
if (is_head[i] == 1) { dfs(i); break;}
}
return 0;
}