爆炸入口

给定一颗带权值的,节点数为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. window系统下SVN服务器和客户端的搭建和使用

    SVN服务器下载地址: http://subversion.apache.org/packages.html 这里我选用VisualSVN server 服务端和 TortoiseSVN客户端搭配使用 ...

  2. [转]JS判断访问设备、客户端操作系统类型

    本文转自:http://www.cnblogs.com/duanguyuan/p/3534470.html 先给出一个实例:判断windows.linux.android 复制以下代码另存为html文 ...

  3. Intellij IDEA 14 自动生成 serialVersionUID

    1.  Preferences > Editor > Inspections > Java > Serialization issues > Serializable c ...

  4. ajax的serialize()方法

    自己看吧,超级简单,就不用挨个获取表单名称和值对装在Json里往php传了,直接传个form就可以. [HTML] <form method="post" id=" ...

  5. macOS 使用软件(外加装逼特效)

    macOS 使用软件(外加装逼特效) Backgroud 和 火萤: 动态桌面壁纸 iTools Pro: macOS 版本的爱思助手 MEGAsync: 网盘工具 Microsoft Remote ...

  6. C知识要点-个人总结

    [数据结构]()C 结构体.C 共用体.C 数组.C 指针..... 存储类.auto.register.static.extern C 函数.定义函数.返回类型.函数名称.参数.函数主体.返回语句. ...

  7. vue学习笔记 vue

    目前为止对vue完全懵逼. 对着菜鸟教程,现在我尝试梳理下. 服务我已经启起来.可以看到页面 在src/App.vue里面有展示模板<template></template> ...

  8. 显卡(GPU)的基础知识

    显卡的性能指标有: 流处理器(SP)数量 核心频率 流处理器的架构 显存容量 显存频率 显存带宽 1. 流处理器的数量 把一个GPU当成是一个画画的工厂,其中流处理器的数量就是画师的数量,其数量自然是 ...

  9. angular2-组件样式

    组件样式: :host 选择器 使用:host伪类选择器,用来选择组件宿主元素中的元素(相对于组件模板内部的元素) 这是我们能以宿主元素为目标的唯一方式.除此之外,我们将没办法指定它, 因为宿主不是组 ...

  10. head头部内放些什么标签?

    前言 在学html时,在头部标签内除了知道可以放title标签外,不知道还可以放什么标签,一脸迷茫后赶快去百度,然后小笔记记起来,整理一番. 关键字 <title>  <meta&g ...