#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// Ofast, O0, O1, O2, O3, unroll-loops, fast-math, trapv
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define Mp make_pair
#define sep ' '
#define endl '\n'
#define F first
#define S second
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define kill(res) cout << res << '\n', exit(0);
#define set_dec(x) cout << fixed << setprecision(x);
#define fast_io ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("input.txt", "r", stdin) ; freopen("output.txt", "w", stdout);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll L = 20;
const ll N = 1e5 + 50;
const ll Mod = 1e9 + 7;
ll n, q, r, sz[N], cnt[N], h[N], sum[N], par[N][L], st[N], timer, upd[N], head[N];
vector<int> adj[N];
void dfs(int v, int p = 0){
par[v][0] = p;
for(int i = 1; i < L; i++){
if(!par[v][i-1]) break;
par[v][i] = par[par[v][i-1]][i-1];
}
sz[v] = 1;
st[v] = ++timer;
for(int u: adj[v]){
if(u == p) continue;
h[u] = h[v] + 1; dfs(u, v);
sz[v] += sz[u]; cnt[v] += cnt[u];
}
if(sz[v] == 1) cnt[v]++;
}
int getPar(int v, int k){
for(int i = 0; i < L; i++){
if(k >> i & 1) v = par[v][i];
}
return v;
}
int lca(int u, int v){
if(h[v] < h[u]) swap(u, v);
v = getPar(v, h[v] - h[u]);
if(u == v) return v;
for(int i = L-1; i >= 0; i--){
if(par[v][i] != par[u][i]){
v = par[v][i]; u = par[u][i];
}
}
return par[v][0];
}
void pre(int v, int p = 0){
sum[v] = sum[p] + (cnt[v] % 2);
for(int u: adj[v]){
if(u != p) pre(u, v);
}
}
bool cmp(int i, int j){
return st[i] < st[j];
}
int main(){
fast_io;
cin >> n >> q;
int u, v;
for(int i = 1; i < n; i++){
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
r = 1;
while(adj[r].size() == 1) r++;
dfs(r); pre(r);
ll ans = 2 * (n-1);
for(int i = 1; i <= n; i++) if(i != r) ans -= (cnt[i] % 2);
//cout << ans << endl;
while(q--){
int d, x; cin >> d;
vector<int> vec, vert;
for(int i = 1; i <= d; i++){
cin >> x; vec.pb(x); upd[x]++;
}
sort(all(vec), cmp);
vec.resize(unique(all(vec)) - vec.begin());
for(int i: vec) if(adj[i].size() == 1) upd[i]--;
set<pii> ms;
for(int i: vec) ms.insert(Mp(st[i], i));
while(ms.size() > 1){
auto lst = ms.end();
auto lst1 = lst; lst1--;
auto lst2 = lst1; lst2--;
int ulv = lca(lst1 -> S, lst2 -> S);
head[lst1 -> S] = ulv;
vert.pb(lst1 -> S);
ms.erase(lst1);
ms.insert(Mp(st[ulv], ulv));
}
vert.pb(ms.begin() -> S);
if(vert.back() != r) head[vert.back()] = r;
for(int i: vert){
if(head[i]) upd[head[i]] += upd[i];
}
ll res = ans;
for(int i: vert){
//cout << i << sep << head[i] << sep << upd[i] << endl;
if(!head[i]) continue;
if(upd[i] % 2){
res += sum[i] - sum[head[i]];
res -= h[i] - h[head[i]] - (sum[i] - sum[head[i]]);
}
}
if((cnt[r] + upd[r]) % 2) cout << -1 << endl;
else cout << res + d << endl;
for(int i: vert){
head[i] = 0; upd[i] = 0;
}
upd[r] = 0;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8796 KB |
Output is correct |
2 |
Correct |
44 ms |
12636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
9936 KB |
Output is correct |
2 |
Correct |
13 ms |
9944 KB |
Output is correct |
3 |
Correct |
27 ms |
28372 KB |
Output is correct |
4 |
Correct |
50 ms |
26056 KB |
Output is correct |
5 |
Correct |
63 ms |
31668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
10404 KB |
Output is correct |
2 |
Correct |
15 ms |
10464 KB |
Output is correct |
3 |
Correct |
45 ms |
37436 KB |
Output is correct |
4 |
Correct |
77 ms |
39624 KB |
Output is correct |
5 |
Correct |
45 ms |
36128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
13396 KB |
Output is correct |
2 |
Correct |
19 ms |
12380 KB |
Output is correct |
3 |
Correct |
7 ms |
12040 KB |
Output is correct |
4 |
Correct |
10 ms |
12636 KB |
Output is correct |
5 |
Correct |
10 ms |
12892 KB |
Output is correct |
6 |
Correct |
36 ms |
12892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
23432 KB |
Output is correct |
2 |
Correct |
92 ms |
23492 KB |
Output is correct |
3 |
Correct |
61 ms |
15528 KB |
Output is correct |
4 |
Correct |
90 ms |
23476 KB |
Output is correct |
5 |
Correct |
86 ms |
23408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
92 ms |
29376 KB |
Output is correct |
2 |
Correct |
115 ms |
32860 KB |
Output is correct |
3 |
Correct |
108 ms |
33064 KB |
Output is correct |
4 |
Correct |
104 ms |
32124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8796 KB |
Output is correct |
2 |
Correct |
44 ms |
12636 KB |
Output is correct |
3 |
Correct |
13 ms |
9936 KB |
Output is correct |
4 |
Correct |
13 ms |
9944 KB |
Output is correct |
5 |
Correct |
27 ms |
28372 KB |
Output is correct |
6 |
Correct |
50 ms |
26056 KB |
Output is correct |
7 |
Correct |
63 ms |
31668 KB |
Output is correct |
8 |
Correct |
15 ms |
10404 KB |
Output is correct |
9 |
Correct |
15 ms |
10464 KB |
Output is correct |
10 |
Correct |
45 ms |
37436 KB |
Output is correct |
11 |
Correct |
77 ms |
39624 KB |
Output is correct |
12 |
Correct |
45 ms |
36128 KB |
Output is correct |
13 |
Correct |
33 ms |
13396 KB |
Output is correct |
14 |
Correct |
19 ms |
12380 KB |
Output is correct |
15 |
Correct |
7 ms |
12040 KB |
Output is correct |
16 |
Correct |
10 ms |
12636 KB |
Output is correct |
17 |
Correct |
10 ms |
12892 KB |
Output is correct |
18 |
Correct |
36 ms |
12892 KB |
Output is correct |
19 |
Correct |
43 ms |
23432 KB |
Output is correct |
20 |
Correct |
92 ms |
23492 KB |
Output is correct |
21 |
Correct |
61 ms |
15528 KB |
Output is correct |
22 |
Correct |
90 ms |
23476 KB |
Output is correct |
23 |
Correct |
86 ms |
23408 KB |
Output is correct |
24 |
Correct |
92 ms |
29376 KB |
Output is correct |
25 |
Correct |
115 ms |
32860 KB |
Output is correct |
26 |
Correct |
108 ms |
33064 KB |
Output is correct |
27 |
Correct |
104 ms |
32124 KB |
Output is correct |
28 |
Correct |
72 ms |
21060 KB |
Output is correct |
29 |
Correct |
125 ms |
32548 KB |
Output is correct |
30 |
Correct |
69 ms |
31916 KB |
Output is correct |
31 |
Correct |
89 ms |
39508 KB |
Output is correct |
32 |
Correct |
82 ms |
23276 KB |
Output is correct |
33 |
Correct |
108 ms |
29268 KB |
Output is correct |
34 |
Correct |
145 ms |
32844 KB |
Output is correct |
35 |
Correct |
162 ms |
32800 KB |
Output is correct |