poj3764字典树路径最大异或和
2024-08-26 23:10:17
The xor-longest Path
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 6853 | Accepted: 1464 |
Description
In an edge-weighted tree, the xor-length of a path p is defined as the xor sum of the weights of edges on p:
⊕ is the xor operator.
We say a path the xor-longest path if it has the largest xor-length. Given an edge-weighted tree with n nodes, can you find the xor-longest path?
Input
The input contains several test cases. The first line of each test case contains an integer n(1<=n<=100000), The following n-1 lines each contains three integers u(0 <= u < n),v(0 <= v < n),w(0 <= w < 2^31), which means there is an edge between node u and v of length w.
Output
For each test case output the xor-length of the xor-longest path.
Sample Input
4
0 1 3
1 2 4
1 3 6
Sample Output
7
Hint
The xor-longest path is 0->1->2, which has length 7 (=3 ⊕ 4)
先求出来到节点1的,然后字典树从高位到低位。异或和的性质以及贪心?。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=2e5+;
int head[N],tot,sz,val[N];
struct node
{
int v,next,w;
} e[N<<];
struct Trie
{
int next[];
void init()
{
memset(next,,sizeof(next));
}
} Ti[N*];
void add(int u,int v,int w)
{
e[tot].v=v;
e[tot].next=head[u];
e[tot].w=w;
head[u]=tot++;
e[tot] .v=u;
e[tot].next=head[v];
e[tot].w=w;
head[v]=tot++;
}
void Add(int x)
{
int u=,ind;
for(int i=; i>=; --i)
{
if((<<i)&x) ind=;
else ind=;
if(!Ti[u].next[ind])
{
Ti[u].next[ind]=++sz;
Ti[sz].init();
}
u=Ti[u].next[ind];
}
}
int get(int x)
{
int u=,num=,ind;
for(int i=; i>=; --i)
{
if((<<i)&x) ind=;
else ind=;
if(Ti[u].next[ind])
{
u=Ti[u].next[ind];
num|=(<<i);
}
else u=Ti[u].next[!ind];
}
return num;
}
void dfs(int x,int v,int fa)
{
val[x]=v;
for(int i=head[x]; i+; i=e[i].next)
{
int to=e[i].v;
if(to==fa) continue;
dfs(to,e[i].w^v,x);
}
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
tot=sz=;
int x,y,z;
memset(head,-,sizeof(head));
for(int i=; i<n; ++i)
{
scanf("%d%d%d",&x,&y,&z);
add(x+,y+,z);
}
Ti[].init(); //必须初始化
dfs(,,);
int maxx=;
for(int i=; i<=n; ++i)
{
maxx=max(maxx,get(val[i]));
Add(val[i]);
}
printf("%d\n",maxx);
}
}
最新文章
- C#基础_单例模式
- 一个国家专利查询demo
- Myeclipse10编写jsp时出现 Multiple annotations found at this line:
- ubuntu14.04恢复系统默认中文字体
- Netty4 自定义Decoder,Encoder进行对象传递
- find the majority element
- JAVA解析XML文件(DOM,SAX,JDOM,DOM4j附代码实现)
- C#中DataGridView 对XML文档的使用
- python学习笔记2_条件循环和其他语句
- Django first lesson 环境搭建
- PC逆向之代码还原技术,第五讲汇编中乘法的代码还原
- 关于JDBC和连接池我学到的(转载保存)
- 第52节:String,权限修饰符,方法,集合
- NPOI 上传Excel功能(三)
- Mining Station on the Sea HDU - 2448(费用流 || 最短路 &;&; hc)
- PHP文件缓存包含三种格式
- Confluence 6 使用 JConsole 监控远程 Confluence
- EntityFramework.Extended 对EF进行扩展
- java 如何用pattern 和 Matcher 来使用正则表达式
- 运放参数的详细解释和分析-part3,输入失调电压Vos及温漂
热门文章
- KVM虚拟化平台环境部署
- RequestDispatcher.forward() 方法和HttpServletResponse.sendRedirect()方法的区别
- POJ1651:Multiplication Puzzle(区间dp)
- Everything 本地磁盘文件搜索工具下载!
- Ubuntu Install Chinese Input Method
- POJ - 2251 Dungeon Master (搜索)
- 题目分享k
- 首次使用AWS服务器EC2
- 网络流 I - Fox And Dinner CodeForces - 510E
- SpringBoot:整合Druid、MyBatis