#include <bits/stdc++.h>
#define fo(i, d, c) for (int i = d; i <= c; i++)
#define fod(i, c, d) for (int i = c; i >= d; i--)
#define maxn 1000010
#define N 1010
#define fi first
#define se second
#define pb emplace_back
#define en cout << "\n";
#define int long long
#define inf (int)1e18
#define bitcount(x) __builtin_popcountll(x)
#define pii pair<int, int>
#define vii vector<pii>
#define lb(x) x & -x
#define bit(i, j) ((i >> j) & 1)
#define offbit(i, j) (i ^ (1LL << j))
#define onbit(i, j) (i | (1LL << j))
#define vi vector<int>
#define all(x) x.begin(), x.end()
#define ss(x) (int)x.size()
template <typename T1, typename T2>
bool minimize(T1 &a, T2 b)
{
if (a > b)
{
a = b;
return true;
}
return false;
}
template <typename T1, typename T2>
bool maximize(T1 &a, T2 b)
{
if (a < b)
{
a = b;
return true;
}
return false;
}
using namespace std;
const int nsqrt = 450;
const int mod = 1e9 + 7;
void add(int &x, int k)
{
x += k;
x %= mod;
if (x < 0)
x += mod;
}
void del(int &x, int k)
{
x -= k;
x %= mod;
if (x < 0)
x += mod;
}
struct SegmentTree
{
vector<int> st, lazy;
int n;
/// real n
stack<pii> s;
SegmentTree(int _n = 0) : n(_n)
{
st.resize(4 * n + 10);
lazy.resize(4 * n + 10);
}
void resize(int _n)
{
n = _n;
st.resize(4 * n + 10);
lazy.resize(4 * n + 10);
}
void down(int id, int l, int r)
{
if (lazy[id] == 0)
return;
int mid = l + r >> 1;
st[id << 1] = (mid - l + 1) - st[id << 1];
lazy[id << 1] = lazy[id << 1] ^ 1;
st[id << 1 | 1] = (r - mid) - st[id << 1 | 1];
lazy[id << 1 | 1] = lazy[id << 1 | 1] ^ 1;
lazy[id] = 0;
}
void build(int id,int l,int r)
{
if(l == r)
{
st[id] = r - l + 1;
return;
}
int mid = l + r >> 1;
build(id << 1,l,mid);
build(id << 1 | 1,mid + 1,r);
st[id] = st[id << 1] + st[id << 1 | 1];
}
void update(int id, int l, int r, int u, int v)
{
if (u > r || l > v)
return;
if (u <= l && r <= v)
{
st[id] = (r - l + 1) - st[id];
lazy[id] = lazy[id] ^ 1;
return;
}
down(id, l, r);
int mid = (l + r) >> 1;
update(id << 1, l, mid, u, v);
update(id << 1 | 1, mid + 1, r, u, v);
st[id] = st[id << 1] + st[id << 1 | 1];
}
int get(int id, int l, int r, int u, int v)
{
if (l > v || u > r)
return 0;
if (u <= l && r <= v)
return st[id];
down(id, l, r);
int mid = (l + r) >> 1;
return get(id << 1, l, mid, u, v) + get(id << 1 | 1, mid + 1, r, u, v);
}
int get(int l, int r)
{
return get(1, 1, n, l, r);
}
void update(int l, int r)
{
s.push({l, r});
update(1, 1, n, l, r);
}
} st(maxn);
vi ke[maxn];
int n, q;
int par[maxn][20];
int h[maxn];
int head[maxn], ind[maxn], pos[maxn], sz[maxn];
int cnt, chaincnt;
void dfs(int u)
{
sz[u] = 1;
for (int v : ke[u])
{
if (v == par[u][0])
continue;
h[v] = h[u] + 1;
par[v][0] = u;
fo(i, 1, 19) par[v][i] = par[par[v][i - 1]][i - 1];
dfs(v);
sz[u] += sz[v];
}
}
void hld(int u)
{
if (head[chaincnt] == 0)
head[chaincnt] = u;
ind[u] = chaincnt;
pos[u] = ++cnt;
int maxx = 0, sc = -1;
for (int v : ke[u])
{
if (v == par[u][0])
continue;
if (sz[v] > maxx)
{
maxx = sz[v], sc = v;
}
}
if (sc != -1)
hld(sc);
for (int v : ke[u])
if (v != sc && v != par[u][0])
{
++chaincnt;
hld(v);
}
}
int lca(int u, int v)
{
if (h[u] < h[v])
swap(u, v);
int del = h[u] - h[v];
fod(i, 19, 0) if (bit(del, i)) u = par[u][i];
if (u == v)
return u;
fod(i, 19, 0) if (par[u][i] != par[v][i]) u = par[u][i], v = par[v][i];
return par[u][0];
}
void change(int u, int v)
{
int uchain, vchain = ind[v];
while (1)
{
uchain = ind[u];
if (uchain == vchain)
{
st.update(pos[v], pos[u]);
return;
}
st.update(pos[par[head[uchain]][0]], pos[u]);
u = par[head[uchain]][0];
}
}
main()
{
#define name "TASK"
if (fopen(name ".inp", "r"))
{
freopen(name ".inp", "r", stdin);
freopen(name ".out", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> q;
st.resize(n);
st.build(1,1,n);
fo(i, 1, n - 1)
{
int u, v;
cin >> u >> v;
ke[u].pb(v);
ke[v].pb(u);
}
int root;
fo(i, 1, n) if (ss(ke[i]) > 1)
{
root = i;
dfs(root);
hld(root);
break;
}
int leaves = 0;
fo(i, 1, n) if (ss(ke[i]) == 1)
{
leaves++;
change(i, root);
}
while (q--)
{
int m;
cin >> m;
vi v(m);
for (int &x : v)
{
cin >> x;
if (ss(ke[x]) == 1)
continue;
else
{
leaves++;
change(x, root);
}
}
if (leaves % 2 == 1)
cout << -1;
else
cout << n + m + st.st[1] - 2;
en;
for (int x : v)
{
if (ss(ke[x]) == 1)
continue;
else
{
leaves--;
change(x, root);
}
}
}
}
Compilation message
cleaning.cpp: In member function 'void SegmentTree::down(long long int, long long int, long long int)':
cleaning.cpp:80:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
80 | int mid = l + r >> 1;
| ~~^~~
cleaning.cpp: In member function 'void SegmentTree::build(long long int, long long int, long long int)':
cleaning.cpp:94:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
94 | int mid = l + r >> 1;
| ~~^~~
cleaning.cpp: At global scope:
cleaning.cpp:207:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
207 | main()
| ^~~~
cleaning.cpp: In function 'int main()':
cleaning.cpp:212:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
212 | freopen(name ".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
cleaning.cpp:213:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
213 | freopen(name ".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
cleaning.cpp:269:23: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
269 | change(x, root);
| ~~~~~~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
86352 KB |
Output is correct |
2 |
Incorrect |
148 ms |
98768 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
50 ms |
88224 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
83 ms |
91708 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
89 ms |
95312 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
283 ms |
116124 KB |
Output is correct |
2 |
Incorrect |
445 ms |
120404 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
310 ms |
123360 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
86352 KB |
Output is correct |
2 |
Incorrect |
148 ms |
98768 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |