# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
477699 | InternetPerson10 | Spring cleaning (CEOI20_cleaning) | C++17 | 1094 ms | 7632 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, bad;
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 main() {
int n, q;
scanf("%d %d", &n, &q);
adj.resize(n);
par.resize(n);
chi.resize(n);
le.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);
bad.resize(k);
for(int i = 0; i < k; i++) {
int x;
scanf("%d", &x);
x--;
bad[i] = x;
while(x != -1) {
if(le[x] == 1) {
le[x] = -1;
break;
}
chi[x]++;
x = par[x];
}
}
int ans = n-1+bad.size();
for(int i = 0; i < n; i++) {
if(i == r) continue;
if(chi[i]%2 == 0) ans++;
}
if(chi[r]%2) ans = -1;
printf("%d\n", ans);
for(int g : bad) {
int x = g;
while(x != -1) {
if(le[x] == -1) {
le[x] = 1;
break;
}
chi[x]--;
x = par[x];
}
}
vector<int>().swap(bad);
}
}
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... |