Submission #977402

# Submission time Handle Problem Language Result Execution time Memory
977402 2024-05-07T21:27:08 Z OAleksa Spring cleaning (CEOI20_cleaning) C++14
0 / 100
1000 ms 19484 KB
#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
const int N = 2e5 + 69;
int n, q, ch[N], par[N], lf[N], is[N];
vector<int> g[N];
void dfs(int v, int p) {
	par[v] = p;
	if (g[v].size() == 1)
		ch[v] = lf[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 += 1;
			if (lf[a[i]]) {
				uk -= 1;
				lf[a[i]] = 0;
			}
			int v = a[i];
			while (v != 0) {
				ch[v]++;
				v = par[v];
			}
		}
		if (uk % 2 == 1)
			cout << "-1\n";
		else {
			int ans = t;
			for (int i = 2;i <= n;i++) 
				ans += (ch[i] % 2 == 0 ? 2 : 1);
			cout << ans << '\n';
		}
		for (int i = 0;i < t;i++) {
			int v = a[i];
			while (v != 0) {
				ch[v]--;
				v = par[v];
			}
			uk -= 1;
			if (is[a[i]] && !lf[a[i]]) {
				lf[a[i]] = 1;
				uk += 1;
			}
		}
	}
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6748 KB Output is correct
2 Incorrect 81 ms 8144 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 7 ms 7512 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 747 ms 7768 KB Output is correct
2 Correct 753 ms 8020 KB Output is correct
3 Correct 395 ms 19484 KB Output is correct
4 Execution timed out 1033 ms 18520 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1004 ms 8600 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1057 ms 10688 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1036 ms 12432 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6748 KB Output is correct
2 Incorrect 81 ms 8144 KB Output isn't correct
3 Halted 0 ms 0 KB -