제출 #29003

#제출 시각아이디문제언어결과실행 시간메모리
29003h0ngjun7트리 (KOI16_tree)C++14
100 / 100
476 ms37728 KiB
#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");
		}
	}
}

컴파일 시 표준 에러 (stderr) 메시지

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);
                                                     ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...