#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x,y) ((x+y-1)/(y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
#define rev(i,s,e) for(int i = s; i >= e; --i)
#define trav(i,a) for(auto &i : a)
template<typename T>
void amin(T &a, T b) {
a = min(a,b);
}
template<typename T>
void amax(T &a, T b) {
a = max(a,b);
}
#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif
/*
*/
const int MOD = 1e9 + 7;
const int N = 2e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;
vector<ll> adj[N];
struct lca_algo {
// LCA template (for graphs with 1-based indexing)
int LOG = 1;
vector<int> depth;
vector<vector<int>> up;
vector<int> tin, tout;
int timer = 1;
lca_algo() {
}
lca_algo(int n, int r) {
lca_init(n,r);
}
void lca_init(int n, int r) {
while ((1 << LOG) < n) LOG++;
up = vector<vector<int>>(n + 1, vector<int>(LOG, r));
depth = vector<int>(n + 1);
tin = vector<int>(n + 1);
tout = vector<int>(n + 1);
lca_dfs(r, -1);
}
void lca_dfs(int node, int par) {
tin[node] = timer++;
trav(child, adj[node]) {
if (child == par) conts;
up[child][0] = node;
rep1(j, LOG - 1) {
up[child][j] = up[up[child][j - 1]][j - 1];
}
depth[child] = depth[node] + 1;
lca_dfs(child, node);
}
tout[node] = timer-1;
}
int lift(int u, int k) {
rep(j, LOG) {
if (k & (1 << j)) {
u = up[u][j];
}
}
return u;
}
int query(int u, int v) {
if (depth[u] < depth[v]) swap(u, v);
int k = depth[u] - depth[v];
u = lift(u, k);
if (u == v) return u;
rev(j, LOG - 1, 0) {
if (up[u][j] != up[v][j]) {
u = up[u][j];
v = up[v][j];
}
}
u = up[u][0];
return u;
}
int get_dis(int u, int v) {
int lca = query(u, v);
return depth[u] + depth[v] - 2 * depth[lca];
}
bool is_ances(int u, int v){
return tin[u] <= tin[v] and tout[u] >= tout[v];
}
};
vector<ll> depth(N);
vector<ll> leaves(N);
vector<ll> dp(N);
vector<ll> single(N);
vector<bool> leaf(N);
void dfs1(ll u, ll p){
leaves[u] = 0;
ll children = 0;
trav(v,adj[u]){
if(v == p) conts;
depth[v] = depth[u]+1;
dfs1(v,u);
children++;
}
if(!children){
leaves[u] = leaf[u] = single[u] = 1;
return;
}
trav(v,adj[u]){
if(v == p) conts;
leaves[u] += leaves[v];
}
if(leaves[u]){
if(leaves[u]&1){
dp[u] = depth[u]*(leaves[u]>>1);
single[u] = (leaves[u] == 1);
leaves[u] = 1;
}
else{
dp[u] = depth[u]*((leaves[u]-2)>>1);
leaves[u] = 2;
}
}
}
ll parity_sum[N][2];
vector<ll> single_sum(N);
void dfs2(ll u, ll p){
parity_sum[u][leaves[u]&1] = depth[u];
single_sum[u] = depth[u]*single[u];
if(p != -1){
rep(j,2){
parity_sum[u][j] += parity_sum[p][j];
}
single_sum[u] += single_sum[p];
}
trav(v,adj[u]){
if(v == p) conts;
dfs2(v,u);
}
}
vector<ll> adj2[N];
void solve(int test_case)
{
ll n,q; cin >> n >> q;
rep1(i,n-1){
ll u,v; cin >> u >> v;
adj[u].pb(v), adj[v].pb(u);
}
ll root = -1;
rep1(i,n){
if(sz(adj[i]) >= 2){
root = i;
}
}
dfs1(root,-1);
dfs2(root,-1);
lca_algo LCA(n,root);
ll depth_sum = 0;
rep1(i,n) depth_sum += depth[i]*leaf[i];
ll leaf_cnt = 0;
rep1(i,n) leaf_cnt += leaf[i];
ll sub = 0;
rep1(i,n) sub += dp[i];
vector<ll> val(n+5);
rep1(u,n) val[u] = -leaf[u];
while(q--){
ll k; cin >> k;
vector<pll> b(k);
ll curr_depth_sum = depth_sum;
ll curr_leaf_cnt = leaf_cnt;
ll curr_sub = sub;
rep(i,k){
ll u; cin >> u;
b[i] = {LCA.tin[u],u};
curr_leaf_cnt++;
curr_depth_sum += depth[u]+1;
if(val[u] == -1){
curr_leaf_cnt--;
curr_depth_sum -= depth[u];
}
val[u]++;
}
sort(all(b));
rep(i,k-1){
ll u = b[i].ss, v = b[i+1].ss;
ll lca = LCA.query(u,v);
b.pb({LCA.tin[lca],lca});
}
b.pb({LCA.tin[root],root});
sort(all(b));
b.resize(unique(all(b))-b.begin());
ll siz = sz(b);
vector<ll> par(siz);
stack<ll> stk;
rep(i,siz){
ll u = b[i].ss;
while(!stk.empty() and !LCA.is_ances(stk.top(),u)){
stk.pop();
}
if(i){
par[i] = stk.top();
adj2[par[i]].pb(u);
}
stk.push(u);
}
rev(i,siz-1,0){
ll u = b[i].ss, p = par[i];
trav(v,adj2[u]){
val[u] += val[v];
}
ll even_sum = parity_sum[u][0]-parity_sum[p][0];
ll odd_sum = parity_sum[u][1]-parity_sum[p][1];
ll curr_single_sum = single_sum[u]-single_sum[p];
curr_sub += even_sum*(val[u]/2);
curr_sub += odd_sum*ceil2(val[u],2);
if(val[u] >= 1){
curr_sub -= curr_single_sum;
}
}
ll ans = curr_depth_sum-curr_sub*2;
if(curr_leaf_cnt&1) ans = -1;
cout << ans << endl;
for(auto [ti,u] : b){
val[u] = -leaf[u];
adj2[u].clear();
}
}
}
int main()
{
fastio;
int t = 1;
// cin >> t;
rep1(i, t) {
solve(i);
}
return 0;
}
Compilation message
cleaning.cpp: In function 'void dfs1(ll, ll)':
cleaning.cpp:165:41: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
165 | leaves[u] = leaf[u] = single[u] = 1;
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
20824 KB |
Output is correct |
2 |
Incorrect |
50 ms |
25236 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
24 ms |
24180 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
32 ms |
24908 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
48 ms |
25424 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
64 ms |
33108 KB |
Output is correct |
2 |
Incorrect |
93 ms |
33288 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
104 ms |
39112 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
20824 KB |
Output is correct |
2 |
Incorrect |
50 ms |
25236 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |