#include <bits/stdc++.h>
typedef long long ll;
#define FOR(i,x,y) for(ll i=x; i<y; i++)
#define FORNEG(i,x,y) for(ll i=x; i>y; i--)
using namespace std;
vector<ll> edges[200005];
ll realans = 0;
ll vals[200005];
ll origvals[200005];
ll par[200005];
int dp(ll a, ll p){
par[a] = p;
ll ans = 0;
if (edges[a].size() <= 1) ans = 1;
for (auto&i : edges[a]){
if (i != p) ans += dp(i, a);
}
if (p!=-1) realans += 1 + (ans+1)%2;
if (p != -1) vals[a] = 1 + (ans+1)%2;
origvals[a] = 1 + (ans+1)%2;
return 1 + (ans+1)%2;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n,q;
cin >> n >> q;
FOR(i,0,n-1){
ll a,b;
cin >> a >> b;
edges[a].push_back(b);
edges[b].push_back(a);
}
ll root = -1;
ll cnt = 0;
FOR(j,1,n+1){
if (edges[j].size() != 1){
root = j;
}else{
cnt++;
}
}
dp(root, -1);
ll ans = 0;
FOR(i,1,n+1) ans += vals[i];
FOR(i,0,q){
ll d; cin >> d;
ll cur = n+1;
ll curans = 0;
set<ll> used;
ll newleaves=0;
FOR(k,0,d){
ll temp; cin >> temp;
if (edges[temp].size() != 1){
newleaves++;
curans+= 1;
ll idk = temp;
while (idk != root){
used.insert(idk);
if (vals[idk]==1) curans += 1, vals[idk]=2;
else curans -= 1, vals[idk] = 1;
idk = par[idk];
}
}else{
curans += 1;
}
edges[temp].push_back(cur++);
}
for (auto&k : used){
vals[k] = origvals[k];
}
if ((cnt+newleaves)%2==1) cout << -1 << "\n";
else{
cout << ans+curans << "\n";
}
FOR(i,1,n+1){
while (edges[i].size() && edges[i][edges[i].size()-1]>n){
edges[i].pop_back();
}
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
9564 KB |
Output is correct |
2 |
Correct |
180 ms |
10968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
10844 KB |
Output is correct |
2 |
Correct |
11 ms |
10844 KB |
Output is correct |
3 |
Correct |
15 ms |
13776 KB |
Output is correct |
4 |
Correct |
26 ms |
14532 KB |
Output is correct |
5 |
Correct |
30 ms |
15560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1073 ms |
10556 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1054 ms |
11444 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1060 ms |
12684 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1010 ms |
13648 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
9564 KB |
Output is correct |
2 |
Correct |
180 ms |
10968 KB |
Output is correct |
3 |
Correct |
12 ms |
10844 KB |
Output is correct |
4 |
Correct |
11 ms |
10844 KB |
Output is correct |
5 |
Correct |
15 ms |
13776 KB |
Output is correct |
6 |
Correct |
26 ms |
14532 KB |
Output is correct |
7 |
Correct |
30 ms |
15560 KB |
Output is correct |
8 |
Execution timed out |
1073 ms |
10556 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |