#include <bits/stdc++.h>
using namespace std;
int n,q,sz,root;
int cntEven;
int nrLeaf[100005];
int aux[100005];
int parV[100005];
vector<int> v[100005];
vector<int> upd;
void dfs(int p, int par)
{
parV[p] = par;
nrLeaf[p] = 0;
for(int it : v[p])
{
if(it == par)
continue;
dfs(it, p);
nrLeaf[p] += nrLeaf[it];
}
nrLeaf[p] += aux[p];
if(!nrLeaf[p])
nrLeaf[p] = 1;
if(nrLeaf[p] % 2 == 0)
cntEven++;
}
void solveBig(int k, vector<int> &upd)
{
for(int it : upd)
aux[it] ++;
cntEven=0;
dfs(root, 0);
if(nrLeaf[root] % 2)
cout<<"-1\n";
else
cout<<n+k-1 + cntEven-1<<'\n';
for(int i=1;i<=n;i++)
aux[i] = 0;
cntEven=0;
dfs(root, 0);
}
void add(int p)
{
nrLeaf[p]++;
if(nrLeaf[p] % 2)
cntEven++;
else
cntEven--;
}
void del(int p)
{
nrLeaf[p]--;
if(nrLeaf[p] % 2)
cntEven++;
else
cntEven--;
}
void solveSmall(int k, vector<int> &upd)
{
for(int it : upd)
add(it);
if(nrLeaf[root] % 2)
cout<<"-1\n";
else
cout<<n+k-1 + cntEven-1<<'\n';
for(int it : upd)
del(it);
}
int main()
{
cin>>n>>q;
sz = sqrt((int)1e5);
//sz=0;
for(int i=1;i<n;i++)
{
int x, y;
cin>>x>>y;
v[x].push_back(y);
v[y].push_back(x);
}
for(int i=1;i<=n;i++)
if((int)v[i].size() >= 2)
root = i;
cntEven=0;
dfs(root, 0);
while(q)
{
q--;
upd.clear();
int k;
cin>>k;
for(int i=1;i<=k;i++)
{
int x;
cin>>x;
upd.push_back(x);
}
if(k >= sz)
solveBig(k, upd);
else
solveSmall(k, upd);
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
3316 KB |
Output is correct |
2 |
Correct |
22 ms |
3388 KB |
Output is correct |
3 |
Correct |
63 ms |
7364 KB |
Output is correct |
4 |
Correct |
62 ms |
6432 KB |
Output is correct |
5 |
Correct |
84 ms |
7704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
3784 KB |
Output is correct |
2 |
Correct |
25 ms |
3932 KB |
Output is correct |
3 |
Correct |
126 ms |
14564 KB |
Output is correct |
4 |
Correct |
136 ms |
13936 KB |
Output is correct |
5 |
Incorrect |
71 ms |
13144 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
472 ms |
3924 KB |
Output is correct |
2 |
Incorrect |
24 ms |
3540 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
164 ms |
5800 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
293 ms |
7404 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |