[2016] Tsinghua training and Alice and Bob are playing games

The topic is not difficult. Because SG nature, we only need to find a tree.

Then if it is found not from the top row DP, DP so from the bottom up.

Consider a point subtree merge, delete consideration of a point in which sub-tree, then the rest of the state is actually a sub-tree can reach all of the state or a different number.

In this case there is not sub-tree state, directly into the exclusive-OR value SG subtree.

So obviously, it is to maintain a supporting all XOR, and state merge, query mex data structures directly trie merge with tag just fine.

Temporal complexity \ (O (n \ log n ) \)

#include <bits/stdc++.h>

const int MAXN = 100010;
const int UP = 21;
const int MN = MAXN * (UP + 1);
int son[MN][2], val[MN], tag[MN], txt;
void mktag(int u, int v) { tag[u] ^= v; }
void pushdown(int u, int dig) {
    if (tag[u]) {
        if (son[u][0]) mktag(son[u][0], tag[u]);
        if (son[u][1]) mktag(son[u][1], tag[u]);
        if (tag[u] >> dig & 1)
            std::swap(son[u][0], son[u][1]);
        tag[u] = 0;
    }
}
void update(int u) { val[u] = val[son[u][0]] + val[son[u][1]]; }
void insert(int & x, int v, int dig = UP) {
    if (!x) x = ++txt, son[x][0] = son[x][1] = val[x] = tag[x] = 0;
    if (dig == -1) return (void) (val[x] = 1);
    pushdown(x, dig);
    insert(son[x][v >> dig & 1], v, dig - 1);
    update(x);
}
int merge(int x, int y, int dig = UP) {
    if (!x || !y) return x | y;
    pushdown(x, dig), pushdown(y, dig);
    son[x][0] = merge(son[x][0], son[y][0], dig - 1);
    son[x][1] = merge(son[x][1], son[y][1], dig - 1);
    if (dig != -1) update(x);
    return x;
}
int query(int u, int dig = UP) {
    if (!u) return 0;
    if (val[son[u][0]] < (1 << dig)) return query(son[u][0], dig - 1);
    return query(son[u][1], dig - 1) | 1 << dig;
}
int head[MAXN], nxt[MAXN << 1], to[MAXN << 1], tot;
void addedge(int b, int e) {
    nxt[++tot] = head[b]; to[head[b] = tot] = e;
    nxt[++tot] = head[e]; to[head[e] = tot] = b;
}
int sg[MAXN], rts[MAXN];
bool vis[MAXN];
int solve(int u, int fa = 0) {
    vis[u] = true;
    int pre = 0, & rt = rts[u];
    for (int i = head[u]; i; i = nxt[i]) if (to[i] != fa)
        solve(to[i], u), pre ^= sg[to[i]];
    for (int i = head[u]; i; i = nxt[i]) 
        if (to[i] != fa) {
            mktag(rts[to[i]], pre ^ sg[to[i]]);
            rt = merge(rt, rts[to[i]]);
        }
    insert(rt, pre);
    sg[u] = query(rt);
    return rt;
}
int n, m;
int main() {
    std::ios_base::sync_with_stdio(false), std::cin.tie(0);
    int T; std::cin >> T;
    while (T --> 0) {
        std::cin >> n >> m;
        for (int i = 1; i <= m; ++i) {
            int t1, t2; std::cin >> t1 >> t2;
            addedge(t1, t2);
        }
        int ans = 0;
        for (int i = 1; i <= n; ++i) if (!vis[i])
            solve(i), ans ^= sg[i];
        std::cout << (ans ? "Alice" : "Bob") << '\n';
        tot = 0; memset(head, 0, n + 1 << 2);
        memset(vis, 0, n + 1); txt = 0;
        memset(rts, 0, n + 1 << 2);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/daklqw/p/11563341.html