Submission #957474

# Submission time Handle Problem Language Result Execution time Memory
957474 2024-04-03T20:44:00 Z Mizo_Compiler Synchronization (JOI13_synchronization) C++17
0 / 100
99 ms 64968 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double ld;
#define pb push_back
#define sz(x) int32_t(x.size())
#define all(x) x.begin(),x.end()
#define F first
#define S second
const int N = 1e5;
int n, m, q, p[N][17], cur = 0, par[N], sz[N], idx[N];
vector<int> adj[N], edg[(1 << 18)];
vector<pair<int, int>> ed;
bool vis[int(2e5)], qr[N];
ll ans[N], tk[N];
vector<pair<int, int>> vv;

int findp(int u) {
	return (par[u] == u ? u : findp(par[u]));
}

void merge(int u, int v) {
	u = findp(u), v = findp(v);
	if (u == v)return;
	cur++;
	if (sz[u] < sz[v])swap(u, v);
	sz[u] += sz[v];
	par[v] = u;
	vv.pb({u, v});
}

void rollback(int tm) {
	while (cur > tm) {
		par[vv.back().S] = vv.back().S;
		sz[vv.back().F] -= sz[vv.back().S];
		vv.pop_back();
		cur--;
	}
}

void dfs(int u) {
	for (auto &v : adj[u]) {
		if (v == p[u][0])continue;
		p[v][0] = u;
		for (int i = 1; i < 17; i++) {
			p[v][i] = p[p[v][i-1]][i-1];
		}
		dfs(v);
	}
}

void upd(int li, int ri, int x, int l, int r, int id) {
	if (li >= l && ri <= r) {
		edg[x].pb(id);
		return;
	}
	if (li > r || ri < l)return;
	int mid = (li + ri) >> 1;
	upd(li, mid, x*2+1, l, r, id);
	upd(mid+1, ri, x*2+2, l, r, id);
}

int getp(int u) {
	int x = findp(u);
	for (int i = 16; i >= 0; i--) {
		if (findp(p[u][i]) == x) {
			u = p[u][i];
		}
	}
	return u;
}

void sol(int i) {
	int u = ed[idx[i]].F, v = ed[idx[i]].S;
	int rt = getp(u);
	if (qr[i]) {
		ans[rt] += ans[v] - tk[v];
		tk[v] = ans[v];
	} else {
		ans[v] = ans[rt];
		tk[v] = ans[v];
	}
}

void walk(int l, int r, int x) {
	int tmp = cur;
	for (auto &i : edg[x]) {
		merge(ed[i].F, ed[i].S);
	}
	if (l == r) {
		sol(l);
		rollback(tmp);
		return;
	}
	int mid = (l + r) >> 1;
	walk(l, mid, x*2+1);
	walk(mid+1, r, x*2+2);
	rollback(tmp);
	return;
}

signed main () {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cin >> n >> m >> q;
	for (int i = 0; i+1 < n; i++) {
		int u, v;
		cin >> u >> v;
		--u, --v;
		adj[u].pb(v);
		adj[v].pb(u);
		ed.pb({u, v});
		ans[i] = 1;
		sz[i] = 1;
		par[i] = i;
	}
	sz[n-1] = ans[n-1] = 1;
	par[n-1] = n-1;
	dfs(0);
	for (int i = 0; i+1 < n; i++) {
		if (p[ed[i].F][0] == ed[i].S) {
			swap(ed[i].F, ed[i].S);
		}
	}
	map<int, int> mp;
	for (int i = 0; i < m; i++) {
		int x;
		cin >> x;
		--x;
		vis[x] ^= 1;
		if (vis[x])mp[x] = i;
		else upd(0, m-1, 0, mp[x], i-1, x), mp.erase(x);
		qr[i] = vis[x];
		idx[i] = x;
	}
	for (auto &[x, y] : mp) {
		upd(0, m-1, 0, y, m-1, x);
	}
	walk(0, m-1, 0);
	for (auto &[x, y] : mp) {
		merge(ed[x].F, ed[x].S);
	}
	while (q--) {
		int u;
		cin >> u;
		--u;
		cout << ans[getp(u)] << "\n";
	}
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 12636 KB Output is correct
2 Correct 2 ms 12636 KB Output is correct
3 Correct 3 ms 12636 KB Output is correct
4 Correct 4 ms 12636 KB Output is correct
5 Correct 3 ms 12632 KB Output is correct
6 Correct 4 ms 12892 KB Output is correct
7 Correct 20 ms 14940 KB Output is correct
8 Correct 20 ms 14940 KB Output is correct
9 Correct 21 ms 14940 KB Output is correct
10 Runtime error 96 ms 55840 KB Execution killed with signal 11
11 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 68 ms 57536 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 12632 KB Output is correct
2 Correct 3 ms 12636 KB Output is correct
3 Correct 2 ms 12636 KB Output is correct
4 Correct 2 ms 12636 KB Output is correct
5 Correct 2 ms 12636 KB Output is correct
6 Correct 4 ms 12892 KB Output is correct
7 Correct 24 ms 15704 KB Output is correct
8 Runtime error 94 ms 64968 KB Execution killed with signal 11
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 97 ms 64960 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12636 KB Output is correct
2 Correct 3 ms 12636 KB Output is correct
3 Correct 2 ms 12636 KB Output is correct
4 Correct 3 ms 12772 KB Output is correct
5 Correct 4 ms 12892 KB Output is correct
6 Correct 23 ms 14916 KB Output is correct
7 Runtime error 99 ms 55636 KB Execution killed with signal 11
8 Halted 0 ms 0 KB -