B1954 The xor-longest Path
给定一颗带权值的,节点数为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的更多相关文章
- poj3764 The XOR Longest Path【dfs】【Trie树】
The xor-longest Path Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10038 Accepted: ...
- 题解 bzoj1954【Pku3764 The xor – longest Path】
做该题之前,至少要先会做这道题. 记 \(d[u]\) 表示 \(1\) 到 \(u\) 简单路径的异或和,该数组可以通过一次遍历求得. \(~\) 考虑 \(u\) 到 \(v\) 简单路径的异或和 ...
- 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 ...
- 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 ...
- FB面经Prepare: Find Longest Path in a Multi-Tree
给的多叉树, 找这颗树里面最长的路径长度 解法就是在子树里面找最大的两个(或一个,如果只有一个子树的话)高度加起来. 对于每一个treenode, 维护它的最高的高度和第二高的高度,经过该点的最大路径 ...
- SP1437 Longest path in a tree(树的直径)
应该是模板题了吧 定义: 树的直径是指一棵树上相距最远的两个点之间的距离. 方法:我使用的是比较常见的方法:两边dfs,第一遍从任意一个节点开始找出最远的节点x,第二遍从x开始做dfs找到最远节点的距 ...
- Educational DP Contest G - Longest Path (dp,拓扑排序)
题意:给你一张DAG,求图中的最长路径. 题解:用拓扑排序一个点一个点的拿掉,然后dp记录步数即可. 代码: int n,m; int a,b; vector<int> v[N]; int ...
- [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 ...
- [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 ...
- 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 ...
随机推荐
- @ControllerAdvice 拦截异常并统一处理
在spring 3.2中,新增了@ControllerAdvice 注解,可以用于定义@ExceptionHandler.@InitBinder.@ModelAttribute,并应用到所有@Requ ...
- Django-1 简介
1.1 MVC与MTV模型 MVCWeb服务器开发领域里著名的MVC模式,所谓MVC就是把Web应用分为模型(M),控制器(C)和视图(V)三层,他们之间以一种插件式的.松耦合的方式连接在一起,模型负 ...
- liunx ssh
ssh-keygen -t rsa 生成创建公匙 ssh username@ip 连接远程服务器
- 利用Serv-U搭建FTP服务器
以前在学校的时候,学校的整个宿舍楼都是在一个局域网中,经常有人用个人电脑搭个网站或者FTP啊什么的,主要是进行一些影视资源的传播活动.不乏 有些资源充沛的有志青年利用业余时间翻译某岛国影视资源,利用局 ...
- PHP设计原则
Laravel PHP设计模式 定义:将PHP设计成一个固化的模式 面向对象设计原则 内聚度:高内聚,表示一个应用程序的单个单元所负责的任务数量和多样性.内聚与单个类或者单个方法单元相关 耦合度: ...
- 前端如何做好SEO优化
https://www.cnblogs.com/weiyf/p/9511021.html 一:什么是SEO? 搜索引擎优化(Search Engine Optimization),简称SEO.是按照搜 ...
- 前端性能优化-keep-alive
什么是Keep-Alive Keep-Alive是浏览器端和服务器端约定的一种提高传输效率的协议.我先举个例子吧,我现在搬家,有10个箱子,如果我自己来搬的话,每次只能带一个箱子,那么搬到目的地,需要 ...
- intellijidea课程 intellijidea神器使用技巧1-3 idea下载
下载: 下载地址:https://www.jetbrains.com/idea/download/ download==>windows==>ultimate版本(付费版本免费试用30天) ...
- jQuery遍历节点
html代码 <div> <p>武汉长乐教育</p> <span>在哪才能真正学习会PHP技术?</span> <a href=&qu ...
- #include stdio.h(5)
#include <stdio.h> int main() { //1.数组的排序-冒泡排序 /* 1.规则:相邻的两个数据进行比较 2.如果有N个数据,需要选择N-1次参照物来比较, 因 ...