#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;
used.insert(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 (auto&i : used){
while (edges[i].size() && edges[i][edges[i].size()-1]>n){
edges[i].pop_back();
}
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
9564 KB |
Output is correct |
2 |
Correct |
64 ms |
11204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
10840 KB |
Output is correct |
2 |
Correct |
12 ms |
10844 KB |
Output is correct |
3 |
Correct |
15 ms |
13836 KB |
Output is correct |
4 |
Correct |
35 ms |
15888 KB |
Output is correct |
5 |
Correct |
39 ms |
17352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1052 ms |
10624 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1063 ms |
11356 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
12884 KB |
Output is correct |
2 |
Correct |
105 ms |
13088 KB |
Output is correct |
3 |
Correct |
80 ms |
11664 KB |
Output is correct |
4 |
Correct |
122 ms |
13040 KB |
Output is correct |
5 |
Correct |
106 ms |
12888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
96 ms |
15116 KB |
Output is correct |
2 |
Execution timed out |
1020 ms |
16216 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
9564 KB |
Output is correct |
2 |
Correct |
64 ms |
11204 KB |
Output is correct |
3 |
Correct |
13 ms |
10840 KB |
Output is correct |
4 |
Correct |
12 ms |
10844 KB |
Output is correct |
5 |
Correct |
15 ms |
13836 KB |
Output is correct |
6 |
Correct |
35 ms |
15888 KB |
Output is correct |
7 |
Correct |
39 ms |
17352 KB |
Output is correct |
8 |
Execution timed out |
1052 ms |
10624 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |