链接: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字典树启发式合并的更多相关文章

  1. 牛客2018国庆集训 DAY1 D Love Live!(01字典树+启发式合并)

    牛客2018国庆集训 DAY1 D Love Live!(01字典树+启发式合并) 题意:给你一颗树,要求找出简单路径上最大权值为1~n每个边权对应的最大异或和 题解: 根据异或的性质我们可以得到 \ ...

  2. HDU6191 Query on A Tree (01字典树+启发式合并)

    题意: 给你一棵1e5的有根树,每个节点有点权,1e5个询问(u,x),问你子树u中与x异或最大的值是多少 思路: 自下而上启发式合并01字典树,注意合并时清空trie 线段树.字典树这种结构确定的数 ...

  3. HDU6191(01字典树启发式合并)

    Query on A Tree Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Othe ...

  4. 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, ...

  5. Chip Factory---hdu5536(异或值最大,01字典树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题意:有一个数组a[], 包含n个数,从n个数中找到三个数使得 (a[i]+a[j])⊕a[k] ...

  6. Xor Sum---hdu4825(01字典树模板)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4825 题意:有n个数m个查找,每个查找有一个数x, 从序列中找到一个数y,使得x异或y最大 ...

  7. Codeforces Round #367 (Div. 2)---水题 | dp | 01字典树

    A.Beru-taxi 水题:有一个人站在(sx,sy)的位置,有n辆出租车,正向这个人匀速赶来,每个出租车的位置是(xi, yi) 速度是 Vi;求人最少需要等的时间: 单间循环即可: #inclu ...

  8. hdu5296 01字典树

    根据二进制建一棵01字典树,每个节点的答案等于左节点0的个数 * 右节点1的个数 * 2,遍历整棵树就能得到答案. AC代码: #include<cstdio> using namespa ...

  9. Choosing The Commander CodeForces - 817E (01字典树+思维)

    As you might remember from the previous round, Vova is currently playing a strategic game known as R ...

随机推荐

  1. STM32的内存管理

    ref:https://www.cnblogs.com/leo0621/p/9977932.html 这里针对STM32F407芯片+1M外部内存的内存管理!(全篇是个人愚见,如果错误,请不吝指出!) ...

  2. Angular记录(4)

    文档资料 箭头函数--MDN:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Functions/Arrow_fun ...

  3. Python高级笔记(二) -- 深拷贝和浅拷贝

    1. 深拷贝 1.1 类型1 注意: d没有改变, 因为d所拷贝的数据没有改变, 而是c往后添加数据. 1.2 类型2: 元组 如果copy.copy拷贝的是元组是深拷贝! 不会进行浅拷贝, 仅仅是指 ...

  4. jquery 中 this 的范围

    jquery 中的 this button 通常会存在  onclick;// input[type='file'] 会存在 onchange 事件 eg:  onclick = " del ...

  5. mix-blend-mode

    mix-blend-mode是一个css3新增的混合color与背景元素颜色的样式,同一个元素的两个颜色不影响. mix-blend-mode: normal;          //正常mix-bl ...

  6. [译]asp-net-core-mvc-ajax-form-requests-using-jquery-unobtrusive

    原文 全文源码 开始项目 项目使用了package.json'文件,添加需要的前端package到项目中.在这我们添加了jquery-ajax-unobstrusive`. { "versi ...

  7. C++编译/运行过程中产生的各种文件

    参考:https://blog.csdn.net/weixin_39609623/article/details/82348576 Windows中的obj,lib,dll,exe文件 lib是和dl ...

  8. win10下安装Cygwin配置gcc编译环境

    首先要说明的是,我个人安装cygwin的用途是为了使用kenlm工具训练通及语言模型. 注:统计语言模型工具有比较多的选择,目前比较好的有srilm以及kenlm,其中kenlm比srilm晚出来,训 ...

  9. Mysql 时间差(年、月、天、时、分、秒)

    SELECT TIME_TO_SEC(TIMEDIFF('2018-09-30 19:38:45', '2018-08-23 10:13:01')) AS DIFF_SECOND1, -- 秒 UNI ...

  10. pwnable.tw applestore

    存储结构 0x804B070链表头 struct _mycart_binlist { int *name; //ebp-0x20 int price; //ebp-0x1c struct _mycar ...