제출 #839118

#제출 시각아이디문제언어결과실행 시간메모리
839118serifefedartar동기화 (JOI13_synchronization)C++17
100 / 100
478 ms28824 KiB
#include <bits/stdc++.h>
using namespace std;
 
#define debug(x) {cout << #x << ": "; for (auto it : x) cout << it << " ";cout << endl;}
#define fast ios::sync_with_stdio(0);cin.tie(0);
typedef long long ll;
#define f first
#define s second
#define MOD 1000000007
#define LOGN 20
#define MAXN 100005

vector<vector<int>> graph;
vector<pair<int,int>> edges;
int fen[2*MAXN], tin[MAXN], tout[MAXN];
int up[LOGN][MAXN], val[MAXN], sync_up[MAXN];
int par[MAXN], active[MAXN], depth[MAXN];
void update(int k, int val) {
	while (k < 2*MAXN) {
		fen[k] += val;
		k += k&-k;
	}
}

int query(int k) {
	int res = 0;
	while (k) {
		res += fen[k];
		k -= k&-k;
	}
	return res;
}
	
int T = 0;
void dfs(int node, int parent) {
	tin[node] = ++T;
	for (auto u : graph[node]) {
		if (u == parent)
			continue;
		depth[u] = depth[node] + 1;
		up[0][u] = node;
		for (int i = 1; i < LOGN; i++)
			up[i][u] = up[i-1][up[i-1][u]];
		dfs(u, node); 
	}
	tout[node] = ++T;
}

int upmost(int node) {
	for (int i = LOGN-1; i >= 0; i--) {
		if (query(tin[node]) == query(tin[up[i][node]]))
			node = up[i][node];
	}
	return node;
}

int main() {
	fast
	int N, M, Q, A, B;
	cin >> N >> M >> Q;

	graph = vector<vector<int>>(N+1, vector<int>());
	for (int i = 1; i <= N; i++)
		val[i] = 1;
	for (int i = 1; i < N; i++) {
		cin >> A >> B;
		graph[A].push_back(B);
		graph[B].push_back(A);
		edges.push_back({A, B});
	}
	for (int i = 0; i < LOGN; i++)
		up[i][1] = 1; 
	dfs(1, 1);

	for (int i = 1; i <= N; i++) {
		update(tin[i], 1);
		update(tout[i]+1, -1);
	}

	for (int i = 0; i < M; i++) {
		int x, up, down;
		cin >> x;
		if (depth[edges[x-1].f] > depth[edges[x-1].s])
			down = edges[x-1].f, up = edges[x-1].s;
		else
			down = edges[x-1].s, up = edges[x-1].f;

		if (active[x]) {
			par[down] = down;
			val[down] = val[upmost(down)]; 
			sync_up[down] = val[down];
			update(tin[down], 1);
			update(tout[down]+1, -1);
			active[x] = false;
		} else {
			par[down] = upmost(up);
			val[par[down]] = val[par[down]] + val[down] - sync_up[down];
			update(tin[down], -1);
			update(tout[down]+1, 1);
			active[x] = true;
		}
	}

	while (Q--) {
		int x;
		cin >> x;
		cout << val[upmost(x)] << "\n"; 
	}
}
#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...