Tsinsen A1486. 树(王康宁)
Description
一棵树,问至少有 \(k\) 个黑点的路径最大异或和.
Sol
点分治.
用点分治找重心控制树高就不说了,主要是对答案的统计的地方.
将所有路径按点的个数排序.
可以发现当左端点递增的时候右端点单调递减,时刻满足Trie树里的所有元素都是合法的即可,不断把右端点丢进去,用左端点统计答案.
主要跨越根的时候根的贡献计算了两次,需要删掉一次.
对于需要满足不是一颗子树,可以将Trie树上的节点打一个标记,表示这个节点及其子节点都是在某子树下的路径,子树个数大于1的时候这个标记就没用了.
我在维护标记的时候标记位置打错了...居然有95...调了好长时间QAQ...
Code
#include <bits/stdc++.h>
using namespace std; #define debug(a) cout<<#a<<"="<<a<<" "
const int N = 1e5+50;
const int M = 31; int n,k,kk,rt,ans=-1;
int pow2[M];
int bl[N],v[N],sz[N],t[N];
vector< int > g[N];
int usd[N]; struct pr { int x,y,z; };
bool operator < (const pr &a,const pr &b) { return a.x<b.x; }
vector< pr > S; struct Trie {
int cnt,rt;
int ch[N*M][2],s[N*M],bl[N*M]; int GetNode() { cnt++;ch[cnt][0]=ch[cnt][1]=s[cnt]=0;return cnt; }
void init() {
cnt=0,rt=GetNode();
}
void insert(int x,int fr) {
int o=rt,r;
for(int i=M-1;~i;i--) {
if(x&pow2[i]) r=1;else r=0;
if(!ch[o][r]) ch[o][r]=GetNode(),bl[ch[o][r]]=fr;
else bl[ch[o][r]]=bl[ch[o][r]]==fr ? fr : 0;
o=ch[o][r],s[o]++;
}
}
int getv(int x,int fr) {
int o=rt,r,res=0;
if(!ch[rt][0] && !ch[rt][1]) return -1;
for(int i=M-1;~i;i--) {
if(x&pow2[i]) r=1;else r=0;
if(s[ch[o][r^1]] && bl[ch[o][r^1]]!=fr) res|=pow2[i],r^=1;
if(bl[ch[o][r]]==fr) return -1;
o=ch[o][r];
}return res;
}
}py;
inline int in(int x=0,char ch=getchar()) { while(ch>'9' || ch<'0') ch=getchar();
while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();return x; } void GetRoot(int u,int fa,int nn) {
t[u]=0,sz[u]=1;
for(vector< int >::iterator i=g[u].begin();i!=g[u].end();i++)
if((*i)!=fa && !usd[(*i)]) GetRoot(*i,u,nn),t[u]=max(t[u],sz[*i]),sz[u]+=sz[*i];
t[u]=max(t[u],nn-sz[u]);
if(t[u]<t[rt]) rt=u;
}
void GetS(int u,int fa,int c,int vv,int ff) {
S.push_back((pr){ c,vv,ff });
if(c>=k) ans=max(ans,vv);
for(vector< int >::iterator i=g[u].begin();i!=g[u].end();i++)
if((*i)!=fa && !usd[*i]) GetS(*i,u,c+bl[*i],vv^v[*i],ff);
}
void GetAns(int u,int fa,int nn) {
usd[u]=1,py.init(),S.clear();if(bl[u]>=k) ans=max(ans,v[u]);
for(vector< int >::iterator i=g[u].begin();i!=g[u].end();i++)
if((*i)!=fa && !usd[(*i)]) GetS((*i),u,bl[u]+bl[(*i)],v[(*i)],*i);
sort(S.begin(),S.end()); // cout<<u<<" : "<<nn<<endl;
// for(int i=0;i<(int)S.size();i++) cout<<S[i].x<<" "<<S[i].y<<" "<<S[i].z<<endl; int lim=S.size(),l=0,r=lim-1;
for(;l<lim;l++) {
while(l<r && S[l].x+S[r].x>=k+bl[u]) py.insert(S[r].y,S[r].z),r--;
ans=max(ans,py.getv(S[l].y^v[u],S[l].z));
// debug(l),debug(r),debug(ans)<<endl;
}
// debug(ans)<<endl;
// cout<<"-------------------------"<<endl; int ss;
for(vector< int >::iterator i=g[u].begin();i!=g[u].end();i++)
if((*i)!=fa && !usd[(*i)]) rt=0,ss=sz[(*i)]>sz[u] ? nn-sz[u] : sz[*i],GetRoot((*i),u,ss),GetAns(rt,rt,ss);
}
int main() {
n=in(),k=in();
for(int i=1;i<=n;i++) bl[i]=in();
for(int i=1;i<=n;i++) v[i]=in();
for(int i=1,u,v;i<n;i++) u=in(),v=in(),g[u].push_back(v),g[v].push_back(u); pow2[0]=1;for(int i=1;i<M;i++) pow2[i]=pow2[i-1]<<1;
// for(int i=0;i<M;i++) cout<<pow2[i]<<endl;
rt=0,t[rt]=n+1,GetRoot(1,1,n),GetAns(rt,rt,n); cout<<ans<<endl;
return 0;
}
Tsinsen A1486. 树(王康宁)的更多相关文章
- 【Tsinsen-A1486】树(王康宁) 点分治 + Trie
A1486. 树(王康宁) 时间限制:1.0s 内存限制:512.0MB 总提交次数:455 AC次数:97 平均分:52.62 查看未格式化的试题 提交 试题讨论 试题来源 ...
- A1486. 树(王康宁)
题目:http://www.tsinsen.com/A1486 题解: 其实看到和路径有关的就应该想到点分治. 我们找出重心之后遍历每一棵子树得到它的 { x=经过特殊点的个数,y=到rt的异或和} ...
- Tsinsen A1505. 树(张闻涛) 倍增LCA,可持久化线段树,DFS序
题目:http://www.tsinsen.com/A1505 A1505. 树(张闻涛) 时间限制:1.0s 内存限制:512.0MB 总提交次数:196 AC次数:65 平均分: ...
- 搭建Django链接MySQL流程(python2版)
之前生成选型python3,除了用的python3的pymysql模块之外其他的都是一样的. 1.首先搭建mysql(Mariadb)数据库(单点) 安装方式分为yum安装,rpm包安 ...
- Tsinsen A1219. 采矿(陈许旻) (树链剖分,线段树 + DP)
[题目链接] http://www.tsinsen.com/A1219 [题意] 给定一棵树,a[u][i]代表u结点分配i人的收益,可以随时改变a[u],查询(u,v)代表在u子树的所有节点,在u- ...
- Tsinsen A1517. 动态树 树链剖分,线段树,子树操作
题目 : http://www.tsinsen.com/A1517 A1517. 动态树 时间限制:3.0s 内存限制:1.0GB 总提交次数:227 AC次数:67 平均分:49. ...
- 王学长的AAA树
让我们响应王学长的号召勇敢的分开写splay和lct吧! 分开写大法好!!!!!!!!!!!杜教的ch[4]弱爆了!!!! #include <stdio.h> #include < ...
- 【Tsinsen A1039】【bzoj2638】黑白染色 (BFS树)
Descroption 原题链接 你有一个\(n*m\)的矩形,一开始所有格子都是白色,然后给出一个目标状态的矩形,有的地方是白色,有的地方是黑色,你每次可以选择一个连通块(四连通块,且不要求颜色一样 ...
- 【Tsinsen A1339】JZPLCM (树状数组)
Description 原题链接 给定一长度为\(~n~\)的正整数序列\(~a~\),有\(~q~\)次询问,每次询问一段区间内所有数的\(~LCM~\)(即最小公倍数).由于答案可能很大,输出 ...
随机推荐
- [LeetCode] Fizz Buzz 嘶嘶嗡嗡
Write a program that outputs the string representation of numbers from 1 to n. But for multiples of ...
- [LeetCode] Basic Calculator II 基本计算器之二
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
- Codeforces Round #384(div 2)
A 题意:有n个机场处于一直线上,可两两到达,每个机场只可能属于两家公司中的一家(用0,1表示),现在要从a机场到b机场,可任意次转机.若机场i与机场j从属同一公司,则费用为0,否则费用为1.问最小费 ...
- KD-tree(2维)
用于动态插入以及求某点的最近点的距离(BZOJ2648,BZOJ2716) #include <cstdio> #include <cmath> #include <al ...
- 格式化 float 类型,保留小数点后1位
""" 练习 : 小明的成绩从去年的72分提升到了今年的85分,请计算小明成绩提升的百分点, 并用字符串格式化显示出'xx.x%',只保留小数点后1位: &qu ...
- virtualenv 安装使用
不同的人喜欢用不同的方式建立各自的开发环境,但在几乎所有的编程社区,总有一个(或一个以上)开发环境让人更容易接受. 使用不同的开发环境虽然没有什么错误,但有些环境设置更容易进行便利的测试,并做一些 ...
- JNI开发的常见错误
1. 写错了load的library java.lang.UnsatisfiedLinkError: Couldn't load hell0: findLibrary returned null 2. ...
- 如何在网页标题栏加入logo图标?
标题栏: <link rel="icon" href="ico地址" type="image/x-icon">收藏夹:<l ...
- jquery理财贷款计算器
先放效果图,如下: 需要引入jquery ,bootstrap jq代码如下: function pCalculator(amount,term,rating,repayway){ var zhong ...
- Manage Metadata Service Error: There are no addresses available for this application
打开正常创建的Metadata Service后发现了如下的错误: 检查了Application Pool和Managed Metadata Web Service ,发现两者一切正常,之后查看Sh ...