Submission #839117

# Submission time Handle Problem Language Result Execution time Memory
839117 2023-08-28T17:28:08 Z serifefedartar Synchronization (JOI13_synchronization) C++17
50 / 100
333 ms 28860 KB
#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(node) == query(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 time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Incorrect 0 ms 468 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 73 ms 23752 KB Output is correct
2 Correct 72 ms 23596 KB Output is correct
3 Correct 89 ms 27108 KB Output is correct
4 Correct 93 ms 27072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 456 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 2 ms 724 KB Output is correct
7 Correct 18 ms 3284 KB Output is correct
8 Correct 333 ms 28816 KB Output is correct
9 Correct 314 ms 28816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 312 ms 28860 KB Output is correct
2 Correct 140 ms 28140 KB Output is correct
3 Correct 136 ms 28344 KB Output is correct
4 Correct 140 ms 28368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Incorrect 1 ms 468 KB Output isn't correct
3 Halted 0 ms 0 KB -