[洛谷P3292] [SCOI2016]幸运数字

洛谷题目链接:[SCOI2016]幸运数字

题目描述

A 国共有 n 座城市,这些城市由 n-1 条道路相连,使得任意两座城市可以互达,且路径唯一。每座城市都有一个幸运数字,以纪念碑的形式矗立在这座城市的正中心,作为城市的象征。

一些旅行者希望游览 A 国。旅行者计划乘飞机降落在 x 号城市,沿着 x 号城市到 y 号城市之间那条唯一的路径游览,最终从 y 城市起飞离开 A 国。在经过每一座城市时,游览者就会有机会与这座城市的幸运数字拍照,从而将这份幸运保存到自己身上。然而,幸运是不能简单叠加的,这一点游览者也十分清楚。他们迷信着幸运数字是以异或的方式保留在自己身上的。

例如,游览者拍了 3 张照片,幸运值分别是 5,7,11,那么最终保留在自己身上的幸运值就是 9(5 xor 7 xor 11)。

有些聪明的游览者发现,只要选择性地进行拍照,便能获得更大的幸运值。例如在上述三个幸运值中,只选择 5 和 11 ,可以保留的幸运值为 14 。现在,一些游览者找到了聪明的你,希望你帮他们计算出在他们的行程安排中可以保留的最大幸运值是多少。

输入输出格式

输入格式:

第一行包含 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

输出格式:

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

输入输出样例

输入样例#1:

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

输出样例#1:

14
11

题解: 看到求最大异或和,可能会想到线性基.(事实上我也想不到什么其他的东西)

没错,我们就用线性基来做.

我们在每个节点上维护一个线性基,并且像倍增一样维护一个向上(2^j)距离的线性基,然后每次询问倍增(lca)一下就可以了.

需要注意一下的就是因为(lca)的倍增数组是一个左闭右开的区间,也就是说我们在跳到(lca)的时候还需要多合并一下线性基(因为线性基是左闭右闭的区间).

具体时间复杂度我感觉有点奇怪,大概是(m*logn*60^2)吧....反正就是能过.

#include<bits/stdc++.h>
using namespace std;
const int N = 2e4+5;
typedef int _int;
#define int long long

int n, m, last[N], ecnt = 0, v[N], gup[16][N], dep[N];

struct edge{ int to, nex; }e[N*2];

struct Basis{
    int a[65];
    Basis(){ memset(a, 0, sizeof(a)); }
    void insert(int val){
        for(int i = 61; i >= 0; i--){
            if((val >> i) & 1){
                if(a[i]) val ^= a[i];
                else { a[i] = val; break; }
            }
        }
    }
    int query(){
        int res = 0;
        for(int i = 61; i >= 0; i--)
            if((res^a[i]) > res) res ^= a[i];
        return res;
    }
    void clear(){ memset(a, 0, sizeof(a)); }
}b[16][N], ans;

int gi(){
    int res = 0, f = 1; char i = getchar();
    while(i < '0' || i > '9'){ if(i == '-') f = -1; i = getchar(); }
    while(i >= '0' && i <= '9') res = res*10+i-'0', i = getchar();
    return res*f;
}

void add(int x, int y){
    e[++ecnt].to = y, e[ecnt].nex = last[x], last[x] = ecnt;
}

void dfs(int x, int deep, int f){
    dep[x] = deep, gup[0][x] = f, b[0][x].insert(v[x]);
    for(int to, i = last[x]; i; i = e[i].nex)
        if((to = e[i].to) != f) dfs(to, deep+1, x);
}

Basis merge(Basis x, Basis y){
    Basis res = x;
    for(int i = 61; i >= 0; i--) if(y.a[i]) res.insert(y.a[i]);
    return res;
}

void init(){
    for(int j = 1; j <= 15; j++)
        for(int i = 1; i <= n; i++){
            gup[j][i] = gup[j-1][gup[j-1][i]];
            b[j][i] = merge(b[j-1][i], b[j-1][gup[j-1][i]]);
        }
}

int lca(int x, int y){
    if(dep[x] < dep[y]) swap(x, y);
    for(int i = 15; i >= 0; i--)
        if(dep[gup[i][x]] >= dep[y])
            ans = merge(ans, b[i][x]), x = gup[i][x];
    if(x == y){ ans = merge(ans, b[0][x]); return x; }
    for(int i = 15; i >= 0; i--)
        if(gup[i][x] != gup[i][y]){
            ans = merge(ans, merge(b[i][x], b[i][y]));
            x = gup[i][x], y = gup[i][y];
        }
    ans = merge(merge(ans, b[0][gup[0][x]]), merge(b[0][x], b[0][y]));
    return gup[0][x];
}

_int main(){
    ios::sync_with_stdio(false);
    int x, y; n = gi(), m = gi();
    for(int i = 1; i <= n; i++) v[i] = gi();
    for(int i = 1; i < n; i++) x = gi(), y = gi(), add(x, y), add(y, x);
    dfs(1, 1, 0), init();
    for(int i = 1; i <= m; i++){
        x = gi(), y = gi(), ans.clear(), lca(x, y);
        cout << ans.query() << endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/BCOI/p/10446814.html