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 ...
随机推荐
- Magento2与Magento1的区别有哪些
magento2是15年正式上线的正式版,框架和写法跟magento1有很大区别,用到了命名空间和composer,模块化设计更强.因为是刚出生不久 所以bug比较多.目前全世界做magento2的公 ...
- MySQL中drop,delete与truncate的区别
drop直接删掉表 truncate删除表中数据,再插入时自增长id又从1开始 delete删除表中数据,可以加where字句. (1) DELETE语句执行删除的过程是每次从表中删除一行,并且同时将 ...
- 008-ThreadLocal原理分析
一.简介 早在JDK 1.2的版本中就提供java.lang.ThreadLocal,ThreadLocal为解决多线程程序的并发问题提供了一种新的思路.使用这个工具类可以很简洁地编写出优美的多线程程 ...
- Java 基础 常用API (Object类,String类,StringBuffer类)
Java API Java 的API(API: Application(应用) Programming(程序) Interface(接口)) Java API就是JDK中提供给我们使用的类,这些类将底 ...
- docker 在宿主机上根据进程PID查找归属容器ID
在使用docker时经常出现一台docker主机上跑了多个容器,可能其中一个容器里的进程导致了整个宿主机load很高,其实一条命令就可以找出罪魁祸首 #查找容器ID docker inspect -f ...
- sync修饰符的简易说明
其实这个就说的很好了. sync会自动更新父组件的数据 原本valuechild 的值是222,父页面显示的222,把值传递给子组件 子组件也显示的222, 我点击子组件的按钮 把333传递给父组件, ...
- "pip3 install requests"
后续设置参考 “selenium python3” https://www.cnblogs.com/jpr-ok/p/10108231.html
- 函数 return
return 的作用 一.返回一个值给函数,主函数调用这个函数后能得到这个返回的值.二.结束函数,例如你运行到一个地方,虽然后面还有代码但是你不想再继续运行,这时就可以直接用 return:这条语句来 ...
- JAVA编程思想学习笔记2-chap4-6-斗之气2段
1.foreach:只能用于数组与容器 2.this指针:内部有个指针指向自己 3.super指针:内部有个指针指向父类部分 4.方法存放于代码区:方法调用时,a.fun()可能会被转换为fun(a) ...
- vim自动安装插件Vundle
https://github.com/VundleVim/Vundle.vim Set up Vundle: git clone https://github.com/VundleVim/Vundle ...