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 或者 - ...
随机推荐
- (转)使用Spring配置文件实现事务管理
http://blog.csdn.net/yerenyuan_pku/article/details/52886207 前面我们讲解了使用Spring注解方式来管理事务,现在我们就来学习使用Sprin ...
- IIR数字滤波器
对于N阶IIR的计算方程式为: 一阶 Y(n)=a∗X(n)+(1−a)∗Y(n−1) 二阶 y[n]=b0⋅x[n]+b1⋅x[n−1]+b2⋅x[n−2]−a1⋅y[n−1]−a2⋅y[n−2]
- Calling method 'get' is not valid without an active transaction
在进行使用注解来配置Spring和Hibernate的整合的时候, 遇到了这个问题, 它的意思是说在调用'get'方法的时候,没有活动的事务. 原因分析: Hibernate强制要求在进行数据库操作的 ...
- P1357 花园 (矩阵快速幂+ DP)
题意:一个只含字母C和P的环形串 求长度为n且每m个连续字符不含有超过k个C的方案数 m <= 5 n <= 1e15 题解:用一个m位二进制表示状态 转移很好想 但是这个题是用矩阵快速 ...
- flash player vista or win7
win10 or 8 firefox https://fpdownload.adobe.com/get/flashplayer/pdc/31.0.0.122/install_flash_player. ...
- upupoo(网页壁纸)自主修改一:农历
最近在使用一款upupoo的壁纸软件,感觉还可以,主要是对其中html可设置为壁纸方面情有独钟 前几天在它的网页区发现了一个壁纸,感觉挺好: 感觉内容有点少,今天在工作空余时间就在其中加上了农历,同时 ...
- mysql语句中判断是否包含某字符串的方法
当我们需要对数据做筛选和查询的时候,往往会涉及到一些限制条件的判断,今天就分享一个判断字符串的技巧. like 相信大家对like的用法肯定都很熟悉了,它可以匹配字段以某字符串开始,以某字符串结尾,包 ...
- selenium——操作滚动条
在自动化测试的过程中,难免会应用到翻页键,但是webdriver提供的方法都是操作当前页面可见的元素,对于未在当前范围展示的翻页键,该如何操作呢? 小编在这里介绍一种方法:使用JavaScript操作 ...
- Docker客户端连接Docker Daemon的方式
Docker为C/S架构,服务端为docker daemon,客户端为docker.service,支持本地unix socket域套接字通信与远程socket通信. 默认为本地unix socket ...
- ssm+activiti+maven
1spring整合activiti中添加activiti依赖 <!-- 添加Activiti支持 --> <dependency> <groupId>org.act ...