Submission #825451

# Submission time Handle Problem Language Result Execution time Memory
825451 2023-08-14T20:34:59 Z NK_ Spring cleaning (CEOI20_cleaning) C++17
100 / 100
176 ms 32588 KB
// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>

using namespace std;

#define nl '\n'
#define pb push_back 
#define mp make_pair
#define f first
#define s second
#define sz(x) int(x.size())

template<class T> using V = vector<T>;
using vi = V<int>;
using pi = pair<int, int>;
using ll = long long;
using vl = V<ll>;

const int LG = 20;
int main() {
	cin.tie(0)->sync_with_stdio(0);
	
	int N, Q; cin >> N >> Q;

	V<vi> adj(N); for(int i = 0; i < N - 1; i++) {
		int u, v; cin >> u >> v; --u, --v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	int R = -1; for(int i = 0; i < N; i++) if (sz(adj[i]) > 1) {
		R = i;
		break;
	}

	V<vi> C(2, vi(N)); vi L(N), st(N), en(N); int t = 0; 
	V<vi> up(N, vi(LG)); vi dep(N);

	auto jmp = [&](int u, int d) {
		for(int i = 0; i < LG; i++) if ((d >> i) & 1) u = up[u][i];
		return u;
	};

	auto lca = [&](int a, int b) {
		if (dep[a] < dep[b]) swap(a, b);
		a = jmp(a, dep[a] - dep[b]);
		// cerr << dep[a] << " " << dep[b] << endl;
		// cerr << a << " <-> " << b << endl;
		if (a == b) return a;

		for(int i = LG - 1; i >= 0; i--) {
			if (up[a][i] != up[b][i]) {
				a = up[a][i];
				b = up[b][i];
			}
		}

		return up[a][0];
	};

	function<void(int, int)> gen = [&](int u, int p) {
		up[u][0] = p; dep[u] = (u == p ? 0 : dep[p] + 1);
		for(int i = 1; i < LG; i++) up[u][i] = up[up[u][i-1]][i-1];

		st[u] = t++;
		for(auto& v : adj[u]) if (v != p) {
			gen(v, u); L[u] += L[v];
		}
		en[u] = t - 1;

		if (st[u] == en[u]) L[u] = 1;
	};

	gen(R, R);

	int ex = 0;
	function<void(int, int)> dfs = [&](int u, int p) {
		C[L[u] & 1][u]++;
		if (L[u] % 2 == 0) ex++;

		for(auto& v : adj[u]) if (v != p) {
			C[0][v] += C[0][u]; 
			C[1][v] += C[1][u]; 
			dfs(v, u); 
		}
	};

	dfs(R, -1);
	// cerr << R << endl;

	// assert(false);

	int leaf = L[R]; 
	// cerr << ex << endl;

	vi F(N, 0);
	for(int i = 0; i < Q; i++) {
		int K; cin >> K;
		vi D(K); for(auto& x : D) { cin >> x; --x; }

		// cerr << "ENTER" << endl;

		set<int> seen; vi S;
		for(auto x : D) {
			if (st[x] == en[x]) {
				if (seen.count(x)) { S.pb(x); F[x]++; }
				else seen.insert(x);
			} else {
				S.pb(x);
				F[x]++;
				// cerr << "ADD: " << x << endl;
			}
		}

		if ((leaf + sz(S)) & 1) {
			for(auto& x : S) F[x] = 0;
			cout << -1 << nl;
			continue;
		}
			
		int ans = ex;
		sort(begin(S), end(S), [&](int x, int y) { return st[x] < st[y]; });

		int M = sz(S);
		for(int x = 0; x < M - 1; x++) {
			int l = lca(S[x], S[x + 1]); S.pb(l);
			// cerr << S[x] << " " << S[x + 1] << " " << l << endl;
		}

		sort(begin(S), end(S), [&](int x, int y) { return st[x] < st[y]; });
		S.erase(unique(begin(S), end(S)), end(S)); 

		M = sz(S);
		vi stk = {-1}; V<vi> chd(M);
		int rt = -1;
		for(int u = 0; u < M; u++) {
			// cerr << "AT: " << S[u] << endl;
			while(stk.back() != -1 && en[S[stk.back()]] < st[S[u]]) stk.pop_back();
			
			// parent is stk.back()
			int p = stk.back();
			if (p != -1) chd[p].pb(u);
			else rt = u;
			// cerr << S[u] << " " << (p == -1 ? -1 : S[p]) << endl;
			stk.pb(u);
		}

		function<void(int, int)> upd = [&](int u, int p) {
			for(auto& v : chd[u]) {
				upd(v, u); 
				F[S[u]] += F[S[v]];
			}

			// cerr << S[u] << " - " << F[S[u]] << endl;

			if (F[S[u]] & 1) {
				// assert(p != -1);
				// int r = L[S[u]] & 1; 
				// cerr << r << endl;
				// cerr << -C[r][S[u]] << ", " << +C[r^1][S[u]] << endl;
				// cerr << -C[r^1][S[p]] << ", " << +C[r][S[p]] << endl;

				ans -= C[0][S[u]]; ans += C[1][S[u]]; 
				if (p != -1) { ans -= C[1][S[p]]; ans += C[0][S[p]]; }
			}
		};

		if (rt != -1) upd(rt, -1);

		for(auto& x : S) F[x] = 0;
		
		// cerr << ans << endl;
		// cerr << "EXIT" << endl;

		// cerr << N + K << endl;
		cout << (N + K) - 1 + ans - 1 << nl;
		// cerr << endl;
	}


    return 0;
}			

# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 37 ms 4628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 2536 KB Output is correct
2 Correct 16 ms 2400 KB Output is correct
3 Correct 64 ms 21624 KB Output is correct
4 Correct 87 ms 19492 KB Output is correct
5 Correct 93 ms 26044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 3148 KB Output is correct
2 Correct 7 ms 2640 KB Output is correct
3 Correct 76 ms 29432 KB Output is correct
4 Correct 100 ms 31140 KB Output is correct
5 Correct 51 ms 26612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 5124 KB Output is correct
2 Correct 19 ms 4924 KB Output is correct
3 Correct 10 ms 4564 KB Output is correct
4 Correct 12 ms 5196 KB Output is correct
5 Correct 13 ms 5340 KB Output is correct
6 Correct 27 ms 5592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 13676 KB Output is correct
2 Correct 68 ms 13652 KB Output is correct
3 Correct 49 ms 8000 KB Output is correct
4 Correct 78 ms 14956 KB Output is correct
5 Correct 77 ms 15016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 106 ms 20804 KB Output is correct
2 Correct 157 ms 26060 KB Output is correct
3 Correct 130 ms 26096 KB Output is correct
4 Correct 130 ms 25424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 37 ms 4628 KB Output is correct
3 Correct 23 ms 2536 KB Output is correct
4 Correct 16 ms 2400 KB Output is correct
5 Correct 64 ms 21624 KB Output is correct
6 Correct 87 ms 19492 KB Output is correct
7 Correct 93 ms 26044 KB Output is correct
8 Correct 24 ms 3148 KB Output is correct
9 Correct 7 ms 2640 KB Output is correct
10 Correct 76 ms 29432 KB Output is correct
11 Correct 100 ms 31140 KB Output is correct
12 Correct 51 ms 26612 KB Output is correct
13 Correct 37 ms 5124 KB Output is correct
14 Correct 19 ms 4924 KB Output is correct
15 Correct 10 ms 4564 KB Output is correct
16 Correct 12 ms 5196 KB Output is correct
17 Correct 13 ms 5340 KB Output is correct
18 Correct 27 ms 5592 KB Output is correct
19 Correct 55 ms 13676 KB Output is correct
20 Correct 68 ms 13652 KB Output is correct
21 Correct 49 ms 8000 KB Output is correct
22 Correct 78 ms 14956 KB Output is correct
23 Correct 77 ms 15016 KB Output is correct
24 Correct 106 ms 20804 KB Output is correct
25 Correct 157 ms 26060 KB Output is correct
26 Correct 130 ms 26096 KB Output is correct
27 Correct 130 ms 25424 KB Output is correct
28 Correct 77 ms 13904 KB Output is correct
29 Correct 158 ms 25948 KB Output is correct
30 Correct 94 ms 25196 KB Output is correct
31 Correct 88 ms 32588 KB Output is correct
32 Correct 75 ms 14884 KB Output is correct
33 Correct 122 ms 21136 KB Output is correct
34 Correct 151 ms 26156 KB Output is correct
35 Correct 176 ms 26004 KB Output is correct