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 或者 - ...
随机推荐
- 从URL输入到页面展示都发生了什么?
总的来说分为以下过程: DNS解析 TCP三次握手 发送HTTP请求 服务器端处理请求并返回HTTP保文 浏览器渲染页面 断开连接:TCP的四次挥手 URL到底是什么? URL(Uniform Res ...
- Linux OpenGL 实践篇-11-shadow
OpenGL 阴影 在三维场景中,为了使场景看起来更加的真实,通常需要为其添加阴影,OpenGL可以使用很多种技术实现阴影,其中有一种非常经典的实现是使用一种叫阴影贴图的实现,在本节中我们将使用阴影贴 ...
- light oj 1336 sigma function
常用的化简方法(高中就常用了): p^(e+1)-1/p-1= [ p^(e+1) -p + (p-1) ]/ (p-1) = p*(p^e-1)/(p-1) + 1 ...
- 原生查找DOM的方法
JS获取DOM元素的方法(8种) 通过ID获取(getElementById) 通过name属性(getElementsByName) 通过标签名(getElementsByTagName) 通过类名 ...
- CNN完成mnist数据集手写数字识别
# coding: utf-8 import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data d ...
- C++学习周记
自开学到现在,原本可谓是对C++一无所知,也通过这几周的学习而渐渐有所了解. 最开始的编程任务虽然简单,但解决过程中却不乏磕绊,由一开始的中英文字符的不注意,到现在对一些函数的运用难免出错,出现bug ...
- 360 Atlas中间件安装及使用
1.下载Atlas wget https://github.com/Qihoo360/Atlas/releases/download/2.2.1/Atlas-2.2.1.el6.x86_64.rpm ...
- linux破解root登录密码,并重置
重启系统后按'e'键,进入编辑模式,在'UTF -8'后空格输入'rd.break'后,按快捷键'Ctrl+X'进入新界面进行编辑,代码如下: switch_root:/# mount -o remo ...
- 2019年,Python工程师必考的6个面试题,Python面试题No5
第1题:Python里面如何实现tuple和list的转换? 函数tuple(seq)可以把所有可迭代的(iterable)序列转换成一个tuple, 元素不变,排序也不变 list转为tuple: ...
- Python 3.52官方文档翻译 http://usyiyi.cn/translate/python_352/library/index.html 必看!
Python 3.52官方文档翻译 http://usyiyi.cn/translate/python_352/library/index.html 觉得好的麻烦点下推荐!谢谢!