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;
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 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... |