爆炸入口

给定一颗带权值的,节点数为n的树,求树上路径最大异或和。

solution: 先dfs将所有点到根的异或和算出来。然后放进tire树中贪心。

#include<cstdio>
#include<iostream>
using namespace std;
const int manx=100010;
int n;
struct node
{
int point;
int nxt;
int val;
};
node line[manx<<1];
int head[manx],tail;
int dis[manx];
int t[manx<<5][2],end;
void add(int a,int b,int c)
{
line[++tail].point=b;
line[tail].val=c;
line[tail].nxt=head[a];
head[a]=tail;
}
void dfs(int now,int f,int sum)
{
dis[now]=sum;
for(int i=head[now];i;i=line[i].nxt)
if(f!=line[i].point)
dfs(line[i].point,now,sum^line[i].val);
return ;
}
void ins(int val)
{
int now=0;
for(int i=(1<<30);i;i>>=1)
{
int nxt= (i&val) ? 1 : 0;
if(!t[now][nxt]) t[now][nxt]=++end;
now=t[now][nxt];
}
return ;
}
int find(int val)
{
int res=0,now=0;
for(int i=(1<<30);i;i>>=1)
{
int nxt=(i&val) ? 0 : 1;
if(t[now][nxt]) now=t[now][nxt],res+=i;
else now=t[now][nxt^1];
}
return res;
}
int main()
{
scanf("%d",&n);
int a,b,c;
for(int i=1;i<n;i++)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c),add(b,a,c);
}
dfs(1,0,0);
for(int i=1;i<=n;i++)
ins(dis[i]);
int ans=0;
for(int i=1;i<=n;i++)
ans=max(ans,find(dis[i]));
printf("%d",ans);
}

B1954 The xor-longest Path的更多相关文章

  1. poj3764 The XOR Longest Path【dfs】【Trie树】

    The xor-longest Path Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10038   Accepted:  ...

  2. 题解 bzoj1954【Pku3764 The xor – longest Path】

    做该题之前,至少要先会做这道题. 记 \(d[u]\) 表示 \(1\) 到 \(u\) 简单路径的异或和,该数组可以通过一次遍历求得. \(~\) 考虑 \(u\) 到 \(v\) 简单路径的异或和 ...

  3. Solve Longest Path Problem in linear time

    We know that the longest path problem for general case belongs to the NP-hard category, so there is ...

  4. Why longest path problem doesn't have optimal substructure?

    We all know that the shortest path problem has optimal substructure. The reasoning is like below: Su ...

  5. FB面经Prepare: Find Longest Path in a Multi-Tree

    给的多叉树, 找这颗树里面最长的路径长度 解法就是在子树里面找最大的两个(或一个,如果只有一个子树的话)高度加起来. 对于每一个treenode, 维护它的最高的高度和第二高的高度,经过该点的最大路径 ...

  6. SP1437 Longest path in a tree(树的直径)

    应该是模板题了吧 定义: 树的直径是指一棵树上相距最远的两个点之间的距离. 方法:我使用的是比较常见的方法:两边dfs,第一遍从任意一个节点开始找出最远的节点x,第二遍从x开始做dfs找到最远节点的距 ...

  7. Educational DP Contest G - Longest Path (dp,拓扑排序)

    题意:给你一张DAG,求图中的最长路径. 题解:用拓扑排序一个点一个点的拿掉,然后dp记录步数即可. 代码: int n,m; int a,b; vector<int> v[N]; int ...

  8. [LeetCode] Longest Univalue Path 最长相同值路径

    Given a binary tree, find the length of the longest path where each node in the path has the same va ...

  9. [Swift]LeetCode687. 最长同值路径 | Longest Univalue Path

    Given a binary tree, find the length of the longest path where each node in the path has the same va ...

  10. Recursion-687. Longest Univalue Path

    Given a binary tree, find the length of the longest path where each node in the path has the same va ...

随机推荐

  1. VMware Workstation 12.5.9 Pro虚拟机软件中文版

    更新为 VMware Workstation 12.5.9 pro版.VMware虚拟机软件无疑是windows系统下最强大好用的虚拟机软件.最新的VMware Workstation 12 Pro ...

  2. 使用vue-router beforEach实现判断用户登录跳转路由筛选功能

    在开发webApp的时候,考虑到用户体验,经常会把不需要调用个人数据的页面设置成游客可以访问,而当用户进入到一些需要个人数据的,例如购物车,个人中心,我的钱包等等,在进行登录的验证判断,如果判断已经登 ...

  3. SparseArray替代HashMap来提高性能

    SparseArray是 Android框架独有的类,在标准的JDK中不存在这个类.它要比 HashMap 节省内存,某些情况下比HashMap性能更好,按照官方问答的解释,主要是因为SparseAr ...

  4. 智能时代的到来,企业APP给企业带来的好处

    智能手机的出现给大家带来了巨大的便捷,无论是,文字沟通,购物点餐,开车导航,查看信息,生活服务,上下班打卡,交付各种费用,娱乐生活等都可以在一部小小的手机上来实现.随着智能手机的不断更新,越来越多的软 ...

  5. inventor安装不了

    AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...

  6. git使用笔记-提高篇

    一.分支.合并 1.合并一个特定提交 a specific commit git cherry-pick commit-id 把commit-id代表的本次提交合并到当前分支,如果有冲突需要解决后,提 ...

  7. windows 7下安装tomcat6 web服务器

    因为项目中要使用Mondrian提供ROLAP应用,而Mondrian是运行在tomcat上的. 一. 软件获取: http://tomcat.apache.org/ 二. 安装步骤: 运行可执行程序 ...

  8. 创建weblogic受管理服务器和安全文件

    启动 admin server Oracle/Middleware/user_projects/domains/base_domain/bin> startWebLogic.sh 1创建受管服务 ...

  9. 使用mspaint查看图片像素

    图片打码服务,要求对图像验证码进行切图,即具体知道像素的位置,网上搜了yhb-chi但下载不方便. 就想着看看mspaint可不可以,将图片拖到mspaint中: 显示了整张图片的像素大小(100X4 ...

  10. HDU 5371——Hotaru's problem——————【manacher处理回文】

    Hotaru's problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...