bzoj1594 Pku3764 The xor-longest Path
先求每个点到根的异或和
然后就要找出两个点,使dis[a]^dis[b]最大
注意异或的性质,我们可以用trie树,沿着与当前数字每位的相反方向走
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<map>
#include<set>
#define rre(i,r,l) for(int i=(r);i>=(l);i--)
#define re(i,l,r) for(int i=(l);i<=(r);i++)
#define Clear(a,b) memset(a,b,sizeof(a))
#define inout(x) printf("%d",(x))
#define douin(x) scanf("%lf",&x)
#define strin(x) scanf("%s",(x))
#define LLin(x) scanf("%lld",&x)
#define op operator
#define CSC main
typedef unsigned long long ULL;
typedef const int cint;
typedef long long LL;
using namespace std;
void inin(int &ret)
{
ret=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=;ch=getchar();}
while(ch>=''&&ch<='')ret*=,ret+=ch-'',ch=getchar();
ret=f?-ret:ret;
}
int n,head[],next[],zhi[],w[],ed,dis[];
int ch[][],eed,po[];
void add(int a,int b,int c)
{
next[++ed]=head[a],head[a]=ed,zhi[ed]=b,w[ed]=c;
next[++ed]=head[b],head[b]=ed,zhi[ed]=a,w[ed]=c;
}
void dfs(int x,int fa)
{
for(int i=head[x];i;i=next[i])
if(zhi[i]!=fa)
{
dis[zhi[i]]=dis[x]^w[i];
dfs(zhi[i],x);
}
}
void add(int x)
{
int now=;
rre(i,,)
{
int temp=x&po[i];
if(temp)temp=;
if(!ch[now][temp])ch[now][temp]=++eed;
now=ch[now][temp];
}
}
int ans=;
void query(int x)
{
int now=,xx=;
rre(i,,)
{
int temp=x&po[i];
if(temp)temp=;
if(ch[now][temp^])now=ch[now][temp^],xx|=po[i];
else now=ch[now][temp];
}
ans=max(ans,xx);
}
int CSC()
{
inin(n);po[]=;re(i,,)po[i]=po[i-]<<;
re(i,,n)
{
int q,w,e;
inin(q),inin(w),inin(e);
add(q,w,e);
}
dfs(,);
re(i,,n)
add(dis[i]);
re(i,,n)query(dis[i]);
printf("%d",ans);
return ;
}
bzoj1594 Pku3764 The xor-longest Path的更多相关文章
- 题解 bzoj1954【Pku3764 The xor – longest Path】
做该题之前,至少要先会做这道题. 记 \(d[u]\) 表示 \(1\) 到 \(u\) 简单路径的异或和,该数组可以通过一次遍历求得. \(~\) 考虑 \(u\) 到 \(v\) 简单路径的异或和 ...
- poj3764 The XOR Longest Path【dfs】【Trie树】
The xor-longest Path Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10038 Accepted: ...
- 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 ...
随机推荐
- OC侧滑删除
做侧滑的时候发现一个问题,当一个UITableView的cell有的有侧滑,有的没有,当用editActionsForRowAtIndexPath方法的时候发现有点问题,查看了下api,需要用到can ...
- 011-docker-安装-rabbitmq-management:3.7.13
1.搜索镜像 docker search rabbitmq 2.拉取合适镜像 选择合适tag:https://hub.docker.com/,下载3.7.13 带web管理界面版本 docker pu ...
- php5.6+Redis+Windows7安装 (phpstudy)
Windows下为PHP安装redis扩展 1.使用phpinfo()函数查看PHP的版本信息,这会决定扩展文件版本. 2.下载php_igbinary-2.0.1-7.0-ts-vc14-x64.z ...
- python的目录
1.python的当前目录 d = os.path.dirname(__file__) #和文件强依赖,即使该语句被别的文件调用,d也不会改变或者d = os.getcwd() #当该语句被别的文件调 ...
- awesome vue
https://blog.csdn.net/caijunfen/article/details/78216868
- spring boot 启动类一定要放置到包的根目录下,也就是和所有包含java文件的包在同一级目录。如果不放置在根目录下,将会提示 no mybatis mapper was found
spring boot 启动类一定要放置到包的根目录下,也就是和所有包含java文件的包在同一级目录.将会将同一目录下的包扫描成bean. 如果不放置在根目录下,将会提示 no mybatis map ...
- spark提交任务的三种的方法
在学习Spark过程中,资料中介绍的提交Spark Job的方式主要有三种: 第一种: 通过命令行的方式提交Job,使用spark 自带的spark-submit工具提交,官网和大多数参考资料都是已这 ...
- MYSQL: set names utf8是什么意思?
set names utf8 是用于设置编码,可以再在建数据库的时候设置,也可以在创建表的时候设置,或只是对部分字段进行设置,而且在设置编码的时候,这些地方最好是一致的,这样能最大程度上避免数据记录出 ...
- git的reset的理解
git的reset的理解 1.在理解reset命令之前,先对git中涉及到的与该reset命令相关概念进行说明和解释HEAD这是当前分支版本顶端的别名,也就是在当前分支你最近的一个提交Indexind ...
- 不同平台windows、linux、mac 上换行符的问题
http://blog.chinaunix.net/uid-26748613-id-3179595.html?page=2 https://blog.csdn.net/changruihe/artic ...