Submission #430126

#TimeUsernameProblemLanguageResultExecution timeMemory
430126OzySpring cleaning (CEOI20_cleaning)C++17
0 / 100
1084 ms8804 KiB
#include <iostream> #include <bits/stdc++.h> using namespace std; #define lli long long int #define rep(i,a,b) for (lli i = (a); i <= (b); i++) #define repa(i,a,b) for (lli i = (a); i >= (b); i--) #define debug(a) cout << #a << " = " << a << endl #define debugsl(a) cout << #a << " = " << a << ", " #define MAX 100000 lli n,a,b,q,d,res,total,raiz; vector<lli> hijos[MAX+2]; lli val[MAX+2],borr[MAX+2],padres[MAX+2]; lli DFS(lli pos) { if (hijos[pos].size() == 1) return 1; for (auto h : hijos[pos]) { if (h == padres[pos]) continue; padres[h] = pos; val[pos] += DFS(h); } res = !(val[pos] % 2) ? res + 1 : res; return val[pos]; } void agrega(lli pos) { while (pos > 0) { borr[pos]++; if (borr[pos] == 1) break; if (borr[pos]%2) ++res; else --res; pos = padres[pos]; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> q; rep(i,1,n-1) { cin >> a >> b; hijos[a].push_back(b); hijos[b].push_back(a); if (raiz != 0) continue; if (hijos[a].size() > 1) raiz = a; if (hijos[b].size() > 1) raiz = b; } res = 0; a = DFS(raiz); res += n - 1; if (!(val[raiz] % 2)) --res; rep(Q,1,q) { rep(i,1,n) borr[i] = val[i]; total = res; cin >> d; rep(i,1,d) { cin >> a; total++; agrega(a); } if (borr[raiz] % 2) cout << -1 << "\n"; else cout << total << "\n"; } }
#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...