Luo Valley P3292 [SCOI2016] + yl lucky numbers linear multiplier

P3292 [SCOI2016] lucky number

Portal

Title Description

A country there are n cities, these cities are connected by n-1 road, such that any two cities can reach each other, and the only path. Every city has a lucky number, in the form of a monument stands in the center of the city, as a symbol of the city.

A few travelers wishing to explore the country. Voyager plane landed in x number of cities, between cities along the x-y No. No. The only piece of city tour path, finally took off from the city to leave the country A y. While passing through every city, who will have the opportunity to visit and photograph the lucky number of the city, so this will be lucky to save himself. Fortunately, however, is not simply superimposed, this tour had also been very clear. They adore the lucky number is a different way or remain in his body.

For example, the tour who took three photos, fortunately values ​​are 5,7,11, then the final to retain himself lucky value is 9 (5 xor 7 xor 11).

Some clever tour were found, as long as selectively to take pictures, will be able to get more lucky value. Fortunately, the value three in the above example, only select 5 and 11, the value 14 can be retained lucky. Now, some tour who found clever you, I hope you help them calculate the maximum value fortunate in their itinerary can be retained is.

Input Format

The first line contains two positive integers n, q, and the number of travelers denote the number of cities.

The second line contains a non-negative integer n, where Gi denotes the i-th integer value i lucky number of cities.

Then n-1 rows, each row comprising two positive integers x, y, expressed coupled to a number of urban road between x and y number city.

Then q lines, each line contains two positive integers x, y, indicate the name of the traveler's itinerary is from x to y number number of urban cities. N <= 20000, Q <= 200000, Gi <= 2 ^ 60

Output Format

Q output need to include lines, each line contains a non-negative integer representing the maximum value of the name of the lucky travelers can keep.

Sample input and output

Entry
4 2
11 5 7 9
1 2
1 3
1 4
2 3
1 4
Export 
14 
11 


Solution: Requirements XOR maximum value on the path, we know that demand maximum XOR value can be done by linear base, then how do we get a linear base between the two cities do? We know that n cities and roads constitute a tree, we can find these two cities LCA, then the two cities to its two linear LCA-based LCA combined to get on this path. Multiplying the array usually used to dis recording linear array group maintenance.
Code:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2e4 + 10;
ll a[N],b[N][20][70],dep[N],ans[70],fa[N][20];
vector<int> v[N];
ll read() {
    ll x = 0, f = 1;
    char ch = getchar();
    while(ch>'9'||ch<'0'){
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9') {
        x = x * 10 + ch-'0';
        ch = getchar();
    }
    return x * f;
}
void add(ll B[],ll x) {
    for (int i = 63; i >= 0; i--) {
        if (x & (1ll<<i)) {
            if (B[i]) x^=B[i];
            else {
                B[i] = x;
                break;
            }
        }
    }
}
void merge(ll B1[],ll B2[]){
    for (int i = 63; i >= 0; i--) 
        if (B2[i]) add(B1,B2[i]);
}
void dfs(int f,int u) {
    fa[u][0] = f;
    dep[u] = dep[f] + 1;
    for (int i = 1; i <= 15; i++) {
        fa[u][i] = fa[fa[u][i-1]][i-1];
        merge(b[u][i],b[u][i-1]);
        merge(b[u][i] ,b[fa[u][i-1]][i-1]);
    }
    for (int i = 0; i < v[u].size(); i++) {
        if (f == v[u][i]) continue;
        dfs(u,v[u][i]);
    }
}
void lca(int x,int y) {
    memset(ans,0,sizeof(ans));
    if (dep[x] < dep[y]) swap(x,y);
    for (int i = 15; i >= 0; i--) 
        if (dep[fa[x][i]] >= dep[y]) {
            merge(ans,b[x][i]);
            x = fa[x][i];
        }
    if (x == y) {
        merge(ans,b[x][0]);
        return;
    }
    for (int i = 15; i >= 0; i--) 
        if (fa[x][i] != fa[y][i]) {
            merge(ans,b[x][i]);
            merge(ans,b[y][i]);
            x = fa[x][i];
            y = fa[y][i];
        }
    merge(ans,b[x][0]);
    merge(ans,b[y][0]);
    merge(ans,b[fa[x][0]][0]);
}
int main() {
    ll n = read(),q = read(),x,y;
    for (ll i = 1; i <= n; i++) 
        add(b[i][0],a[i] = read());
    for(ll i = 1;i < n; i++){
        x = read(),y = read();
        v[x].push_back(y);
        v[y].push_back(x);
    }
    dfs(0,1);
    while(q--) {
        x = read(),y = read();
        lca(x,y);
        ll sum = 0;
        for (int i = 63; i >= 0; i--)
            sum Max = (SUM, SUM ^ ANS [I]); 
        the printf ( " % LLD \ n- " , SUM); 
    } 
    return  0 ; 
} 


// variables wrong looking to find more than one hour bug
View Code
 
  

 

 
 

Guess you like

Origin www.cnblogs.com/l999q/p/11291239.html