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 ...
随机推荐
- idea中如何添加RunDashboard
在微服务开发中,往往要同时启动多个服务,这时候使用Run控制台难免会出错,并且不方便管理,这里推荐一个功能Run Dashboard idea中打开Run Dashboard的方法如下 view &g ...
- python之路(9)反射、包装类、动态模块导入
目录 反射 利用继承二次包装标准类 利用授权二次包装标准类 动态模块导入 反射 python提供自省的四个方法: hasattr(object,name) 判断object中有没有有个name字符串 ...
- django-个人博客登录及权限验证功能的实现
完成注册后随即开始进行登录,登录后页面显示登录者的名称 实现如下: 前端页面html,对session进行判断,有值则显示登录者的名字 ,无值则显示注册字样: 后台views函数 首先对验证码进行验 ...
- 线段树——习题、lazy解析
习题: C. Cloud Computing lazy操作解析:
- CTypes
参考:http://docs.pythontab.com/interpy/c_extensions/ctypes/ Python中的ctypes模块可能是Python调用C方法中最简单的一种.ctyp ...
- CAD快捷键
F1: 获取帮助 F2: 实现作图窗和文本窗口的切换 F3: 控制是否实现对象自动捕捉 F4:数字化仪控制 F5: 等轴测平面切换 F6: 控制状态行上坐标的显示方式 F7: 栅格显示模式控制 F8: ...
- Nodejs一键实现微信内打开网页url自动跳转外部浏览器访问的功能
前言 现如今微信对第三方推广链接的审核是越来越严格了,域名在微信中分享转发经常会被拦截,一旦被拦截用户就只能复制链接手动打开浏览器粘贴才能访问,要不然就是换个域名再推,周而复始.无论是哪一种情况都会面 ...
- 2018-2019-2 20165234 《网络对抗技术》 Exp5 MSF基础应用
实验五 MSF基础应用 实验内容 本实践目标是掌握metasploit的基本应用方式,重点常用的三种攻击方式的思路.具体需要完成: 1.一个主动攻击实践,ms08_067(成功) 2. 一个针对浏览器 ...
- UGUI打字机效果文本组件
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; ...
- 基于python的WGS84转百度坐标
from urllib.request import urlopen, quote import json def wgs84tobaidu(x,y): data=str(x)+','+str(y); ...