# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
866020 | 2023-10-25T09:53:03 Z | boris_mihov | Spring cleaning (CEOI20_cleaning) | C++17 | 1000 ms | 17444 KB |
#include <algorithm> #include <iostream> #include <numeric> #include <cassert> #include <vector> #include <queue> #include <stack> typedef long long llong; const int MAXN = 200000 + 10; const int MOD = 1e9 + 7; const int INF = 2e9; int n, q; int cntEven; int res[MAXN]; int par[MAXN]; bool was[MAXN]; bool isLeaf[MAXN]; std::vector <int> g[MAXN]; int dfs(int node, int p) { par[node] = p; if (g[node].size() == 1) { isLeaf[node] = true; res[node] = 1; return 1; } res[node] = 0; for (const int &u : g[node]) { if (u == p) { continue; } res[node] ^= dfs(u, node); } cntEven += !res[node]; return res[node]; } void climb(int node) { cntEven -= !res[node]; res[node] ^= 1; cntEven += !res[node]; if (par[node] != 0) { climb(par[node]); } } int a[MAXN]; void solve() { int root = 1; if (g[1].size() == 1) root = g[1][0]; dfs(root, 0); for (int i = 1 ; i <= q ; ++i) { int d; int cnt = n; std::cin >> d; for (int j = 1 ; j <= d ; ++j) { std::cin >> a[j]; if (isLeaf[a[j]] && !was[a[j]]) was[a[j]] = true; else climb(a[j]); } if (res[root] == 1) std::cout << -1 << '\n'; else std::cout << n + d + cntEven - 2 << '\n'; for (int j = 1 ; j <= d ; ++j) { climb(a[j]); if (isLeaf[a[j]]) was[a[j]] = false; } } } void input() { std::cin >> n >> q; for (int i = 2 ; i <= n ; ++i) { int u, v; std::cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } } void fastIOI() { std::ios_base :: sync_with_stdio(0); std::cout.tie(nullptr); std::cin.tie(nullptr); } int main() { fastIOI(); input(); solve(); return 0; }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 6616 KB | Output is correct |
2 | Incorrect | 23 ms | 7916 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 6 ms | 7256 KB | Output is correct |
2 | Correct | 7 ms | 7308 KB | Output is correct |
3 | Correct | 18 ms | 11220 KB | Output is correct |
4 | Correct | 20 ms | 10448 KB | Output is correct |
5 | Correct | 28 ms | 11848 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 926 ms | 7852 KB | Output is correct |
2 | Correct | 924 ms | 7504 KB | Output is correct |
3 | Correct | 481 ms | 17444 KB | Output is correct |
4 | Execution timed out | 1039 ms | 16728 KB | Time limit exceeded |
5 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Execution timed out | 1062 ms | 8284 KB | Time limit exceeded |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Incorrect | 31 ms | 10428 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Incorrect | 59 ms | 12632 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 6616 KB | Output is correct |
2 | Incorrect | 23 ms | 7916 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |