#include <bits/stdc++.h>
using namespace std;
#define int ll
#define endl '\n' //comment for interactive
#define fast_io ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pb push_back
#define re resize
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define all1(x) (x).begin()+1, (x).end()
#define loop(i, n) for(int i = 0; i < n; i++)
#define loop1(i, n) for(int i = 1; i <= n; i++)
#define print(x) cout << #x << ": " << x << endl << flush
typedef long long ll;
typedef vector<int> vi;
typedef array<int, 2> ii;
typedef array<int, 3> ti;
typedef vector<ii> vii;
typedef vector<ti> vti;
typedef vector<vi> vvi;
typedef priority_queue<int> pq;
template<class T> bool ckmin(T&a, T b) { bool B = a > b; a = min(a, b); return B; }
template<class T> bool ckmax(T&a, T b) { bool B = a < b; a = max(a, b); return B; }
const int inf = 1e17;
const int maxn = 1e5+5;
vi adj[maxn];
int tin[maxn];
int tout[maxn];
int lv[maxn];
int par[maxn][21];
int edge[maxn][21];
vi virt_adj[maxn];
ii virt_edge[maxn];
int virt_lv[maxn];
int timer = 1;
int total = 0;
ii operator+(ii l, ii r) { return {l[0]+r[0],l[1]+r[1]}; }
ii& operator+=(ii& l, ii r) { l = l + r; return l; }
bool is_leaf(int u) {
return adj[u].size() == 1;
}
bool is_anc(int u, int v) {
return tin[u] <= tin[v] and tout[v] <= tout[u];
}
int lca(int u, int v) {
if(is_anc(u, v)) return u;
if(is_anc(v, u)) return v;
for(int j = 20; j >= 0; j--)
if(!is_anc(par[u][j], v))
u = par[u][j];
return par[u][0];
}
void predfs(int u, int p) {
tin[u] = timer++;
par[u][0] = p;
for(auto v : adj[u]) if(v != p) {
predfs(v, u);
lv[u] += lv[v];
}
if(is_leaf(u)) lv[u] += 1;
if(u != p) total += (edge[u][0] = !(lv[u] & 1));
tout[u] = timer;
}
bool comp(int a, int b) { return tin[a] < tin[b]; }
void solve() {
int n, q;
cin >> n >> q;
loop1(i, n-1) {
int u, v;
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
predfs(1, 1);
edge[1][0] = 0;
loop1(j, 20) loop1(i, n) {
par[i][j] = par[par[i][j-1]][j-1];
edge[i][j] = edge[par[i][j-1]][j-1] + edge[i][j-1];
}
vi new_lv(n+1);
while(q--) {
int d; cin >> d;
vi a(d);
loop(i, d) {
cin >> a[i];
new_lv[a[i]]++;
}
vi b = a;
b.pb(1);
sort(all(b), comp);
b.erase(unique(all(b)), b.end());
int cnt = b.size();
loop(i, cnt-1) {
b.pb(lca(b[i], b[i+1]));
}
sort(all(b), comp);
b.erase(unique(all(b)), b.end());
cnt = b.size();
stack<int> st;
st.push(b[0]);
loop1(i, cnt-1) {
while(!is_anc(st.top(), b[i]))
st.pop();
int u = st.top();
int v = b[i];
for(int j = 20; j >= 0; j--)
if(!is_anc(par[v][j], u)) {
virt_edge[b[i]] += (ii) {edge[v][j], 1 << j};
v = par[v][j];
}
if(!is_anc(v, u)) {
virt_edge[b[i]] += (ii) {edge[v][0], 1};
v = par[v][0];
}
virt_adj[u].pb(b[i]);
st.push(b[i]);
}
int delta = 0;
auto dfs = [&] (int u, auto dfs) -> void {
if(new_lv[u])
virt_lv[u] += new_lv[u] - is_leaf(u);
for(auto v : virt_adj[u]) {
dfs(v, dfs);
virt_lv[u] += virt_lv[v];
}
if((virt_lv[u] & 1) and u != b[0]) {
auto [x, y] = virt_edge[u];
delta += y - 2*x;
}
};
dfs(b[0], dfs);
if((lv[1] + virt_lv[b[0]]) & 1) cout << -1 << endl;
else cout << (n+d-1) + total + delta << endl;
loop(i, cnt) {
virt_adj[b[i]].clear();
virt_edge[b[i]] = {0, 0};
virt_lv[b[i]] = 0;
}
loop(i, d)
new_lv[a[i]]--;
}
}
signed main() {
fast_io;
int t = 1; //cin >> t;
while(t--)
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
93 ms |
14232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
8404 KB |
Output is correct |
2 |
Correct |
20 ms |
8368 KB |
Output is correct |
3 |
Correct |
87 ms |
47776 KB |
Output is correct |
4 |
Correct |
103 ms |
37272 KB |
Output is correct |
5 |
Correct |
149 ms |
50296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
9396 KB |
Output is correct |
2 |
Correct |
22 ms |
9384 KB |
Output is correct |
3 |
Correct |
120 ms |
52964 KB |
Output is correct |
4 |
Correct |
150 ms |
52448 KB |
Output is correct |
5 |
Correct |
100 ms |
46976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
58 ms |
14668 KB |
Output is correct |
2 |
Correct |
39 ms |
13908 KB |
Output is correct |
3 |
Correct |
15 ms |
13624 KB |
Output is correct |
4 |
Correct |
17 ms |
14008 KB |
Output is correct |
5 |
Correct |
19 ms |
14404 KB |
Output is correct |
6 |
Correct |
56 ms |
14684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
124 ms |
33804 KB |
Output is correct |
2 |
Correct |
187 ms |
33672 KB |
Output is correct |
3 |
Correct |
112 ms |
19888 KB |
Output is correct |
4 |
Correct |
184 ms |
33836 KB |
Output is correct |
5 |
Correct |
157 ms |
33820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
193 ms |
48076 KB |
Output is correct |
2 |
Correct |
234 ms |
52300 KB |
Output is correct |
3 |
Correct |
207 ms |
50956 KB |
Output is correct |
4 |
Correct |
231 ms |
51760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
93 ms |
14232 KB |
Output is correct |
3 |
Correct |
20 ms |
8404 KB |
Output is correct |
4 |
Correct |
20 ms |
8368 KB |
Output is correct |
5 |
Correct |
87 ms |
47776 KB |
Output is correct |
6 |
Correct |
103 ms |
37272 KB |
Output is correct |
7 |
Correct |
149 ms |
50296 KB |
Output is correct |
8 |
Correct |
21 ms |
9396 KB |
Output is correct |
9 |
Correct |
22 ms |
9384 KB |
Output is correct |
10 |
Correct |
120 ms |
52964 KB |
Output is correct |
11 |
Correct |
150 ms |
52448 KB |
Output is correct |
12 |
Correct |
100 ms |
46976 KB |
Output is correct |
13 |
Correct |
58 ms |
14668 KB |
Output is correct |
14 |
Correct |
39 ms |
13908 KB |
Output is correct |
15 |
Correct |
15 ms |
13624 KB |
Output is correct |
16 |
Correct |
17 ms |
14008 KB |
Output is correct |
17 |
Correct |
19 ms |
14404 KB |
Output is correct |
18 |
Correct |
56 ms |
14684 KB |
Output is correct |
19 |
Correct |
124 ms |
33804 KB |
Output is correct |
20 |
Correct |
187 ms |
33672 KB |
Output is correct |
21 |
Correct |
112 ms |
19888 KB |
Output is correct |
22 |
Correct |
184 ms |
33836 KB |
Output is correct |
23 |
Correct |
157 ms |
33820 KB |
Output is correct |
24 |
Correct |
193 ms |
48076 KB |
Output is correct |
25 |
Correct |
234 ms |
52300 KB |
Output is correct |
26 |
Correct |
207 ms |
50956 KB |
Output is correct |
27 |
Correct |
231 ms |
51760 KB |
Output is correct |
28 |
Correct |
150 ms |
32164 KB |
Output is correct |
29 |
Correct |
194 ms |
51728 KB |
Output is correct |
30 |
Correct |
144 ms |
51104 KB |
Output is correct |
31 |
Correct |
136 ms |
53516 KB |
Output is correct |
32 |
Correct |
183 ms |
34636 KB |
Output is correct |
33 |
Correct |
234 ms |
43204 KB |
Output is correct |
34 |
Correct |
280 ms |
52228 KB |
Output is correct |
35 |
Correct |
286 ms |
52304 KB |
Output is correct |