# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
29003 | h0ngjun7 | 트리 (KOI16_tree) | C++14 | 476 ms | 37728 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |