Submission #1282892

#TimeUsernameProblemLanguageResultExecution timeMemory
1282892HiepVu217Spring cleaning (CEOI20_cleaning)C++20
100 / 100
120 ms37236 KiB
//Proud of You//
#include <bits/stdc++.h>
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
using namespace std;
const int N = 1e5 + 17;
int n, q, a[N], sz[N], p[N], d[N], b[N], cnt, ans;
bool lz[4 * N];
int zch, head[N], ch[N], zp, pos[N], ar[N];
vector <int> adj[N];
struct node
{
    int c1, c2;
    node operator + (const node &o) const
    {
        return {c1 + o.c1, c2 + o.c2};
    }
} st[4 * N];
inline void dfs (int u, int pr)
{
    sz[u] = 1, p[u] = pr;
    a[u] = (d[u] == 1), cnt += (d[u] == 1);
    for (int v: adj[u])
    {
        if (v == pr)
        {
            continue;
        }
        dfs (v, u);
        sz[u] += sz[v], a[u] += a[v];
    }
    if (u > 1)
    {
        ans += (a[u] % 2 ? 1 : 2);
    }
}
inline void hld (int u, int pr)
{
    if (!head[zch])
    {
        head[zch] = u;
    }
    ch[u] = zch, pos[u] = ++zp, ar[zp] = u;
    int bc = 0;
    for (int v: adj[u])
    {
        if (v == pr)
        {
            continue;
        }
        if (sz[bc] < sz[v])
        {
            bc = v;
        }
    }
    if (bc)
    {
        hld (bc, u);
    }
    for (int v: adj[u])
    {
        if (v == pr || v == bc)
        {
            continue;
        }
        ++zch;
        hld (v, u);
    }
}
inline void build (int id, int l, int r)
{
    if (l == r)
    {
        st[id] = {a[ar[l]] % 2 == 1, a[ar[l]] % 2 == 0};
        return;
    }
    int mid = l + r >> 1;
    build (id * 2, l, mid);
    build (id * 2 + 1, mid + 1, r);
    st[id] = st[id * 2] + st[id * 2 + 1];
}
inline void down (int id)
{
    swap (st[id * 2].c1, st[id * 2].c2);
    lz[id * 2] ^= lz[id];
    swap (st[id * 2 + 1].c1, st[id * 2 + 1].c2);
    lz[id * 2 + 1] ^= lz[id];
    lz[id] = 0;
}
inline void update (int id, int l, int r, int u, int v)
{
    if (v < l || r < u)
    {
        return;
    }
    if (u <= l && r <= v)
    {
        ans -= st[id].c1 + st[id].c2 * 2;
        swap (st[id].c1, st[id].c2);
        ans += st[id].c1 + st[id].c2 * 2;
        lz[id] ^= 1;
        return;
    }
    int mid = l + r >> 1;
    if (lz[id])
    {
        down(id);
    }
    update (id * 2, l, mid, u, v);
    update (id * 2 + 1, mid + 1, r, u, v);
    st[id] = st[id * 2] + st[id * 2 + 1];
}
inline void change (int u, int v)
{
    while (ch[u] != ch[v])
    {
        update (1, 1, n, pos[head[ch[u]]], pos[u]);
        u = p[head[ch[u]]];
    }
    update (1, 1, n, pos[v] + 1, pos[u]);
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> q;
    for (int i = 1, u, v; i < n; ++i)
    {
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
        ++d[u], ++d[v];
    }
    dfs (1, 0);
    hld (1, 0);
    build (1, 1, n);
    for (int i = 1, m; i <= q; ++i)
    {
        cin >> m;
        for (int j = 1, u; j <= m; ++j)
        {
            cin >> u;
            if (d[u] == 1)
            {
                --cnt;
            }
            else
            {
                change (u, 1);
            }
            ++d[u], b[j] = u, ++ans, ++cnt;
        }
        if (cnt & 1)
        {
            cout << "-1\n";
        }
        else
        {
            cout << ans << "\n";
        }
        for (int j = 1, u; j <= m; ++j)
        {
            u = b[j], --d[u], --ans, --cnt;
            if (d[u] == 1)
            {
                ++cnt;
            }
            else
            {
                change (u, 1);
            }
        }
    }
}

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...