一般xor 的题目都是用trie解决。

那这道题是在树上的trie;

首先:从root==1,遍历树得到1到所有节点的xor 值。

然后对于每个点我们把其插入二进制树中。

对于每一个点查找其二进值异或值最大的数 依次遍历下来。

注意:边的数量开两倍以上,RE很多次。

find函数具体是这样的:

对于一个书二进值:10001000101

 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<string>
#include<cstring>
#include<set>
#include<map>
#include<stdlib.h> #define N 223456
using namespace std;
struct edge
{
int v,w,next;
}e[N];
int tot,nid;
int head[N],ans[N];
int next[N*][];
void add(int u,int v,int w)
{
e[tot].v=v;
e[tot].w=w;
e[tot].next=head[u];
head[u]=tot++;
} void dfs(int u,int fa)
{
for (int i=head[u];i!=-;i=e[i].next)
{
int v=e[i].v;
if (v==fa) continue;
ans[v]=ans[u]^e[i].w;
dfs(v,u);
}
} void insert(int node,int d,int val)
{
if (d==) return;
int p=-d;
int c=(val&(<<p)) ? :; if (next[node][c]==-) next[node][c]=++nid;
insert(next[node][c],d+,val);
} int solve(int node,int d,int val)
{
if (d==) return ;
int p=-d;
int c=(val&(<<p))?:; if (next[node][c]!=-)
return (<<p)+solve(next[node][c],d+,val); return solve(next[node][!c],d+,val);
} int main()
{
int T;
scanf("%d",&T);
while (T--)
{
int n;
scanf("%d",&n);
memset(head,-,sizeof(head));
memset(next,-,sizeof(next));
memset(ans,,sizeof(ans));
tot=nid=;
for (int i=;i<n;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
} dfs(,-);
for (int i=;i<=n;i++) insert(,,ans[i]);
int tmp=; for (int i=;i<=n;i++)
tmp=max(tmp,solve(,,ans[i]));
printf("%d\n",tmp);
}
return ;
}

我们先要判断           01110111010

存在否,这样才能达到最大值

codechef Polo the Penguin and the Tree的更多相关文章

  1. codeforces 288A:Polo the Penguin and Strings

    Description Little penguin Polo adores strings. But most of all he adores strings of length n. One d ...

  2. A. Polo the Penguin and Strings

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  3. [codechef July Challenge 2017] Pishty and tree

    PSHTTR: Pishty 和城堡题目描述Pishty 是生活在胡斯特市的一个小男孩.胡斯特是胡克兰境内的一个古城,以其中世纪风格的古堡和非常聪明的熊闻名全国.胡斯特的镇城之宝是就是这么一座古堡,历 ...

  4. CodeForces 288C Polo the Penguin and XOR operation (位运算,异或)

    题意:给一个数 n,让你求一个排列,使得这个排列与0-n的对应数的异或之最大. 析:既然是异或就得考虑异或的用法,然后想怎么才是最大呢,如果两个数二进制数正好互补,不就最大了么,比如,一个数是100, ...

  5. CodeForces 288B Polo the Penguin and Houses (暴力或都快速幂)

    题意:给定 n 和k,n 表示有n个房子,然后每个有一个编号,一只鹅要从一个房间中开始走,下一站就是房间的编号,现在要你求出有多少种方法编号并满足下面的要求: 1.如果从1-k房间开始走,一定能直到 ...

  6. CodeForces 288A Polo the Penguin and Strings (水题)

    题意:给定一个字符,让你用前 k 个字符把它排成 n 长度,相邻的字符不能相等,并且把字典序最小. 析:其实很简单么,我们只要多循环ab,就行,最后再把剩下的放上,要注意k为1的时候. 代码如下: # ...

  7. CodeForces 289B Polo the Penguin and Matrix (数学,中位数)

    题意:给定 n * m 个数,然后每次只能把其中一个数减少d, 问你能不能最后所有的数相等. 析:很简单么,首先这个矩阵没什么用,用一维的存,然后找那个中位数即可,如果所有的数减去中位数,都能整除d, ...

  8. CodeForces 289A Polo the Penguin and Segments (水题)

    题意:给你 n 段区间,而且还是不相交的,然后你只能向左扩展左端点,或者向右扩展右端点,然后扩展最少的步数让整数总数能够整除 k. 析:很简单么,只要在记录算一下数量,然后再算出 k 的倍数差多少就行 ...

  9. codeforces B. Polo the Penguin and Matrix 解题报告

    题目链接:http://codeforces.com/problemset/problem/289/B 题目意思:给出一个 n 行 m 列的矩阵和数值 d .通过对矩阵里面的数进行 + d 或者 - ...

随机推荐

  1. php 批量依照ID建立 文件

    <?php // 登录验证 include_once('inc/conn.php'); // sql查询 $sql="SELECT * FROM zcgl ";// $res ...

  2. centos7 搭建双网卡bond1(主备模式)实例

    前景须知: 在redhat6 中网卡叫bond,在redhat7及centos7中改名team,此处只记录centos7中双网卡主备搭建过程. 应用情景:实现网络的高可用,防止一条网线或交换机故障影响 ...

  3. 原生JS--各种兼容性写法总结

    <script> var oEvent = evt || event; ========================================================== ...

  4. tomcat https协议

    一.tomcat证书 JDK自带的keytool工具来生成证书 1. 在jdk的安装目录\bin\keytool.exe下打开keytool.exe 2. 在命令行中输入以下命令: keytool - ...

  5. vue-gemini-scrollbar(vue组件-自定义滚动条)

    vue-gemini-scrollbar(vue组件-自定义滚动条) https://segmentfault.com/a/1190000013338560

  6. autoOpenBrowser: true, 运行npm后自动打开浏览器

    autoOpenBrowser: true, 运行npm后自动打开浏览器

  7. Unity整合Asp.Net MVC

    先来看一下我们的解决方案 我们建立Yubay.Models项目, using System; using System.Collections.Generic; using System.Data.E ...

  8. 日常:论我的T3是如何苟掉的

    说起来都是泪啊 T3的bfs一直不对,我想死. 考试时候苟了两个小时,死活找不到错误(因为lca和离散化都码出来了,太乱) 最后把bfs单独提出来,发现在bfs里面输出的f[ly][0]都是正确的,到 ...

  9. docker 阿里云镜像加速器

    传送门:阿里云镜像地址 Ubuntu/CentOS 安装/升级你的Docker客户端 推荐安装1..0以上版本的Docker客户端,参考文档 docker-ce 如何配置镜像加速器 针对Docker客 ...

  10. 关键css

    参考: 掘金-JS和CSS的位置对资源加载顺序的影响 起舞-什么是关键CSS 有兴趣也可以看看这里一篇关于页面加载的文章.以上掘金那篇文章说css的加载不会影响其他资源的下载,但是我测试了一下,发现是 ...