题目链接

先求每个点到根的异或和

然后就要找出两个点,使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的更多相关文章

  1. 题解 bzoj1954【Pku3764 The xor – longest Path】

    做该题之前,至少要先会做这道题. 记 \(d[u]\) 表示 \(1\) 到 \(u\) 简单路径的异或和,该数组可以通过一次遍历求得. \(~\) 考虑 \(u\) 到 \(v\) 简单路径的异或和 ...

  2. poj3764 The XOR Longest Path【dfs】【Trie树】

    The xor-longest Path Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10038   Accepted:  ...

  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 ...

随机推荐

  1. Linux系统上传文件与下载文件命令

    我们用的服务器都是Linux系统的,如果用的是远程服务器,就需要将我们的代码推送过去,这里可以用到PSCP命令. (一)上传 pscp 本机文件的路径以及文件名 远程主机的用户名@远程主机IP:想要存 ...

  2. mysql在linux上的安装

    前提: 环境:workstation 11 + CentOS 7 + mysql-5.6.40 安装前先查看服务器里是否有老版本的mysql已经被安装了 rpm -qa|grep mysql 如果有就 ...

  3. linux服务器性能查看

    1.1 cpu性能查看 1.查看物理cpu个数: cat /proc/cpuinfo |grep "physical id"|sort|uniq|wc -l 2.查看每个物理cpu ...

  4. Django 框架 数据库操作2

    一.ORM的操作方法总结 get(self, *args, **kwargs): # 获取单个对象 def one_get(request): #直接得到一个表对象,也就是表记录 如果得到多个会报错 ...

  5. kfrobotaidlog查找

    ======查看最新的大于6的数量========== select c.questionid from (select a.questionid, b.clientQuestion from sim ...

  6. jenkins集成sonar

    用于我的sonar已经在一台机器上搭建好了,但是每次都要人工去执行sonar-run,很麻烦,所以就想着集成到jenkins上,在jenkins上点点按钮就可以看sonar结果,所以很抱歉,本博客不设 ...

  7. sqli-labs(三)

    第五关:这关的重点是有联合查询的注入漏洞,但是页面不会显示查询信息,但是会有报错信息显示在页面上 这关是双查询注入,其实用报错注入和盲注都是可以注入的,但是我觉得这个双查询注入还是很有意思的,所以这关 ...

  8. UVAL 3942 Remember the Word(递推+Trie)

    Remember the Word [题目链接]Remember the Word [题目类型]递推+Trie &题解: 蓝书P209,参考的别人公开代码 &代码: #include ...

  9. HDU 1568 Fibonacci(大数前4位)

    转载自:http://blog.csdn.net/thearcticocean/article/details/47615241 分析:x=1234567.求其前四位数: log10(x)=log10 ...

  10. python爬虫-基础入门-爬取整个网站《3》

    python爬虫-基础入门-爬取整个网站<3> 描述: 前两章粗略的讲述了python2.python3爬取整个网站,这章节简单的记录一下python2.python3的区别 python ...