zoukankan      html  css  js  c++  java
  • [BZOJ4568][Scoi2016]幸运数字 倍增+线性基

    4568: [Scoi2016]幸运数字

    Time Limit: 60 Sec  Memory Limit: 256 MB
    Submit: 1791  Solved: 685
    [Submit][Status][Discuss]

    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

    HINT

     

    Source

    树上倍增维护线性基

     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdlib>
     4 #include<cstdio>
     5 #include<cmath>
     6 #include<algorithm>
     7 #define maxn 20005
     8 #define ll long long
     9 using namespace std;
    10 ll fa[maxn][18],s[maxn][18][65];
    11 ll n,q,a[maxn];
    12 ll tmp[65];
    13 int dep[maxn];
    14 struct data {
    15     int to,next;
    16 }e[maxn*2];
    17 int head[maxn],cnt;
    18 void add(int u,int v) {e[cnt].next=head[u];e[cnt].to=v;head[u]=cnt++;}
    19 void build(ll *t1,ll t2) {
    20     for(int i=60;i>=0;i--) {
    21         if((t2>>i)&1ll) {
    22             if(!t1[i]) {t1[i]=t2;return;}
    23             else t2^=t1[i];
    24         }
    25     }
    26 }
    27 void merge(ll *t1,ll *t2) {for(int i=60;i>=0;i--) if(t2[i]) build(t1,t2[i]);}
    28 void dfs(int x,int f) {
    29     dep[x]=dep[f]+1;
    30     build(s[x][0],a[x]);
    31     for(int i=1;i<=17;i++) fa[x][i]=fa[fa[x][i-1]][i-1];
    32     for(int i=1;i<=17;i++) {
    33         memcpy(s[x][i],s[x][i-1],sizeof(s[x][i-1]));
    34         merge(s[x][i],s[fa[x][i-1]][i-1]);
    35     }
    36     for(int i=head[x];i>=0;i=e[i].next) {
    37         int to=e[i].to;if(to==f) continue;
    38         fa[to][0]=x;
    39         dfs(to,x);
    40     }
    41 }
    42 
    43 int lca(int x,int y) {
    44     if(dep[x]<dep[y]) swap(x,y);
    45     int t=dep[x]-dep[y];
    46     for(int i=17;i>=0;i--) if((t>>i)&1) {merge(tmp,s[x][i]);x=fa[x][i];}
    47     for(int i=17;i>=0;i--) if(fa[x][i]!=fa[y][i]) {merge(tmp,s[x][i]);merge(tmp,s[y][i]);x=fa[x][i],y=fa[y][i];}
    48     if(x==y) return x;
    49     merge(tmp,s[x][0]);merge(tmp,s[y][0]);
    50     return fa[x][0];
    51 }
    52 
    53 int main() {
    54     memset(head,-1,sizeof(head));
    55     scanf("%lld%lld",&n,&q);
    56     for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
    57     for(int i=1;i<n;i++) {
    58         int u,v;
    59         scanf("%d%d",&u,&v);add(u,v);add(v,u);
    60     }
    61     dfs(1,0);
    62     for(int i=1;i<=q;i++) {
    63         memset(tmp,0,sizeof(tmp));
    64         int x,y;
    65         scanf("%d%d",&x,&y);
    66         int z=lca(x,y);merge(tmp,s[z][0]);
    67         ll ans=0;
    68         for(int j=60;j>=0;j--) ans=max(ans,ans^tmp[j]);
    69         printf("%lld
    ",ans);
    70     }
    71 }
    View Code
    O(∩_∩)O~ (*^__^*) 嘻嘻…… O(∩_∩)O哈哈~
  • 相关阅读:
    gevent实现基于epoll和协程的服务器
    用greenlet实现协程消费者生产者
    More is better(MST)(求无向图中最大集合元素个数)
    小希的迷宫(MST单棵树判断法则)
    畅通工程再续(MST)
    畅通工程再续
    畅通工程
    还是畅通工程(MST)
    Minimum Inversion Number
    Who Gets the Most Candies?(线段树 + 反素数 )
  • 原文地址:https://www.cnblogs.com/wls001/p/8392238.html
Copyright © 2011-2022 走看看