题目链接

题意分析

一句话题意 : 树上一条链中挑选出某些数 异或和最大

我们可以考虑维护一个树上倍增线性基

然后倍增的时候 维护一个线性基合并就可以了

写起来还是比较容易的

CODE:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<string>
#include<queue>
#include<map>
#include<stack>
#include<list>
#include<set>
#include<deque>
#include<vector>
#include<ctime>
#define ll long long
#define inf 0x7fffffff
#define N 200008
#define IL inline
#define M 66
#define D double
#define ull unsigned long long
#define R register
using namespace std;
template<typename T>IL void read(T &_)
{
T __=0,___=1;char ____=getchar();
while(!isdigit(____)) {if(____=='-') ___=0;____=getchar();}
while(isdigit(____)) {__=(__<<1)+(__<<3)+____-'0';____=getchar();}
_=___ ? __:-__;
}
/*-------------OI使我快乐-------------*/
int n,m,tot;
int to[N<<1],nex[N<<1],head[N];
int dep[N],fath[N][20];
ll num[N],ans[M],key[N][20][M];
IL void add(int x,int y)
{to[++tot]=y;nex[tot]=head[x];head[x]=tot;}
IL void insert(ll *x,ll y)
{
for(R int i=61;i>=0;--i)
{
ll now=(1ll<<i);
if(now&y)
{
if(!x[i]) {x[i]=y;break;}
y^=x[i];
}
}
}
IL void merge(ll *cdy,ll *wzy)
{
for(R int i=61;i>=0;--i) if(cdy[i]) insert(wzy,cdy[i]);
}
IL ll qury_ans()
{
ll res=0;
for(R int i=61;i>=0;--i)
if((res^ans[i])>res) res^=ans[i];
return res;
}
IL void dfs(int now,int fat)
{
fath[now][0]=fat;dep[now]=dep[fat]+1;
insert(key[now][0],num[now]);
for(R int i=1;(1<<i)<=dep[now];++i)
{//倍增 + 合并线性基
fath[now][i]=fath[fath[now][i-1]][i-1];
merge(key[now][i-1],key[now][i]);
merge(key[fath[now][i-1]][i-1],key[now][i]);
}
for(R int i=head[now];i;i=nex[i])
{
int v=to[i];
if(v==fat) continue;
dfs(v,now);
}
}
IL ll qury_LCA(int nowx,int nowy)
{//倍增 + 维护答案线性基
memset(ans,0,sizeof ans);
if(dep[nowx]<dep[nowy]) swap(nowx,nowy);
for(R int i=19;i>=0;--i)
{
if(dep[fath[nowx][i]]>=dep[nowy])
{
merge(key[nowx][i],ans);
nowx=fath[nowx][i];
}
}
if(nowx==nowy)
{
insert(ans,num[nowx]);
// insert(ans,num[nowx]);
// insert(ans,num[fath[nowx][0]]);
return qury_ans();
}
for(R int i=19;i>=0;--i)
{
if(fath[nowx][i]!=fath[nowy][i])
{
merge(key[nowx][i],ans);
merge(key[nowy][i],ans);
nowx=fath[nowx][i];nowy=fath[nowy][i];
}
}
insert(ans,num[nowx]);
insert(ans,num[nowy]);
insert(ans,num[fath[nowx][0]]);
// insert(ans,num[nowx]);
// insert(ans,num[fath[nowx][0]]);
// insert(ans,num[nowy]);
// insert(ans,num[fath[nowy][0]]);
return qury_ans();
}
int main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
read(n);read(m);
for(R int i=1;i<=n;++i) read(num[i]);
for(R int i=1,x,y;i<n;++i)
{
read(x);read(y);
add(x,y);add(y,x);
}
dfs(1,0);
while(m--)
{
int x,y;
read(x);read(y);
printf("%lld\n",qury_LCA(x,y));
}
// fclose(stdin);
// fclose(stdout);
return 0;
}

P3292 [SCOI2016]幸运数字的更多相关文章

  1. 洛谷P3292 [SCOI2016]幸运数字 线性基+倍增

    P3292 [SCOI2016]幸运数字 传送门 题目描述 A 国共有 n 座城市,这些城市由 n-1 条道路相连,使得任意两座城市可以互达,且路径唯一.每座城市都有一个幸运数字,以纪念碑的形式矗立在 ...

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

    洛谷题目链接:[SCOI2016]幸运数字 题目描述 A 国共有 n 座城市,这些城市由 n-1 条道路相连,使得任意两座城市可以互达,且路径唯一.每座城市都有一个幸运数字,以纪念碑的形式矗立在这座城 ...

  3. 洛谷P3292 [SCOI2016] 幸运数字 [线性基,倍增]

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

  4. P3292 [SCOI2016]幸运数字 线性基

    正解:线性基+倍增 解题报告: 先放下传送门QAQ 然后这题,其实没什么太大的技术含量,,,?就几个知识点套在一起,除了代码长以外没任何意义,主要因为想复习下线性基的题目所以还是写下,,, 随便写下思 ...

  5. 洛谷P3292 [SCOI2016]幸运数字(倍增+线性基)

    传送门 不知道线性基是什么东西的可以看看蒟蒻的总结 第一眼:这不会是个倍增LCA暴力合并线性基吧…… 打了一发……A了? 所以这真的是个暴力倍增LCA合并线性基么…… ps:据某大佬说其实可以离线之后 ...

  6. P3292 [SCOI2016]幸运数字 [线性基+倍增]

    线性基+倍增 // by Isaunoya #include <bits/stdc++.h> using namespace std; #define rep(i, x, y) for ( ...

  7. 【 [SCOI2016]幸运数字】

    P3292 [SCOI2016]幸运数字 想法 倍增加上线性基就行惹 线性基的合并可以通过把一个线性基的元素插入到另一个里实现 #include<iostream> #include< ...

  8. BZOJ 4568: [Scoi2016]幸运数字 [线性基 倍增]

    4568: [Scoi2016]幸运数字 题意:一颗带点权的树,求树上两点间异或值最大子集的异或值 显然要用线性基 可以用倍增的思想,维护每个点向上\(2^j\)个祖先这些点的线性基,求lca的时候合 ...

  9. [SCOI2016]幸运数字 树链剖分,线性基

    [SCOI2016]幸运数字 LG传送门 为了快乐,我们用树剖写这题. 强行树剖,线段树上每个结点维护一个线性基,每次查询暴力合并. 瞎分析一波复杂度:树剖两点之间\(\log n\)条重链,每条重链 ...

随机推荐

  1. 使用twised实现一个EchoServer

    ProtocolsProtocols描述了如何以异步的方式处理网络中断时间,HTTP.DNS已经IMAP是应用应用层协议中的例子,Protocols实现了IProtocol接口,它饱和如下的方法 ma ...

  2. How to use mouse to moving windows of not have title bar?

    How to use mouse to moving windows of not have title bar? #include "widget.h" #include < ...

  3. 训练超参数, 出现 Cannot use GPU in CPU-only Caffe 错误?

    当我们用MNIST手写体数字数据库和LeNet CNN 模型训练超参数,运行 examples/mnist/train_lenet.sh是出现Cannot use GPU in CPU-only Ca ...

  4. UVa 1395 Slim Span (最小生成树)

    题意:给定n个结点的图,求最大边的权值减去最小边的权值最小的生成树. 析:这个和最小生成树差不多,从小到大枚举左端点,对于每一个左端点,再枚举右端点,不断更新最小值.挺简单的一个题. #include ...

  5. linux每天一小步---mkdir命令详解

    1 命令功能 mkdir命令用于创建单个目录或者多级目录,但前提在于用户对于当前目录有写权限. 2 命令语法 mkdir  [选项]  [目录名] 3 命令参数 -m 在创建目录的同时设定目录权限(而 ...

  6. CString->char*.,char*->CString,char*->LPCTSTR

    CString->char* CString strSource;//宣告CString char* charSource; //宣告char* 法1: charSource = (char*) ...

  7. R12 更新对应用户的字符集

    R12 更新对应用户的字符集     症状:应用系统数据导出操作,经常发生导出文件(XLS / TSV)产生简体中文乱码. 方案:针对客户端当前用户进行字符集更新 ZHS16GBK,而不影响其他用户. ...

  8. hibernate 中 fetch=FetchType.LAZY 懒加载失败处理

    对这种懒加载问题,最后的做法是利用Spring提供的一个针对Hibernate的一个支持类,其主要意思是在发起一个页面请求时打开Hibernate的Session,一直保持这个Session,使得Hi ...

  9. Jenkins RestAPI调用出现Error 403 No valid crumb was included in the request

    方法一(不推荐): 在jenkins 的Configure Global Security下 , 取消“防止跨站点请求伪造(Prevent Cross Site Request Forgery exp ...

  10. pipeline构建时报错问题解决

    问题: 1.No such field found: field java.lang.String sh. Administrators can decide whether to approve o ...