Submission #927498

#TimeUsernameProblemLanguageResultExecution timeMemory
92749812345678Spring cleaning (CEOI20_cleaning)C++17
34 / 100
1069 ms14172 KiB
#include <bits/stdc++.h> using namespace std; const int nx=1e5+5; int n, q, deg[nx], u, v, t, res, rt, cnt[nx]; vector<int> d[nx]; void dfs(int u, int p) { if (d[u].size()==1&&cnt[u]<1) deg[u]++; for (auto v:d[u]) { if (v==p) continue; dfs(v, u); deg[u]+=deg[v]; } if (deg[u]>2) deg[u]=((deg[u]%2)==1)?1:2; if (u!=rt) res+=deg[u]; } int main() { cin.tie(NULL)->sync_with_stdio(false); cin>>n>>q; for (int i=1; i<n; i++) cin>>u>>v, d[u].push_back(v), d[v].push_back(u); for (int i=1; i<=n; i++) if (d[i].size()>1) rt=i; while (q--) { for (int i=1; i<=n; i++) deg[i]=cnt[i]=0; cin>>t; for (int i=0; i<t; i++) cin>>u, deg[u]++, cnt[u]++; res=t; dfs(rt, rt); if ((deg[rt]%2)==0) cout<<res<<'\n'; else cout<<-1<<'\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...