# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
900482 | Trisanu_Das | Spring cleaning (CEOI20_cleaning) | C++17 | 1064 ms | 15356 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>
using namespace std;
typedef long long ll;
vector<int> adj[100002];
int leaf[100002];
int dp[100002];
ll ans = 0;
void dfs(int x, int p){
dp[x] = leaf[x];
for(auto u : adj[x]){
if(u != p){
dfs(u, x);
dp[x] += dp[u];
}
}
if(x == 1){
if(adj[x].size() == 1 && leaf[x] == 0){
if(dp[x] == 1){
ans++;
return;
}
else { ans = -1LL; return; }
}
if(dp[x]%2) { ans = -1LL; return; }
else{
ans += dp[x];
return;
}
}
if(dp[x] == 0){
dp[x] = 1;
return;
}
if(dp[x]%2){
ans += dp[x];
dp[x] = 1;
return;
}
ans += dp[x];
dp[x] = 2;
}
int main()
{
int n, q;
scanf("%d %d", &n, &q);
for(int i=1;i<n;i++){
int s, e;
scanf("%d %d", &s, &e);
adj[s].push_back(e);
adj[e].push_back(s);
}
for(int i=1;i<=q;i++){
fill(leaf, leaf + 30002, 0);
int d;
scanf("%d", &d);
for(int i=1;i<=d;i++){
int l;
scanf("%d", &l);
leaf[l]++;
}
ans = 0;
dfs(1, -1);
printf("%lld\n", ans);
}
}
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... |