Submission #977712

#TimeUsernameProblemLanguageResultExecution timeMemory
977712OAleksaSpring cleaning (CEOI20_cleaning)C++14
28 / 100
1055 ms16356 KiB
#include <bits/stdc++.h> #define f first #define s second using namespace std; const int N = 1e5 + 69; const int K = 17; int n, q, ch[N], par[N], leaf[N], is[N], dep[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; dep[u] = dep[v] + 1; 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]; int ans = 0; for (int i = 2;i <= n;i++) ans += (ch[i] % 2 == 0 ? 2 : 1); while (q--) { int t; cin >> t; int a[t]; int res = ans + t; for (int i = 0;i < t;i++) { cin >> a[i]; int v = a[i]; uk++; if (leaf[v]) { leaf[v]--; uk--; while (v != 1) { res -= (ch[v] % 2 == 0 ? 2 : 1); ch[v]--; res += (ch[v] % 2 == 0 ? 2 : 1); v = par[v]; } } v = a[i]; while (v != 1) { res -= (ch[v] % 2 == 0 ? 2 : 1); ch[v]++; res += (ch[v] % 2 == 0 ? 2 : 1); v = par[v]; } } if (uk % 2 == 1) cout << "-1\n"; else cout << res << '\n'; for (int i = 0;i < t;i++) { uk--; int v = a[i]; if (is[a[i]] && !leaf[a[i]]) { uk++; leaf[a[i]] = 1; while (v != 1) { res -= (ch[v] % 2 == 0 ? 2 : 1); ch[v]++; res += (ch[v] % 2 == 0 ? 2 : 1); v = par[v]; } } v = a[i]; while (v != 1) { res -= (ch[v] % 2 == 0 ? 2 : 1); ch[v]--; res += (ch[v] % 2 == 0 ? 2 : 1); v = par[v]; } } } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...