题目链接

先求每个点到根的异或和

然后就要找出两个点,使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. 如何卸载VMware虚拟机?

    如何卸载VMware虚拟机? 1.windows + R  打开>运行-->regedit(打开编辑注册表)-->找到HKEY_LOCAL_MACHINE-->Software ...

  2. 002-linux使用SecureCRT

    一.ssh 登录linux系统 1.确定网络连通性:ping www.jd.com 2.rpm -qa | grep ssh 可以看到系统中ssh安装包 查找当前系统是否已经安装 注:如果没有安装SS ...

  3. React篇-子组件调用父组件方法,并传值

    react 中子组件调用父组件的方法,通过props: 父组件: isNote(data){} <div className="tabC01"> <FTab ta ...

  4. axios封装异步获取数据的函数以及全局拦截

    https://www.cnblogs.com/momozjm/p/7273272.html https://www.jianshu.com/p/9a1b99cd2ba6

  5. Centos7安装Openvpn

    前言 搭建openvpn主要参考这篇博客,原文传送: http://www.jianshu.com/p/4bbf946222d5 所以你会发现步骤基本一样. 安装openvpn A:安装EPEL仓库 ...

  6. tf中计算图 执行流程学习【转载】

    转自:https://blog.csdn.net/dcrmg/article/details/79028003 https://blog.csdn.net/qian99/article/details ...

  7. 回归评价指标MSE、RMSE、MAE、R-Squared

    分类问题的评价指标是准确率,那么回归算法的评价指标就是MSE,RMSE,MAE.R-Squared. MSE和MAE适用于误差相对明显的时候,大的误差也有比较高的权重,RMSE则是针对误差不是很明显的 ...

  8. [pat]A1072 Gas Station

    这道题的结点编号是字符串类型,处理的过程很有意思,用getID将house和GasStation进行区分 #include<bits/stdc++.h> using namespace s ...

  9. MSSqlServer 发布/订阅配置(主从同步)

    背景: 1.单个独立数据库的吞吐量是有瓶颈的,那么如何解决这个瓶颈? 2.服务器直接数据如何复制.并具备一致性.可扩展性? 资源: 官方资源:https://technet.microsoft.com ...

  10. vue中把table导出表格excel

    1.首先下载2个js,我的百度网盘有 2.安装依赖 npm install -S file-saver xlsx(这里其实安装了2个依赖) npm install -D script-loader 3 ...