BZOJ 4568 幸运数字

题目传送门

Description

A 国共有 n 座城市,这些城市由 n-1 条道路相连,使得任意两座城市可以互达,且路径唯一。每座城市都有一个幸运数字,以纪念碑的形式矗立在这座城市的正中心,作为城市的象征。一些旅行者希望游览 A 国。旅行者计划乘飞机降落在 x 号城市,沿着 x 号城市到 y 号城市之间那条唯一的路径游览,最终从 y 城市起飞离开 A 国。在经过每一座城市时,游览者就会有机会与这座城市的幸运数字拍照,从而将这份幸运保存到自己身上。然而,幸运是不能简单叠加的,这一点游览者也十分清楚。他们迷信着幸运数字是以异或的方式保留在自己身上的。例如,游览者拍了 3 张照片,幸运值分别是 5,7,11,那么最终保留在自己身上的幸运值就是 9(5 xor 7 xor 11)。有些聪明的游览者发现,只要选择性地进行拍照,便能获得更大的幸运值。例如在上述三个幸运值中,只选择 5 和 11 ,可以保留的幸运值为 14 。现在,一些游览者找到了聪明的你,希望你帮他们计算出在他们的行程安排中可以保留的最大幸运值是多少。

Input

第一行包含 2 个正整数 n ,q,分别表示城市的数量和旅行者数量。第二行包含 n 个非负整数,其中第 i 个整数 Gi 表示 i 号城市的幸运值。随后 n-1 行,每行包含两个正整数 x ,y,表示 x 号城市和 y 号城市之间有一条道路相连。随后 q 行,每行包含两个正整数 x ,y,表示这名旅行者的旅行计划是从 x 号城市到 y 号城市。N<=20000,Q<=200000,Gi<=2^60

Output

输出需要包含 q 行,每行包含 1 个非负整数,表示这名旅行者可以保留的最大幸运值。

Sample Input

4 2
11 5 7 9
1 2
1 3
1 4
2 3
1 4

Sample Output

14
11

题解

说实话,这题我是一点思路也没有,后来看Claris的code才有一点思路。
此题首先要搞一个线性基,用点分治进行求重心到每个点的线性基,之后对于每个查询暴力合并线性基。
说实话我的代码就是把Claris的代码看了一遍后再打了一次,罪过

#include <cstdio>
#include <cstring>
typedef long long LL;
//Input
char Inb[1<<16], *Ins = Inb, *Ine = Inb, Inc; LL Ina;
inline char getc() { return (Ins == Ine && (Ine = (Ins = Inb) + fread(Inb, 1, 1 << 16, stdin), Ins == Ine)) ? 0 : *Ins++; }
inline LL geti() {
    while ((Inc = getc()) < '0' || Inc > '9'); Ina = Inc - '0';
    while ((Inc = getc()) >= '0' && Inc <= '9') Ina = (Ina << 3) + (Ina << 1) + Inc - '0'; return Ina;
}
//
const int Maxn = 20010, Maxm = 200010, MS = Maxm * 30;
int head[Maxn], to[Maxn << 1], nxt[Maxn << 1], ok[Maxn << 1], ed;
int Head[Maxn], To[MS], Nxt[MS], Ed, p[Maxm], cnt;
int all, f[Maxn], Son[Maxn], now, pos[Maxn];
LL val[Maxn], ans[Maxm];
struct Q{ int x, y; }q[Maxm];
struct B{
    LL a[60];
    B() { memset(a, 0, sizeof a); }
    inline void ins(LL x) {
        for (int i = 59; ~i && x; --i) if (x >> i & 1) {
            if (a[i]) x ^= a[i]; else a[i] = x, x = 0;
        }
    }
    inline LL ask() {
        LL ret = 0;
        for (int i = 59; ~i; --i) if ((ret ^ a[i]) > ret) ret ^= a[i];
        return ret;
    }
}h[Maxn], b;
inline void adde(int a, int b) { to[++ed] = b; nxt[ed] = head[a]; ok[head[a] = ed] = true; }
inline void Adde(int a, int b) { To[++Ed] = b; Nxt[Ed] = Head[a]; Head[a] = Ed; }
 
void getroot(int x, int y) {
    Son[x] = 1; f[x] = 0;
    for (int i = head[x]; i; i = nxt[i]) 
      if (ok[i] && (to[i] ^ y)) {
          getroot(to[i], x);
          Son[x] += Son[to[i]];
          if (Son[to[i]] > f[x]) f[x] = Son[to[i]];
      }
    if (all - Son[x] > f[x]) f[x] = all - Son[x];
    if (f[x] < f[now]) now = x;
}
 
void dfs(int x, int y, int z) {
    pos[x] = z;
    h[x] = h[y];
    h[x].ins(val[x]);
    for (int i = head[x]; i; i = nxt[i]) 
      if (ok[i] && (to[i] ^ y)) dfs(to[i], x, z);
}
 
void solve(int x) {
    if (!Head[x]) return;
    f[0] = all = Son[x], getroot(x, now = 0);
    int i;
    pos[now] = now; h[now] = B(); h[now].ins(val[now]);
    for (i = head[now]; i; i = nxt[i]) if (ok[i]) dfs(to[i], now, to[i]);
    for (cnt = 0, i = Head[x]; i; i = Nxt[i]) p[++cnt] = To[i]; Head[x] = 0;
    for (i = 1; i <= cnt; ++i) 
      if (pos[q[p[i]].x] == pos[q[p[i]].y]) Adde(pos[q[p[i]].x], p[i]); 
      else {
          b = h[q[p[i]].x];
          B *y = h + q[p[i]].y;
          for (int j = 59; ~j; --j) if (y->a[j]) b.ins(y->a[j]);
          ans[p[i]] = b.ask();
      }
    for (i = head[now]; i; i = nxt[i]) if (ok[i]) ok[i^1] = false,solve(to[i]);
 
}
 
 
int main() {
    int i, n, m, x, y;
    n = geti(), m = geti();
    for (ed = i = 1; i <= n; ++i) val[i] = geti();
    for (i = 1; i < n; ++i) adde(x = geti(), y = geti()), adde(y, x);
    for (i = 1; i <= m; ++i) {
        q[i].x = geti(), q[i].y = geti();
        if (q[i].x ^ q[i].y) Adde(1, i); else ans[i] = val[q[i].x];
    }
    Son[1] = n; solve(1);
    for (i = 1; i <= m; ++i) printf("%lld\n", ans[i]);
    return 0;
}
posted @ 2016-09-04 11:30  cycleke  阅读(228)  评论(0编辑  收藏  举报