# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
477698 | InternetPerson10 | Spring cleaning (CEOI20_cleaning) | C++17 | 1086 ms | 16228 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
vector<vector<int>> adj;
vector<int> par, chi, le, ct;
int dfs(int n, int pa = -1) {
int childs = 0;
par[n] = pa;
for(int ch : adj[n]) {
if(ch == pa) continue;
childs += dfs(ch, n);
}
if(adj[n].size() == 1) {
le[n] = 1;
return chi[n] = 1;
}
return chi[n] = childs;
}
int br = 1;
int dfs2(int n) {
int bads = ct[n];
for(int ch : adj[n]) {
if(ch == par[n]) continue;
bads += dfs2(ch);
}
chi[n] += br * bads;
return bads;
}
int main() {
int n, q;
scanf("%d %d", &n, &q);
adj.resize(n);
par.resize(n);
chi.resize(n);
le.resize(n);
ct.resize(n);
for(int i = 1; i < n; i++) {
int x, y;
scanf("%d %d", &x, &y);
x--; y--;
adj[x].push_back(y);
adj[y].push_back(x);
}
int r = 0;
while(adj[r].size() == 1) r++;
dfs(r);
while(q--) {
int k;
scanf("%d", &k);
for(int i = 0; i < k; i++) {
int x;
scanf("%d", &x);
x--;
if(le[x] == 1) {
le[x] = -1;
continue;
}
ct[x]++;
}
int ans = n-1+k;
dfs2(r);
for(int i = 0; i < n; i++) {
if(i == r) continue;
if(chi[i]%2 == 0) ans++;
}
if(chi[r]%2) ans = -1;
br = -1;
dfs2(r);
br = 1;
printf("%d\n", ans);
for(int i = 0; i < n; i++) {
if(le[i] == -1) {
le[i] = 1;
continue;
}
ct[i] = 0;
}
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |