#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);
res+=deg[v];
deg[u]+=deg[v];
}
if (deg[u]>2) deg[u]=((deg[u]%2)==1)?1:2;
//cout<<"here "<<u<<' '<<deg[u]<<' '<<res<<'\n';
}
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 |
2652 KB |
Output is correct |
2 |
Execution timed out |
1083 ms |
4228 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
3164 KB |
Output is correct |
2 |
Correct |
5 ms |
3164 KB |
Output is correct |
3 |
Correct |
17 ms |
7892 KB |
Output is correct |
4 |
Correct |
21 ms |
6868 KB |
Output is correct |
5 |
Correct |
23 ms |
8404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
3676 KB |
Output is correct |
2 |
Correct |
6 ms |
3932 KB |
Output is correct |
3 |
Correct |
28 ms |
15560 KB |
Output is correct |
4 |
Correct |
33 ms |
14676 KB |
Output is correct |
5 |
Correct |
25 ms |
14164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
181 ms |
4688 KB |
Output is correct |
2 |
Correct |
110 ms |
3932 KB |
Output is correct |
3 |
Correct |
188 ms |
3816 KB |
Output is correct |
4 |
Correct |
201 ms |
4184 KB |
Output is correct |
5 |
Correct |
167 ms |
4184 KB |
Output is correct |
6 |
Correct |
199 ms |
4656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1036 ms |
6492 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1027 ms |
8016 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Execution timed out |
1083 ms |
4228 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |