[Bzoj4568] [loj # 2013] [Scoi2016] lucky number

Description

\ (A \) State Total \ (n \) cities, these cities by the \ (n-1 \) connected to roads, 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. Some travelers who wish to explore \ (A \) countries. Voyager plane landed at \ (x \) No. city, along \ (x \) No. city to \ (y \) between the number of urban piece unique path tour final from \ (y \) departure city leave \ (A \) countries. 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

The first line contains \ (2 \) positive integer \ (n-\) , \ (Q \) , and the number of travelers denote the number of cities. The second line contains \ (n-\) non-negative integer, wherein the first \ (I \) integer \ (Gi of \) represents \ (I \) Fortunately, the value of the number of cities. Then \ (n-1 \) rows, each row comprising two positive integers \ (X \) , \ (Y \) , represents \ (X \) numbers and the city \ (Y \) there is a road between cities No. connected. Then \ (q \) lines, each line contains two positive integers \ (x \) , \ (y \) , indicate the name of the traveler's itinerary is from \ (x \) No. city to \ (y \) No city.
\ (N \ leq20000, Q \ leq 200000, Gi \ leq 2 ^ 60 \)

Output

Output needs to include \ (q \) lines, each line containing \ (1 \) non-negative integer representing the maximum value of the name of the lucky travelers can keep.

Sample Input

4 2

11 5 7 9

1 2

1 3

1 4

2 3

1 4

Sample Output

14

11


idea

And the maximum use requires exclusive or linear group having in the pile.
So this question we value all stores take on a linear path to throw the base to go to do the most.

Thrown into how to go? Doubled, maintaining the Internet each point jump \ (2 ^ i \) linear points based on this chain
when the query \ (lca \) when it merged linear base on it.

Often there is no card, very moved.


Code

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>

using namespace std;

const int N = 20005;
typedef long long ll;

ll lread(){
    ll x=0;
    char ch=getchar();
    while(!isdigit(ch)) ch=getchar();
    while(isdigit(ch)) x=x*10+ch-'0',ch=getchar();
    return x;
}
int read(){
    int x=0;
    char ch=getchar();
    while(!isdigit(ch)) ch=getchar();
    while(isdigit(ch)) x=x*10+ch-'0',ch=getchar();
    return x;
}

int n;
ll G[N];

struct node{
    int v;
    node *nxt;
}pool[N*2],*h[N];
int cnt;
void addedge(int u,int v){
    node *p=&pool[++cnt],*q=&pool[++cnt];
    p->v=v;p->nxt=h[u];h[u]=p;
    q->v=u;q->nxt=h[v];h[v]=q;
}

int f[N][16],dep[N];
ll g[N][16][65];

void ins(ll *A,ll x) {
    for(int i=60;i>=0;i--)
        if(x&(1ll<<i)){
            if(!A[i]) { A[i]=x; return; }
            x^=A[i];
        }
}
void merge(ll *A,ll *B) { for(int i=60;i>=0;i--) if(B[i]) ins(A,B[i]); }
ll MAX(ll *A) {
    ll ret=0;
    for(int i=60;i>=0;i--) ret=max(ret,ret^A[i]);
    return ret;
}

void dfs(int u){
    int v;
    for(node *p=h[u];p;p=p->nxt)
        if(!dep[v=p->v]){
            dep[v]=dep[u]+1;
            f[v][0]=u; ins(g[v][0],G[u]);
            for(int j=1;j<16;j++){
                f[v][j]=f[f[v][j-1]][j-1];
                memcpy(g[v][j],g[v][j-1],sizeof(g[v][j]));
                merge(g[v][j],g[f[v][j-1]][j-1]);
            }
            dfs(v);
        }
}
ll lca(int x,int y){
    ll c[65]; memset(c,0,sizeof(c));
    ins(c,G[x]); ins(c,G[y]);
    if(dep[x]<dep[y]) swap(x,y);
    for(int i=15;i>=0;i--)
        if(dep[f[x][i]]>=dep[y]) merge(c,g[x][i]),x=f[x][i];
    if(x==y) return MAX(c);
    for(int i=15;i>=0;i--)
        if(f[x][i]!=f[y][i]){
            merge(c,g[x][i]); x=f[x][i];
            merge(c,g[y][i]); y=f[y][i];
        }
    merge(c,g[x][0]);
    return MAX(c);
}

int main()
{
    int Q;
    n=read(); Q=read();
    for(int i=1;i<=n;i++) G[i]=lread();
    for(int i=1;i<n;i++) addedge(read(),read());
    
    dep[1]=1; dfs(1);
    
    while(Q--)
        printf("%lld\n",lca(read(),read()));
    
    return 0;
}

Guess you like

Origin www.cnblogs.com/lindalee/p/11431943.html