Submission #543133

# Submission time Handle Problem Language Result Execution time Memory
543133 2022-03-29T12:50:39 Z sidon Spring cleaning (CEOI20_cleaning) C++17
100 / 100
226 ms 27724 KB
#include <bits/stdc++.h>
using namespace std;

const int Z = 1e5, B = 17;

int N, Q;
vector<int> g[Z], h[Z];
int p[Z][B], L[Z], R[Z], s[Z], d[Z], dfsTimer, rt, leafCnt, ext;

void init(int u) {
	L[u] = dfsTimer++;
	for(int i = 0; i + 1 < B; ++i)
		p[u][i+1] = p[p[u][i]][i];

	s[u] = size(g[u]) > 1;
	leafCnt += !s[u];

	for(const int &v : g[u]) if(v != p[u][0]) {
		p[v][0] = u;
		d[v] = d[u] + 1;
		init(v);
		s[u] ^= s[v] ^ 1;
	}
	if(u != rt) ext += s[u];
	R[u] = dfsTimer++;
}

void dfs(int u) {
	for(const int &v : g[u]) if(v != p[u][0])
		s[v] += s[u], dfs(v);
}

#define isAncestor(U, V) (L[U] < L[V] && R[V] < R[U])

int lca(int u, int v) {
	if(isAncestor(u, v)) return u;
	for(int i = B; i--; ) if(!isAncestor(p[u][i], v)) u = p[u][i];
	return p[u][0];
}

int diff, c[Z];
void calc(int u, int par) {
	for(const int &v : h[u]) if(v != par) {
		calc(v, u);
		c[u] ^= c[v];
	}
	if(c[u]) diff += (d[u] - d[par]) - 2 * (s[u] - s[par]);
}

int main() {
	ios_base::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;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	for(; size(g[rt]) < 2; ++rt);

	p[rt][0] = rt;
	init(rt);
	dfs(rt);

	while(Q--) {
		int D; cin >> D;

		vector<int> a(D);
		set<int> _a;

		for(int &u : a)	{
			cin >> u;
			c[--u] ^= 1;
			if(size(g[u]) < 2) _a.insert(u);
		}
		for(const int &u : _a)
			c[u] ^= 1;

		for(int t : {1, 0}) {
			sort(begin(a), end(a), [&](const int &i, const int &j) {
				return L[i] < L[j];
			});
			if(t) for(int i = D; --i; )
				a.push_back(lca(a[i], a[i-1]));
		}
		a.erase(unique(begin(a), end(a)), end(a));
		vector<int> st;
		diff = 0;

		for(const int &u : a) {
			if(!empty(st)) {
				for(; !isAncestor(st.back(), u); st.pop_back());
				h[st.back()].push_back(u);
			}
			st.push_back(u);
		}
		calc(a[0], rt);
		for(const int &u : a) {
			h[u].clear();
			c[u] = 0;
		}

		cout << ((leafCnt + D - int(size(_a))) & 1 ? -1 : N - 1 + D + ext + diff) << '\n';
	}
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4948 KB Output is correct
2 Correct 77 ms 8512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 6572 KB Output is correct
2 Correct 28 ms 6588 KB Output is correct
3 Correct 57 ms 18248 KB Output is correct
4 Correct 98 ms 18140 KB Output is correct
5 Correct 106 ms 22384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 7232 KB Output is correct
2 Correct 33 ms 7176 KB Output is correct
3 Correct 61 ms 25656 KB Output is correct
4 Correct 130 ms 27724 KB Output is correct
5 Correct 57 ms 23452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 48 ms 9044 KB Output is correct
2 Correct 27 ms 7984 KB Output is correct
3 Correct 12 ms 7600 KB Output is correct
4 Correct 12 ms 8148 KB Output is correct
5 Correct 18 ms 8276 KB Output is correct
6 Correct 51 ms 8972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 66 ms 14256 KB Output is correct
2 Correct 122 ms 14500 KB Output is correct
3 Correct 85 ms 10236 KB Output is correct
4 Correct 116 ms 14512 KB Output is correct
5 Correct 132 ms 14512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 111 ms 19264 KB Output is correct
2 Correct 112 ms 21956 KB Output is correct
3 Correct 133 ms 22024 KB Output is correct
4 Correct 116 ms 21404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4948 KB Output is correct
2 Correct 77 ms 8512 KB Output is correct
3 Correct 31 ms 6572 KB Output is correct
4 Correct 28 ms 6588 KB Output is correct
5 Correct 57 ms 18248 KB Output is correct
6 Correct 98 ms 18140 KB Output is correct
7 Correct 106 ms 22384 KB Output is correct
8 Correct 36 ms 7232 KB Output is correct
9 Correct 33 ms 7176 KB Output is correct
10 Correct 61 ms 25656 KB Output is correct
11 Correct 130 ms 27724 KB Output is correct
12 Correct 57 ms 23452 KB Output is correct
13 Correct 48 ms 9044 KB Output is correct
14 Correct 27 ms 7984 KB Output is correct
15 Correct 12 ms 7600 KB Output is correct
16 Correct 12 ms 8148 KB Output is correct
17 Correct 18 ms 8276 KB Output is correct
18 Correct 51 ms 8972 KB Output is correct
19 Correct 66 ms 14256 KB Output is correct
20 Correct 122 ms 14500 KB Output is correct
21 Correct 85 ms 10236 KB Output is correct
22 Correct 116 ms 14512 KB Output is correct
23 Correct 132 ms 14512 KB Output is correct
24 Correct 111 ms 19264 KB Output is correct
25 Correct 112 ms 21956 KB Output is correct
26 Correct 133 ms 22024 KB Output is correct
27 Correct 116 ms 21404 KB Output is correct
28 Correct 101 ms 13844 KB Output is correct
29 Correct 150 ms 22324 KB Output is correct
30 Correct 125 ms 22356 KB Output is correct
31 Correct 121 ms 27532 KB Output is correct
32 Correct 105 ms 14528 KB Output is correct
33 Correct 169 ms 19864 KB Output is correct
34 Correct 193 ms 23272 KB Output is correct
35 Correct 226 ms 23244 KB Output is correct