Submission #594100

# Submission time Handle Problem Language Result Execution time Memory
594100 2022-07-12T05:38:53 Z penguinhacker Spring cleaning (CEOI20_cleaning) C++17
100 / 100
225 ms 19276 KB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ar array

const int mxN=1e5;
int n, q, rt, s[mxN], hd[mxN], p[mxN], tin[mxN], t, st[1<<18];
vector<int> adj[mxN];
bool leaf[mxN], parity[mxN], init[mxN], lz[1<<18], vis[mxN];

void dfs1(int u) {
	s[u]=1;
	if (adj[u].empty()) {
		leaf[u]=parity[u]=1;
		return;
	}
	for (int& v : adj[u]) {
		adj[v].erase(find(adj[v].begin(), adj[v].end(), u));
		p[v]=u;
		dfs1(v);
		parity[u]^=parity[v];
		s[u]+=s[v];
		if (s[v]>s[adj[u][0]])
			swap(adj[u][0], v);
	}
}

void dfs2(int u, int h) {
	init[t]=parity[u];
	tin[u]=t++;
	hd[u]=h;
	if (adj[u].size()) {
		dfs2(adj[u][0], h);
		for (int i=1; i<adj[u].size(); ++i)
			dfs2(adj[u][i], adj[u][i]);
	}
}

void bld(int u=1, int l=0, int r=n-1) {
	if (l==r) {
		st[u]=init[l];
		return;
	}
	int mid=(l+r)/2;
	bld(2*u, l, mid);
	bld(2*u+1, mid+1, r);
	st[u]=st[2*u]+st[2*u+1];
}

void psh(int u, int l, int r) {
	if (lz[u]) {
		st[u]=r-l+1-st[u];
		if (l!=r)
			lz[2*u]^=1, lz[2*u+1]^=1;
		lz[u]=0;
	}
}

void upd(int ql, int qr, int u=1, int l=0, int r=n-1) {
	psh(u, l, r);
	if (l>qr||r<ql)
		return;
	if (ql<=l&&r<=qr) {
		lz[u]=1;
		psh(u, l, r);
		return;
	}
	int mid=(l+r)/2;
	upd(ql, qr, 2*u, l, mid);
	upd(ql, qr, 2*u+1, mid+1, r);
	st[u]=st[2*u]+st[2*u+1];
}

void upd_node(int u) {
	int ops=1;
	for (; hd[u]!=rt; u=p[hd[u]])
		upd(tin[hd[u]], tin[u]), ++ops;
	upd(0, tin[u]);
	assert(ops<19);
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> n >> q;
	for (int i=1; i<n; ++i) {
		int u, v;
		cin >> u >> v, --u, --v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	for (int i=0; i<n; ++i)
		if (adj[i].size()>1) {
			rt=i;
			break;
		}
	dfs1(rt);
	dfs2(rt, rt);
	bld();
	while(q--) {
		int k;
		cin >> k;
		vector<int> nodes(k);
		bool leaves=parity[rt];
		for (int& i : nodes) {
			cin >> i, --i;
			if (leaf[i]&&!vis[i]) {
				vis[i]=1;
				leaves^=1;
			}
			leaves^=1;
		}
		if (leaves) { // odd number of leaves bad
			for (int i : nodes)
				vis[i]=0;
			cout << "-1\n";
		} else {
			vector<int> rb;
			for (int i : nodes) {
				if (vis[i]) {
					vis[i]=0;
					continue;
				}
				upd_node(i);
				rb.push_back(i);
			}
			int ans=2*(n-1)-st[1]+nodes.size();
			cout << ans << "\n";
			for (int i : rb)
				upd_node(i);
		}
	}
	return 0;
}

Compilation message

cleaning.cpp: In function 'void dfs2(int, int)':
cleaning.cpp:35:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   35 |   for (int i=1; i<adj[u].size(); ++i)
      |                 ~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 88 ms 4160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 50 ms 3876 KB Output is correct
2 Correct 8 ms 3156 KB Output is correct
3 Correct 35 ms 9336 KB Output is correct
4 Correct 58 ms 8428 KB Output is correct
5 Correct 58 ms 9916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 44 ms 4396 KB Output is correct
2 Correct 8 ms 3796 KB Output is correct
3 Correct 47 ms 18036 KB Output is correct
4 Correct 136 ms 17736 KB Output is correct
5 Correct 42 ms 16736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 61 ms 5196 KB Output is correct
2 Correct 44 ms 4440 KB Output is correct
3 Correct 10 ms 4116 KB Output is correct
4 Correct 10 ms 4872 KB Output is correct
5 Correct 13 ms 5124 KB Output is correct
6 Correct 48 ms 5208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 6780 KB Output is correct
2 Correct 183 ms 6656 KB Output is correct
3 Correct 173 ms 4756 KB Output is correct
4 Correct 199 ms 6732 KB Output is correct
5 Correct 185 ms 6716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 225 ms 9580 KB Output is correct
2 Correct 103 ms 12948 KB Output is correct
3 Correct 147 ms 14244 KB Output is correct
4 Correct 92 ms 13924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 88 ms 4160 KB Output is correct
3 Correct 50 ms 3876 KB Output is correct
4 Correct 8 ms 3156 KB Output is correct
5 Correct 35 ms 9336 KB Output is correct
6 Correct 58 ms 8428 KB Output is correct
7 Correct 58 ms 9916 KB Output is correct
8 Correct 44 ms 4396 KB Output is correct
9 Correct 8 ms 3796 KB Output is correct
10 Correct 47 ms 18036 KB Output is correct
11 Correct 136 ms 17736 KB Output is correct
12 Correct 42 ms 16736 KB Output is correct
13 Correct 61 ms 5196 KB Output is correct
14 Correct 44 ms 4440 KB Output is correct
15 Correct 10 ms 4116 KB Output is correct
16 Correct 10 ms 4872 KB Output is correct
17 Correct 13 ms 5124 KB Output is correct
18 Correct 48 ms 5208 KB Output is correct
19 Correct 43 ms 6780 KB Output is correct
20 Correct 183 ms 6656 KB Output is correct
21 Correct 173 ms 4756 KB Output is correct
22 Correct 199 ms 6732 KB Output is correct
23 Correct 185 ms 6716 KB Output is correct
24 Correct 225 ms 9580 KB Output is correct
25 Correct 103 ms 12948 KB Output is correct
26 Correct 147 ms 14244 KB Output is correct
27 Correct 92 ms 13924 KB Output is correct
28 Correct 133 ms 7788 KB Output is correct
29 Correct 132 ms 14620 KB Output is correct
30 Correct 37 ms 10808 KB Output is correct
31 Correct 105 ms 19276 KB Output is correct
32 Correct 182 ms 8044 KB Output is correct
33 Correct 131 ms 12648 KB Output is correct
34 Correct 162 ms 14592 KB Output is correct
35 Correct 130 ms 14456 KB Output is correct