题目链接

先求每个点到根的异或和

然后就要找出两个点,使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. Java Swing界面编程(18)---单行文本输入组件:JTextField

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/xuejiawei123/article/details/27565407 下面的程序与上一例有一点差 ...

  2. 20170915 linux系统管理培训

    进程管理 程序:通常为二进制程序放在存储媒介中(如光盘.硬盘.软盘.磁带等),以物理文件的形式存在: 进程:正在运行当中的程序,程序被触发后,执行者的权限与属性.程序的程序代码与所有数据等都会被加载到 ...

  3. vue接口地址配一个全局的

    main.js加 if (process.env.NODE_ENV !== 'development') { Vue.prototype.URL_PREFIX = 'http://139.196.7. ...

  4. [vue]通过watch实现数据双向绑定

    modal:单向绑定 <head> <meta charset="UTF-8"> <title>test</title> <s ...

  5. Python复习基础篇

    4.4 使用列表的一部分   Python切片(切片就是取值的呗) print(players[0:3])  中括号,冒号隔开,顾头不顾尾. print([:4])    从开始取还是会顾尾的 pri ...

  6. Laravel中路由怎么写(一)

    1.路由基本使用示例 1.1 默认示例 Laravel中所有路由定义在/app/Http/routes.php文件中,该文件默认定义了应用的首页路由: Route::get('/', function ...

  7. python中参数传递之位置传递、关键字传递、包裹传递与解包裹

    原文地址https://blog.csdn.net/love666666shen/article/details/77131487 1.位置与关键字传递 (1)位置传递:先用形式参数定义,然后在调用时 ...

  8. mac控制台快捷键

    ctrl+a //移到行首ctrl+e //移到行尾 ctrl+y // 插入最近删除的单词或语句ctrl+k //删除光标处到行尾部分ctrl+u //删除光标处到行首部分ctrl+w //删除光标 ...

  9. django基础(一)

    一.创建django程序 1.终端:django-admin startproject sitename 2.IDE创建Django程序时,本质上都是自动执行上述命令 常用命令: python man ...

  10. clear/reset select2,重置select2,恢复默认

    4.0 version //方法一$('#yourButton').on('click', function() { $('#yourfirstSelect2').val(null).trigger( ...