#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 |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Execution timed out |
1069 ms |
3600 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
2904 KB |
Output is correct |
2 |
Correct |
5 ms |
2908 KB |
Output is correct |
3 |
Correct |
16 ms |
7124 KB |
Output is correct |
4 |
Correct |
18 ms |
5820 KB |
Output is correct |
5 |
Correct |
22 ms |
6876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
3160 KB |
Output is correct |
2 |
Correct |
6 ms |
3268 KB |
Output is correct |
3 |
Correct |
39 ms |
14172 KB |
Output is correct |
4 |
Correct |
38 ms |
13404 KB |
Output is correct |
5 |
Correct |
24 ms |
13380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
177 ms |
4076 KB |
Output is correct |
2 |
Correct |
114 ms |
3596 KB |
Output is correct |
3 |
Correct |
163 ms |
3596 KB |
Output is correct |
4 |
Correct |
202 ms |
3932 KB |
Output is correct |
5 |
Correct |
164 ms |
3932 KB |
Output is correct |
6 |
Correct |
202 ms |
4188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1022 ms |
5208 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1037 ms |
6740 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Execution timed out |
1069 ms |
3600 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |