爆炸入口

给定一颗带权值的,节点数为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. 《mac的git安装手册-1》

    <mac的git安装手册-1> 下载地址 https://git-scm.com/downloads 如果遇到上面这个问题打开系统偏好设置: OK,这样就能安装了

  2. 安装NetCDF及HDF5

    平台信息 Description: CentOS Linux release 7.6.1810 (Core) 安装步骤 下载NetCDF.HDF5.zlib.curl[使用wget命令即可] 解包:t ...

  3. jsp中判断if 和 else

    <c:choose>                   <c:when test="${date==datetime}">                ...

  4. 使用Dockerfile docker tomcat部署

    在百度上试很多文章都不行,只有这篇可以. 宿主机为:centos64位 //安装docker 1:yum install docker //启动docker 2:systemctl start  do ...

  5. [转]win系统下nodejs安装及环境配置

    本文转自:http://www.cnblogs.com/linjiqin/p/3765390.html 第一步:下载安装文件 下载nodejs,官网:http://nodejs.org/downloa ...

  6. web service, wcf, wcf rest, web api之间的区别

    在.NET Framework中,有很多种技术可以创建基于http协议的服务,譬如说web service, wcf,wcf rest和web api等等.网上有很多的文章教我们如何开发.使用这几种技 ...

  7. Kudu的配置(官网推荐的步骤)(Configuring Apache Kudu)

    不多说,直接上干货! http://kudu.apache.org/docs/configuration.html

  8. 案例52-crm练习新增客户中加入文件上传功能(struts2文件上传)

    1 jsp/customer/add.jsp 完整代码: <%@ page language="java" contentType="text/html; char ...

  9. checkbox判断选中的三种方法

    方法一: if ($("#checkbox-id")get(0).checked) {     // do something } 方法二: if($('#checkbox-id' ...

  10. This blog Test the Open Live Writer

    1. We print HELLOWORLD when we first learned to code, I want to Write this blog to test the software ...