답안 #547976

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
547976 2022-04-12T05:33:16 Z fhvirus 동기화 (JOI13_synchronization) C++17
100 / 100
459 ms 21356 KB
#include <bits/stdc++.h>
using namespace std;

const int kL = 17;
const int kN = 100'001;
int N, M, Q;
int X[kN], Y[kN];
vector<int> aj[kN];

int in[kN], ou[kN], tot;
int jp[kL][kN];

void dfs(int u, int p) {
	jp[0][u] = p;
	in[u] = ++tot;
	for (const int& v: aj[u]) {
		if (v == p) continue;
		dfs(v, u);
	}
	ou[u] = tot;
}

int val[kN];
void mo(int p, int v) {
	for (; p <= N; p += p & -p)
		val[p] += v;
}
int qu(int p) {
	int r = 0;
	for (; p > 0; p -= p & -p)
		r += val[p];
	return r;
}

int gh(int u) {
	int v = qu(in[u]);
	for (int l = kL - 1; l >= 0; --l)
		if (qu(in[jp[l][u]]) == v)
			u = jp[l][u];
	return u;
}

int ans[kN], tag[kN];
bitset<kN> st;

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);

	cin >> N >> M >> Q;

	for (int i = 1; i + 1 <= N; ++i) {
		cin >> X[i] >> Y[i];
		aj[X[i]].push_back(Y[i]);
		aj[Y[i]].push_back(X[i]);
	}

	dfs(1, 1);

	for (int l = 1; l < kL; ++l)
		for (int i = 1; i <= N; ++i)
			jp[l][i] = jp[l - 1][jp[l - 1][i]];

	for (int i = 1; i <= N; ++i) {
		mo(in[i], 1);
		mo(ou[i] + 1, -1);
	}

	fill(ans + 1, ans + N + 1, 1);
	fill(tag + 1, tag + N + 1, 1);

	for (int D, i = 1; i <= M; ++i) {
		cin >> D;
		int u = (X[D] == jp[0][Y[D]] ? Y[D] : X[D]);
		if (st[D]) {
			int h = gh(u);
			ans[u] = ans[h];
			mo(in[u], 1);
			mo(ou[u] + 1, -1);
		} else {
			mo(in[u], -1);
			mo(ou[u] + 1, 1);
			int h = gh(u);
			ans[h] += tag[u];
			tag[h] += tag[u];
			tag[u] = 0;
		}
		st.flip(D);
	}

	for (int C, i = 1; i <= Q; ++i) {
		cin >> C;
		cout << ans[gh(C)] << '\n';
	}

	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2772 KB Output is correct
2 Correct 2 ms 2772 KB Output is correct
3 Correct 2 ms 2676 KB Output is correct
4 Correct 2 ms 2684 KB Output is correct
5 Correct 2 ms 2772 KB Output is correct
6 Correct 3 ms 2900 KB Output is correct
7 Correct 17 ms 4180 KB Output is correct
8 Correct 17 ms 4132 KB Output is correct
9 Correct 15 ms 4248 KB Output is correct
10 Correct 340 ms 15944 KB Output is correct
11 Correct 333 ms 15872 KB Output is correct
12 Correct 334 ms 20528 KB Output is correct
13 Correct 95 ms 16204 KB Output is correct
14 Correct 224 ms 15916 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 84 ms 18380 KB Output is correct
2 Correct 85 ms 18312 KB Output is correct
3 Correct 86 ms 20500 KB Output is correct
4 Correct 97 ms 20496 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2772 KB Output is correct
2 Correct 2 ms 2772 KB Output is correct
3 Correct 2 ms 2772 KB Output is correct
4 Correct 2 ms 2680 KB Output is correct
5 Correct 2 ms 2772 KB Output is correct
6 Correct 3 ms 2976 KB Output is correct
7 Correct 21 ms 4744 KB Output is correct
8 Correct 459 ms 21296 KB Output is correct
9 Correct 450 ms 21284 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 421 ms 21356 KB Output is correct
2 Correct 137 ms 21024 KB Output is correct
3 Correct 132 ms 21128 KB Output is correct
4 Correct 137 ms 21136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2772 KB Output is correct
2 Correct 2 ms 2684 KB Output is correct
3 Correct 2 ms 2676 KB Output is correct
4 Correct 2 ms 2676 KB Output is correct
5 Correct 3 ms 2900 KB Output is correct
6 Correct 22 ms 4292 KB Output is correct
7 Correct 392 ms 16712 KB Output is correct
8 Correct 414 ms 21136 KB Output is correct
9 Correct 119 ms 16704 KB Output is correct
10 Correct 226 ms 16612 KB Output is correct
11 Correct 115 ms 19048 KB Output is correct
12 Correct 118 ms 19084 KB Output is correct
13 Correct 142 ms 21152 KB Output is correct