#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
// #define int long long
typedef pair<ll, ll> pll;
#define fastio ios::sync_with_stdio(false), cin.tie(0)
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2")
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define REP1(i, n) FOR(i, 0, n + 1)
#define ALL(x) (x).begin(), (x).end()
#define pb push_back
#define pf push_front
#define eb emplace_back
#define f first
#define s second
#define lowbit(x) x&-x
#define ckmin(a, b) a = min(a, b)
#define ckmax(a, b) a = max(a, b)
const int N = 1e9 + 7;
const int INF = 1e9 + 7;
const int maxn = 1e5 + 5;
vector<int> adj[maxn];
int dep[maxn], par[maxn], head[maxn], siz[maxn], loc[maxn], id[maxn], dfn = 0;
int leaf[maxn], isleaf[maxn], used[maxn];
int n, q, rt, lfcnt = 0;
int ans = 0;
void dfs(int pos, int prev){
if(prev != -1) adj[pos].erase(find(adj[pos].begin(), adj[pos].end(), prev));
siz[pos] = 1;
for(auto &x : adj[pos]){
par[x] = pos;
dep[x] = dep[pos] + 1;
dfs(x, pos);
siz[pos] += siz[x];
leaf[pos] += leaf[x];
if(siz[x] > siz[adj[pos][0]]) swap(x, adj[pos][0]);
}
if(!adj[pos].size()) isleaf[pos] = leaf[pos] = 1, lfcnt++;
}
void decompose(int pos, int h){
id[dfn] = pos, loc[pos] = dfn++;
head[pos] = h;
for(auto x : adj[pos]){
if(x == adj[pos][0]) decompose(x, h);
else decompose(x, x);
}
}
int st[maxn * 4], tag[maxn * 4];
void build(int v = 1, int l = 0, int r = n - 1){
if(l == r){
st[v] = 1 + (leaf[id[l]] - 1) % 2;
return;
}
int m = (l + r) / 2;
build(v * 2, l, m);
build(v * 2 + 1, m + 1, r);
st[v] = st[v * 2] + st[v * 2 + 1];
}
void flip(int v, int l, int r){
tag[v] ^= 1;
st[v] = 3 * (r - l + 1) - st[v];
}
void push(int v, int l, int r){
if(tag[v]){
int m = (l + r) / 2;
flip(v * 2, l, m);
flip(v * 2 + 1, m + 1, r);
tag[v] = 0;
}
}
void apply(int l, int r, int v = 1, int L = 0, int R = n - 1){
if(l > R || r < L) return;
if(l <= L && r >= R){
flip(v, L, R);
return;
}
push(v, L, R);
int m = (L + R) / 2;
apply(l, r, v * 2, L, m);
apply(l, r, v * 2 + 1, m + 1, R);
st[v] = st[v * 2] + st[v * 2 + 1];
}
void op(int pos){
while(true){
apply(loc[head[pos]], loc[pos]);
if(head[pos] == rt) break;
pos = par[head[pos]];
}
}
void init(){
dfs(rt, -1);
decompose(rt, rt);
build();
}
int main(void){
fastio;
cin>>n>>q;
for(int i = 0; i < n - 1; i++){
int a, b;
cin>>a>>b;
a--, b--;
adj[a].pb(b);
adj[b].pb(a);
}
for(int i = 0; i < n; i++){
if(adj[i].size() > 1){
rt = i;
par[i] = i;
break;
}
}
init();
while(q--){
int d;
cin>>d;
vector<int> a(d);
for(int i = 0; i < d; i++){
cin>>a[i];
a[i] --;
if(isleaf[a[i]] && !used[a[i]]) used[a[i]] = i + 1, lfcnt--;
else op(a[i]);
}
if((lfcnt + d) % 2) cout<<-1<<"\n";
else cout<<st[1] + d - 2<<"\n";
for(int i = 0; i < d; i++){
if(isleaf[a[i]] && used[a[i]] == i + 1) used[a[i]] = 0, lfcnt++;
else op(a[i]);
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
9048 KB |
Output is correct |
2 |
Correct |
68 ms |
10072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
9560 KB |
Output is correct |
2 |
Correct |
39 ms |
9564 KB |
Output is correct |
3 |
Correct |
23 ms |
12756 KB |
Output is correct |
4 |
Correct |
45 ms |
11984 KB |
Output is correct |
5 |
Correct |
46 ms |
13008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
11352 KB |
Output is correct |
2 |
Correct |
32 ms |
11096 KB |
Output is correct |
3 |
Correct |
52 ms |
40272 KB |
Output is correct |
4 |
Correct |
94 ms |
37968 KB |
Output is correct |
5 |
Correct |
47 ms |
37456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
13072 KB |
Output is correct |
2 |
Correct |
26 ms |
10332 KB |
Output is correct |
3 |
Correct |
7 ms |
10516 KB |
Output is correct |
4 |
Correct |
9 ms |
12124 KB |
Output is correct |
5 |
Correct |
12 ms |
12892 KB |
Output is correct |
6 |
Correct |
40 ms |
12436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
89 ms |
11488 KB |
Output is correct |
2 |
Correct |
143 ms |
11600 KB |
Output is correct |
3 |
Correct |
115 ms |
10460 KB |
Output is correct |
4 |
Correct |
144 ms |
11716 KB |
Output is correct |
5 |
Correct |
138 ms |
11488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
120 ms |
13028 KB |
Output is correct |
2 |
Correct |
85 ms |
23572 KB |
Output is correct |
3 |
Correct |
108 ms |
25112 KB |
Output is correct |
4 |
Correct |
113 ms |
23336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
9048 KB |
Output is correct |
2 |
Correct |
68 ms |
10072 KB |
Output is correct |
3 |
Correct |
38 ms |
9560 KB |
Output is correct |
4 |
Correct |
39 ms |
9564 KB |
Output is correct |
5 |
Correct |
23 ms |
12756 KB |
Output is correct |
6 |
Correct |
45 ms |
11984 KB |
Output is correct |
7 |
Correct |
46 ms |
13008 KB |
Output is correct |
8 |
Correct |
32 ms |
11352 KB |
Output is correct |
9 |
Correct |
32 ms |
11096 KB |
Output is correct |
10 |
Correct |
52 ms |
40272 KB |
Output is correct |
11 |
Correct |
94 ms |
37968 KB |
Output is correct |
12 |
Correct |
47 ms |
37456 KB |
Output is correct |
13 |
Correct |
60 ms |
13072 KB |
Output is correct |
14 |
Correct |
26 ms |
10332 KB |
Output is correct |
15 |
Correct |
7 ms |
10516 KB |
Output is correct |
16 |
Correct |
9 ms |
12124 KB |
Output is correct |
17 |
Correct |
12 ms |
12892 KB |
Output is correct |
18 |
Correct |
40 ms |
12436 KB |
Output is correct |
19 |
Correct |
89 ms |
11488 KB |
Output is correct |
20 |
Correct |
143 ms |
11600 KB |
Output is correct |
21 |
Correct |
115 ms |
10460 KB |
Output is correct |
22 |
Correct |
144 ms |
11716 KB |
Output is correct |
23 |
Correct |
138 ms |
11488 KB |
Output is correct |
24 |
Correct |
120 ms |
13028 KB |
Output is correct |
25 |
Correct |
85 ms |
23572 KB |
Output is correct |
26 |
Correct |
108 ms |
25112 KB |
Output is correct |
27 |
Correct |
113 ms |
23336 KB |
Output is correct |
28 |
Correct |
95 ms |
12536 KB |
Output is correct |
29 |
Correct |
96 ms |
25944 KB |
Output is correct |
30 |
Correct |
61 ms |
14540 KB |
Output is correct |
31 |
Correct |
88 ms |
39496 KB |
Output is correct |
32 |
Correct |
143 ms |
12720 KB |
Output is correct |
33 |
Correct |
94 ms |
22356 KB |
Output is correct |
34 |
Correct |
122 ms |
25696 KB |
Output is correct |
35 |
Correct |
114 ms |
25376 KB |
Output is correct |