一般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. mac homebrew安装

    http://book.51cto.com/art/201107/278761.htm 3.2.3 使用 Homebrew 安装 Git Mac OS X 有好几个包管理器,用于管理一些开源软件在 M ...

  2. 光线步进——RayMarching入门

    入门实现 先用RayMarching描绘一个球体,最后在进行光照计算参考:https://www.shadertoy.com/view/llt3R4 模拟摄像机射线float3 rayDirectio ...

  3. break,continue,return的区别

    break,continue,return的区别 break 当break语句用于循环语句时,会终止执行循环,并执行循环后代码(如果有的话). function main() { for(var i ...

  4. 日常[系统]:Linux新人报到(吐槽%&%……&¥……%

    昨天换了系统,从win7换到了NOIP必须面对的Linux系统. 不得不说,真的很不适应.原本右上角的三个按钮变到了左上角. 可爱的DEVCPP被无情的抛弃了. 又用不惯guide,只好用文本编辑器写 ...

  5. JAVA中等待所有线程都执行结束(转2)

    场景: package com.java4all.mypoint; import java.util.concurrent.CountDownLatch; public class ThreadTes ...

  6. Java垃圾回收之老年代垃圾收集器

    1.Serial Old 收集器(-XX: +UseSerialOldGC, 标记-整理算法) 1.1 单线程收集,进行垃圾收集时,必须暂停所有工作线程 1.2 简单高效,Client模式下默认的老年 ...

  7. CSS中列表项list样式

    CSS列表属性 属性 描述 list-style-属性 用于把所有用于列表的属性设置于一个声明中. list-style-image 将图象设置为列表项标志. list-style-position ...

  8. 分享点干货(此this非彼this)this的详细解读

    在javascript编程中,this关键字经常让初学者感到迷惑,这里,针对此this根据查阅的资料和个人的理解分享一下,纯手打,大神勿喷. 首先先说一下this的指向,大致可以分为以下四种. 1.作 ...

  9. minGW64编译Qt

    1.安装minGW64,设置bin目录到环境变量Path 2.cmd 到qt的Src目录 3.configure -debug-and-release -opensource -prefix &quo ...

  10. OpenJudge-百练-2755

    这道题用递归写的话还是很好写的,我们设递归函数的名称为Ways(w,k) . 它的含义就是,w的大小,取k个物品,有多少种方式. 我们可以知道递归的边界条件就是当w的大小为0的时候,我们的方法数只有一 ...