codechef Polo the Penguin and the Tree
一般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的更多相关文章
- 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 ...
- A. Polo the Penguin and Strings
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- [codechef July Challenge 2017] Pishty and tree
PSHTTR: Pishty 和城堡题目描述Pishty 是生活在胡斯特市的一个小男孩.胡斯特是胡克兰境内的一个古城,以其中世纪风格的古堡和非常聪明的熊闻名全国.胡斯特的镇城之宝是就是这么一座古堡,历 ...
- CodeForces 288C Polo the Penguin and XOR operation (位运算,异或)
题意:给一个数 n,让你求一个排列,使得这个排列与0-n的对应数的异或之最大. 析:既然是异或就得考虑异或的用法,然后想怎么才是最大呢,如果两个数二进制数正好互补,不就最大了么,比如,一个数是100, ...
- CodeForces 288B Polo the Penguin and Houses (暴力或都快速幂)
题意:给定 n 和k,n 表示有n个房子,然后每个有一个编号,一只鹅要从一个房间中开始走,下一站就是房间的编号,现在要你求出有多少种方法编号并满足下面的要求: 1.如果从1-k房间开始走,一定能直到 ...
- CodeForces 288A Polo the Penguin and Strings (水题)
题意:给定一个字符,让你用前 k 个字符把它排成 n 长度,相邻的字符不能相等,并且把字典序最小. 析:其实很简单么,我们只要多循环ab,就行,最后再把剩下的放上,要注意k为1的时候. 代码如下: # ...
- CodeForces 289B Polo the Penguin and Matrix (数学,中位数)
题意:给定 n * m 个数,然后每次只能把其中一个数减少d, 问你能不能最后所有的数相等. 析:很简单么,首先这个矩阵没什么用,用一维的存,然后找那个中位数即可,如果所有的数减去中位数,都能整除d, ...
- CodeForces 289A Polo the Penguin and Segments (水题)
题意:给你 n 段区间,而且还是不相交的,然后你只能向左扩展左端点,或者向右扩展右端点,然后扩展最少的步数让整数总数能够整除 k. 析:很简单么,只要在记录算一下数量,然后再算出 k 的倍数差多少就行 ...
- codeforces B. Polo the Penguin and Matrix 解题报告
题目链接:http://codeforces.com/problemset/problem/289/B 题目意思:给出一个 n 行 m 列的矩阵和数值 d .通过对矩阵里面的数进行 + d 或者 - ...
随机推荐
- HDU 4465 Candy (数学期望)
题意:有两个盒子各有n个糖(n<=2*105),每天随机选1个(概率分别为p,1-p),然后吃掉一颗糖.直到有一天打开盒子一看,这个盒子没有糖了.输入n,p,求此时另一个盒子里糖的个数的数学期望 ...
- 51nod 1267 4个数和为0
基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 给出N个整数,你来判断一下是否能够选出4个数,他们的和为0,可以则输出"Yes",否则输出&qu ...
- 程序员面试系列之Java单例模式的攻击与防御
我写的程序员面试系列 Java面试系列-webapp文件夹和WebContent文件夹的区别? 程序员面试系列:Spring MVC能响应HTTP请求的原因? Java程序员面试系列-什么是Java ...
- 暑假集训 || LCA && RMQ
LCA定义为对于一颗树 树上两个点的最近公共祖先 一.Tarjan求LCA(离线方法 https://blog.csdn.net/lw277232240/article/details/7701751 ...
- ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath
问题: ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the ...
- js模块化入门与commonjs解析与应用
JS模块化的基本原理 commonjs规范 commonjs在前端模块化中的基本使用 AMD与CMD规范剖析博客链接 一.JS模块化基本原理 在JS没有提出来模块化的时候,开发JS项目比较简单,同时也 ...
- 异常:java.lang.NoClassDefFoundError: org/springframework/expression/ParserContext
异常:java.lang.NoClassDefFoundError: org/springframework/expression/ParserContext 解决方案:缺少spring-expres ...
- struts2中关于jsp页面向action传值出现乱码问题
在JSP页面输入中文并传给后台的时候,常常会出现乱码问题,产生乱码的原因:java在进行传值的时候,默认用的是iso-8859-1的编码形式进行传输,而我们jsp页面常用的则是utf-8的编码形式.所 ...
- tab bar controller
下面记一下怎样通过代码的方式为选项卡添加视图. 1.创建一个基于Empty Application的项目 2.创建两个新类,基类选择UIViewController,勾选With XIB for us ...
- PHP中的预定义常量、类常量和魔术常量的区别
PHP 向它运行的任何脚本提供了大量的预定义常量.不过很多常量都是由不同的扩展库定义的,只有在加载了这些扩展库时才会出现,或者动态加载后,或者在编译时已经包括进去了. 对于一些基本的常量是这些常量在 ...