Submission #977640

# Submission time Handle Problem Language Result Execution time Memory
977640 2024-05-08T08:10:55 Z OAleksa Spring cleaning (CEOI20_cleaning) C++14
34 / 100
1000 ms 31160 KB
#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
const int N = 4e5 + 69;
int n, q, ch[N], par[N], leaf[N], is[N];
vector<int> g[N];
void dfs(int v, int p) {
	par[v] = p;
	if (g[v].size() == 1)
		leaf[v] = ch[v] = is[v] = 1;
	for (auto u : g[v]) {
		if (u == p)
			continue;
		dfs(u, v);
		ch[v] += ch[u];
	}
}
signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> q;
	for (int i = 1;i <= n - 1;i++) {
		int a, b;
		cin >> a >> b;
		g[a].push_back(b);
		g[b].push_back(a);
	}
	dfs(1, 0);
	int uk = ch[1];
	while (q--) {
		int t;
		cin >> t;
		int a[t];
		for (int i = 0;i < t;i++) {
			cin >> a[i];
			uk++;
			if (leaf[a[i]]) {
				leaf[a[i]]--;
				uk--;
			}
			g[a[i]].push_back(n + i + 1);
			g[n + i + 1].push_back(a[i]);
		}
		function<void(int, int)> calc = [&](int v, int p) {
			if (g[v].size() == 1)
				ch[v] = 1;
			else
				ch[v] = 0;
			for (auto u : g[v]) {
				if (u == p)
					continue;
				calc(u, v);
				ch[v] += ch[u];
			}
		};
		calc(1, 0);
		if (uk % 2 == 1)
			cout << "-1\n";
		else {
			int ans = 0;
			for (int i = 2;i <= n + t;i++)
				ans += (ch[i] % 2 == 0 ? 2 : 1);
			cout << ans << '\n';
		}
		for (int i = 0;i < t;i++) {
			uk--;
			if (is[a[i]] && !leaf[a[i]]) {
				uk++;
				leaf[a[i]] = 1;
			}
			g[a[i]].pop_back();
			assert(g[n + i + 1].size() == 1);
			g[n + i + 1].pop_back();
		}
	}
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 14940 KB Output is correct
2 Execution timed out 1083 ms 16264 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 17 ms 18520 KB Output is correct
2 Correct 15 ms 18588 KB Output is correct
3 Correct 32 ms 19400 KB Output is correct
4 Correct 49 ms 21756 KB Output is correct
5 Correct 41 ms 23240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 19544 KB Output is correct
2 Correct 18 ms 19288 KB Output is correct
3 Correct 36 ms 28752 KB Output is correct
4 Correct 59 ms 31160 KB Output is correct
5 Correct 36 ms 27220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 196 ms 17232 KB Output is correct
2 Correct 123 ms 16188 KB Output is correct
3 Correct 178 ms 15844 KB Output is correct
4 Correct 218 ms 16472 KB Output is correct
5 Correct 184 ms 16728 KB Output is correct
6 Correct 230 ms 16636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1038 ms 17916 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1029 ms 19536 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 14940 KB Output is correct
2 Execution timed out 1083 ms 16264 KB Time limit exceeded
3 Halted 0 ms 0 KB -