#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
#define pb push_back
#define sz size
#define all(X) (X).begin(), (X).end ()
typedef pair < int, int > pii;
const int MAXN = 200005;
int N, M, Lev[MAXN], Par[MAXN], SZ[MAXN], bn, dfsn[MAXN], dn, Tail[MAXN];
int Chain[4 * MAXN], Time[4 * MAXN];
vector <int> E[MAXN];
void DFS(int x, int par) {
Par[x] = par;
SZ[x] = 1;
for (auto &it : E[x]) {
if (it != par) {
Lev[it] = Lev[x] + 1;
DFS(it, x);
SZ[x] += SZ[it];
}
}
}
void hld(int x, int par, int Color) {
dfsn[x] = ++dn;
Chain[bn + dn] = Color;
Tail[Color] = dn;
int idx = -1;
for (auto &it : E[x]) {
if (it != par && (idx == -1 || SZ[it] > SZ[x]))
idx = it;
}
if (idx != -1) hld(idx, x, Color);
for (auto &it : E[x]) {
if (it != par && it != idx)
hld(it, x, it);
}
}
int get(int x) {
x += bn;
int ret = 0, rt = -1;
while (x) {
if (Time[x] > rt) rt = Time[x], ret = Chain[x];
x /= 2;
}
return ret;
}
void cut(int y, int i) {
if (Par[y] == 0) return;
int x = Par[y], Y = y;
int l = dfsn[y], r = Tail[get(dfsn[y])];
if (get(dfsn[y]) == get(dfsn[x])) Tail[get(dfsn[x])] = dfsn[x];
Par[Y] = 0; Tail[Y] = r;
l += bn; r += bn;
while (l <= r) {
if (l == r) {
Time[l] = i; Chain[l] = Y;
break;
}
if (l & 1) {
Time[l] = i; Chain[l] = Y;
l++;
}
if (!(r & 1)) {
Time[r] = i; Chain[r] = Y;
r--;
}
l /= 2; r /= 2;
}
}
int main() {
scanf("%d%d", &N, &M);
bn = 1; while (bn < N) bn *= 2; bn--;
for (int i = 1; i < N; i++) {
int x = i + 1, y; scanf("%d", &y);
E[x].pb(y); E[y].pb(x);
}
DFS(1, 0);
hld(1, 0, 1);
for (int i = 1; i <= M; i++) {
if (i == 7)
i = i;
int op, x, y, tx, ty; scanf("%d%d%d", &x, &y, &op);
tx = x, ty = y;
x = get(dfsn[x]), y = get(dfsn[y]);
while (x != y) {
if (Lev[x] < Lev[y]) {
y = Par[y];
if (!y) break;
y = get(dfsn[y]);
}
else {
x = Par[x];
if (!x) break;
x = get(dfsn[x]);
}
}
if (!x || !y) {
if (op == 1) cut(ty, i);
puts("NO");
}
else {
if (op == 1) cut(tx, i);
puts("YES");
}
}
}
Compilation message
tree.cpp: In function 'int main()':
tree.cpp:72:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &N, &M);
^
tree.cpp:75:36: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int x = i + 1, y; scanf("%d", &y);
^
tree.cpp:83:53: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int op, x, y, tx, ty; scanf("%d%d%d", &x, &y, &op);
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16020 KB |
Output is correct |
2 |
Correct |
0 ms |
16020 KB |
Output is correct |
3 |
Correct |
3 ms |
16020 KB |
Output is correct |
4 |
Correct |
0 ms |
16020 KB |
Output is correct |
5 |
Correct |
0 ms |
16020 KB |
Output is correct |
6 |
Correct |
3 ms |
16020 KB |
Output is correct |
7 |
Correct |
0 ms |
16020 KB |
Output is correct |
8 |
Correct |
3 ms |
16020 KB |
Output is correct |
9 |
Correct |
0 ms |
16020 KB |
Output is correct |
10 |
Correct |
0 ms |
16020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16020 KB |
Output is correct |
2 |
Correct |
0 ms |
16020 KB |
Output is correct |
3 |
Correct |
3 ms |
16020 KB |
Output is correct |
4 |
Correct |
0 ms |
16020 KB |
Output is correct |
5 |
Correct |
0 ms |
16020 KB |
Output is correct |
6 |
Correct |
3 ms |
16020 KB |
Output is correct |
7 |
Correct |
0 ms |
16020 KB |
Output is correct |
8 |
Correct |
3 ms |
16020 KB |
Output is correct |
9 |
Correct |
0 ms |
16020 KB |
Output is correct |
10 |
Correct |
0 ms |
16020 KB |
Output is correct |
11 |
Correct |
0 ms |
16020 KB |
Output is correct |
12 |
Correct |
0 ms |
16020 KB |
Output is correct |
13 |
Correct |
0 ms |
16020 KB |
Output is correct |
14 |
Correct |
0 ms |
16020 KB |
Output is correct |
15 |
Correct |
0 ms |
16020 KB |
Output is correct |
16 |
Correct |
0 ms |
16020 KB |
Output is correct |
17 |
Correct |
3 ms |
16020 KB |
Output is correct |
18 |
Correct |
0 ms |
16020 KB |
Output is correct |
19 |
Correct |
0 ms |
16020 KB |
Output is correct |
20 |
Correct |
0 ms |
16020 KB |
Output is correct |
21 |
Correct |
0 ms |
16020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16020 KB |
Output is correct |
2 |
Correct |
0 ms |
16020 KB |
Output is correct |
3 |
Correct |
3 ms |
16020 KB |
Output is correct |
4 |
Correct |
0 ms |
16020 KB |
Output is correct |
5 |
Correct |
0 ms |
16020 KB |
Output is correct |
6 |
Correct |
3 ms |
16020 KB |
Output is correct |
7 |
Correct |
0 ms |
16020 KB |
Output is correct |
8 |
Correct |
3 ms |
16020 KB |
Output is correct |
9 |
Correct |
0 ms |
16020 KB |
Output is correct |
10 |
Correct |
0 ms |
16020 KB |
Output is correct |
11 |
Correct |
0 ms |
16020 KB |
Output is correct |
12 |
Correct |
0 ms |
16020 KB |
Output is correct |
13 |
Correct |
0 ms |
16020 KB |
Output is correct |
14 |
Correct |
0 ms |
16020 KB |
Output is correct |
15 |
Correct |
0 ms |
16020 KB |
Output is correct |
16 |
Correct |
0 ms |
16020 KB |
Output is correct |
17 |
Correct |
3 ms |
16020 KB |
Output is correct |
18 |
Correct |
0 ms |
16020 KB |
Output is correct |
19 |
Correct |
0 ms |
16020 KB |
Output is correct |
20 |
Correct |
0 ms |
16020 KB |
Output is correct |
21 |
Correct |
0 ms |
16020 KB |
Output is correct |
22 |
Correct |
119 ms |
16152 KB |
Output is correct |
23 |
Correct |
86 ms |
16152 KB |
Output is correct |
24 |
Correct |
89 ms |
16152 KB |
Output is correct |
25 |
Correct |
103 ms |
16152 KB |
Output is correct |
26 |
Correct |
106 ms |
16152 KB |
Output is correct |
27 |
Correct |
93 ms |
16152 KB |
Output is correct |
28 |
Correct |
126 ms |
16152 KB |
Output is correct |
29 |
Correct |
113 ms |
16152 KB |
Output is correct |
30 |
Correct |
149 ms |
16152 KB |
Output is correct |
31 |
Correct |
153 ms |
16152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16020 KB |
Output is correct |
2 |
Correct |
0 ms |
16020 KB |
Output is correct |
3 |
Correct |
3 ms |
16020 KB |
Output is correct |
4 |
Correct |
0 ms |
16020 KB |
Output is correct |
5 |
Correct |
0 ms |
16020 KB |
Output is correct |
6 |
Correct |
3 ms |
16020 KB |
Output is correct |
7 |
Correct |
0 ms |
16020 KB |
Output is correct |
8 |
Correct |
3 ms |
16020 KB |
Output is correct |
9 |
Correct |
0 ms |
16020 KB |
Output is correct |
10 |
Correct |
0 ms |
16020 KB |
Output is correct |
11 |
Correct |
416 ms |
37720 KB |
Output is correct |
12 |
Correct |
433 ms |
37728 KB |
Output is correct |
13 |
Correct |
419 ms |
37728 KB |
Output is correct |
14 |
Correct |
386 ms |
37728 KB |
Output is correct |
15 |
Correct |
466 ms |
37728 KB |
Output is correct |
16 |
Correct |
399 ms |
37724 KB |
Output is correct |
17 |
Correct |
459 ms |
37720 KB |
Output is correct |
18 |
Correct |
449 ms |
37724 KB |
Output is correct |
19 |
Correct |
476 ms |
37720 KB |
Output is correct |
20 |
Correct |
449 ms |
37724 KB |
Output is correct |
21 |
Correct |
423 ms |
37728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16020 KB |
Output is correct |
2 |
Correct |
0 ms |
16020 KB |
Output is correct |
3 |
Correct |
3 ms |
16020 KB |
Output is correct |
4 |
Correct |
0 ms |
16020 KB |
Output is correct |
5 |
Correct |
0 ms |
16020 KB |
Output is correct |
6 |
Correct |
3 ms |
16020 KB |
Output is correct |
7 |
Correct |
0 ms |
16020 KB |
Output is correct |
8 |
Correct |
3 ms |
16020 KB |
Output is correct |
9 |
Correct |
0 ms |
16020 KB |
Output is correct |
10 |
Correct |
0 ms |
16020 KB |
Output is correct |
11 |
Correct |
0 ms |
16020 KB |
Output is correct |
12 |
Correct |
0 ms |
16020 KB |
Output is correct |
13 |
Correct |
0 ms |
16020 KB |
Output is correct |
14 |
Correct |
0 ms |
16020 KB |
Output is correct |
15 |
Correct |
0 ms |
16020 KB |
Output is correct |
16 |
Correct |
0 ms |
16020 KB |
Output is correct |
17 |
Correct |
3 ms |
16020 KB |
Output is correct |
18 |
Correct |
0 ms |
16020 KB |
Output is correct |
19 |
Correct |
0 ms |
16020 KB |
Output is correct |
20 |
Correct |
0 ms |
16020 KB |
Output is correct |
21 |
Correct |
0 ms |
16020 KB |
Output is correct |
22 |
Correct |
119 ms |
16152 KB |
Output is correct |
23 |
Correct |
86 ms |
16152 KB |
Output is correct |
24 |
Correct |
89 ms |
16152 KB |
Output is correct |
25 |
Correct |
103 ms |
16152 KB |
Output is correct |
26 |
Correct |
106 ms |
16152 KB |
Output is correct |
27 |
Correct |
93 ms |
16152 KB |
Output is correct |
28 |
Correct |
126 ms |
16152 KB |
Output is correct |
29 |
Correct |
113 ms |
16152 KB |
Output is correct |
30 |
Correct |
149 ms |
16152 KB |
Output is correct |
31 |
Correct |
153 ms |
16152 KB |
Output is correct |
32 |
Correct |
416 ms |
37720 KB |
Output is correct |
33 |
Correct |
433 ms |
37728 KB |
Output is correct |
34 |
Correct |
419 ms |
37728 KB |
Output is correct |
35 |
Correct |
386 ms |
37728 KB |
Output is correct |
36 |
Correct |
466 ms |
37728 KB |
Output is correct |
37 |
Correct |
399 ms |
37724 KB |
Output is correct |
38 |
Correct |
459 ms |
37720 KB |
Output is correct |
39 |
Correct |
449 ms |
37724 KB |
Output is correct |
40 |
Correct |
476 ms |
37720 KB |
Output is correct |
41 |
Correct |
449 ms |
37724 KB |
Output is correct |
42 |
Correct |
423 ms |
37728 KB |
Output is correct |
43 |
Correct |
369 ms |
23016 KB |
Output is correct |
44 |
Correct |
456 ms |
23016 KB |
Output is correct |
45 |
Correct |
396 ms |
23016 KB |
Output is correct |
46 |
Correct |
446 ms |
23016 KB |
Output is correct |
47 |
Correct |
396 ms |
23016 KB |
Output is correct |
48 |
Correct |
199 ms |
23284 KB |
Output is correct |
49 |
Correct |
229 ms |
23616 KB |
Output is correct |
50 |
Correct |
189 ms |
23556 KB |
Output is correct |
51 |
Correct |
366 ms |
26004 KB |
Output is correct |