Love Live!-01字典树启发式合并
链接:https://ac.nowcoder.com/acm/contest/201/D?&headNav=www
思路:题目要求的是每个等级下的最大 简单路径中的最大异或值,那么我们为了保证目前的路径中最大的权值
为当前访问的边,先进行排序,然后一条一条的插入边,并查集维护 各个联通块,启发式合并,由当前边连接起来的
两个联通块,所谓启发式合并也就是 把小的块 合并到大的上。然后 查询的时候就是再当前 这条边的两个联通块中
找一个包含此边的 最大异或路径, 为了方便处理 我们可以把每个点 存储一个到 它并查集根节点的 异或值,
这样进行 0 1 字典树维护即可。
#include<bits/stdc++.h>
using namespace std;
#define maxn 234567
vector<int>gra[maxn];
int tree[maxn*100][3],ans[maxn];
int n,m,u,v,w,root[maxn],tx,ty;
int fa[maxn],val[maxn],tot,sz[maxn];
struct node
{
int x,y,w;
bool operator<(const node &b)const
{
return w<b.w;
}
} edge[maxn];
int fond(int x)
{
return x==fa[x]?x:fa[x]=fond(fa[x]);
}
void updata(int x,int ad)
{
for(int i=20; i>=0; i--)
{
int id=((1<<i)&ad)?1:0;
if(!tree[x][id])
tree[x][id]=++tot;
x=tree[x][id];
}
}
int query(int x,int ad)
{
int ret=0;
for(int i=20; i>=0; i--)
{
int id=((1<<i)&ad)?1:0;
if(tree[x][id^1])
{
x=tree[x][id^1];
ret|=(1<<i);
}
else x=tree[x][id];
}
return ret;
}
int main()
{
scanf("%d",&n);
for(int i=1; i<n; i++)
{
scanf("%d%d%d",&u,&v,&w);
edge[i]=(node{u,v,w});
}
sort(edge+1,edge+n);
for(int i=1; i<=n; i++)
{
fa[i]=i;
sz[i]=1;
gra[i].push_back(i);
root[i]=++tot;
updata(root[i],0);
}
for(int i=1; i<n; i++)
{
u=edge[i].x,tx=fond(u);
v=edge[i].y,ty=fond(v);
w=(edge[i].w^val[u]^val[v]);
if(sz[tx]>sz[ty])swap(tx,ty),swap(u,v);
for(int j=0; j<sz[tx]; j++)
ans[i]=max(ans[i],query(root[ty],(w^val[gra[tx][j]])));
for(int j=0; j<sz[tx]; j++)
{
val[gra[tx][j]]^=w;
gra[ty].push_back(gra[tx][j]);
updata(root[ty],val[gra[tx][j]]);
}
fa[tx]=ty;
sz[ty]+=sz[tx];
}
for(int i=1; i<n; i++)
{
printf("%d",ans[i]);
if(i!=n-1)printf(" ");
else printf("\n");
}
return 0;
}
Love Live!-01字典树启发式合并的更多相关文章
- 牛客2018国庆集训 DAY1 D Love Live!(01字典树+启发式合并)
牛客2018国庆集训 DAY1 D Love Live!(01字典树+启发式合并) 题意:给你一颗树,要求找出简单路径上最大权值为1~n每个边权对应的最大异或和 题解: 根据异或的性质我们可以得到 \ ...
- HDU6191 Query on A Tree (01字典树+启发式合并)
题意: 给你一棵1e5的有根树,每个节点有点权,1e5个询问(u,x),问你子树u中与x异或最大的值是多少 思路: 自下而上启发式合并01字典树,注意合并时清空trie 线段树.字典树这种结构确定的数 ...
- HDU6191(01字典树启发式合并)
Query on A Tree Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Othe ...
- HDU6191 Query on A Tre【dsu on tree + 01字典树】
Query on A Tree Problem Description Monkey A lives on a tree, he always plays on this tree. One day, ...
- Chip Factory---hdu5536(异或值最大,01字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题意:有一个数组a[], 包含n个数,从n个数中找到三个数使得 (a[i]+a[j])⊕a[k] ...
- Xor Sum---hdu4825(01字典树模板)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4825 题意:有n个数m个查找,每个查找有一个数x, 从序列中找到一个数y,使得x异或y最大 ...
- Codeforces Round #367 (Div. 2)---水题 | dp | 01字典树
A.Beru-taxi 水题:有一个人站在(sx,sy)的位置,有n辆出租车,正向这个人匀速赶来,每个出租车的位置是(xi, yi) 速度是 Vi;求人最少需要等的时间: 单间循环即可: #inclu ...
- hdu5296 01字典树
根据二进制建一棵01字典树,每个节点的答案等于左节点0的个数 * 右节点1的个数 * 2,遍历整棵树就能得到答案. AC代码: #include<cstdio> using namespa ...
- Choosing The Commander CodeForces - 817E (01字典树+思维)
As you might remember from the previous round, Vova is currently playing a strategic game known as R ...
随机推荐
- [面试] Java GC (未整理完)
Java GC简介 什么是 GC ? Java程序不用像C++程序在程序中自行处理内存的回收释放.这是因为Java在JVM虚拟机上增加了垃圾回收(GC)机制,用以在合适的时间触发垃圾回收. 你都了解哪 ...
- Golang入门教程(二)Ubuntu16.04下安装golang(实例:Golang 定时任务管理器)
通过两种方式安装 一.通过apt-get安装1.安装 sudo apt-get install golang 2.设置GOPATH变量 GOPATH是扩展库的目录,Go先搜索标准库目录,然后搜索GOP ...
- 堆排序(heap sort)
参考博客:http://bubkoo.com/2014/01/14/sort-algorithm/heap-sort/ 1.二叉树 二叉树的第 i 层至多有 2i-1 个结点:深度为 k 的二叉树至多 ...
- [Reinforcement Learning] Policy Gradient Methods
上一篇博文的内容整理了我们如何去近似价值函数或者是动作价值函数的方法: \[ V_{\theta}(s)\approx V^{\pi}(s) \\ Q_{\theta}(s)\approx Q^{\p ...
- Swift 4 放大镜功能实现
先上效果图吧,框框被限制在了image内了. 这个feature我把它用在了我的app里了,博客写得不容易,来star下啦
- Python编程四大神兽:迭代器、生成器、闭包和装饰器
生成器 生成器是生成一个值的特殊函数,它具有这样一个特点:第一次执行该函数时,先从头按顺序执行,在碰到yield关键字时该函数会暂停执行该函数后续的代码,并且返回一个值:在下一次调用该函数执行时,程序 ...
- Bootstrap使用-1
目录 结构: 1. 视图函数 2. 模板 3. 登陆 4. 怎样发生的 添加模板 基础模板提供的block 定制基础模板 结构: $ tree -I "__pycache*|*.pyc|*. ...
- AQS学习笔记之独占锁
作用 参与 共享锁 acquireShared() 和 独占锁 acquire() 的抢锁逻辑, 具体的抢锁逻辑不作实现,只对第一次抢锁未抢到锁线程做处理,第一次抢锁就抢到的线程就不需要遇到AQS了 ...
- Linux设置允许指定端口通过防火墙centos7
开启防火墙 1.systemctl start firewalld.service(开启防火墙) 2.systemctl stop firewalld.service(开启防火墙) 3.service ...
- GX/GZOI2019 day2 解题报告
GX/GZOI2019 day2 解题报告 题目链接 逼死强迫症 旅行者 旧词 t1 逼死强迫症 显然地,记 \(f(i)\) 为长度为 \(i\) 的木板的答案,可得: \(\\\) \[f(i)= ...