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

那这道题是在树上的trie;

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

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

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

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

find函数具体是这样的:

对于一个书二进值:10001000101

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cmath>
 5 #include<string>
 6 #include<cstring>
 7 #include<set>
 8 #include<map>
 9 #include<stdlib.h>
10
11 #define N  223456
12 using namespace std;
13 struct edge
14 {
15   int v,w,next;
16 }e[N];
17 int tot,nid;
18 int head[N],ans[N];
19 int next[N*30][2];
20 void add(int u,int v,int w)
21 {
22    e[tot].v=v;
23    e[tot].w=w;
24    e[tot].next=head[u];
25    head[u]=tot++;
26 }
27
28 void dfs(int u,int fa)
29 {
30    for (int i=head[u];i!=-1;i=e[i].next)
31    {
32       int v=e[i].v;
33       if (v==fa) continue;
34       ans[v]=ans[u]^e[i].w;
35       dfs(v,u);
36    }
37 }
38
39 void insert(int node,int d,int val)
40 {
41    if (d==30)  return;
42    int p=29-d;
43    int c=(val&(1<<p))  ? 1:0;
44
45    if (next[node][c]==-1) next[node][c]=++nid;
46    insert(next[node][c],d+1,val);
47 }
48
49 int solve(int node,int d,int val)
50 {
51   if (d==30) return 0;
52   int p=29-d;
53   int c=(val&(1<<p))?0:1;
54
55   if (next[node][c]!=-1)
56   return (1<<p)+solve(next[node][c],d+1,val);
57
58   return solve(next[node][!c],d+1,val);
59 }
60
61 int main()
62 {
63   int T;
64   scanf("%d",&T);
65   while (T--)
66   {
67      int n;
68      scanf("%d",&n);
69      memset(head,-1,sizeof(head));
70      memset(next,-1,sizeof(next));
71      memset(ans,0,sizeof(ans));
72      tot=nid=0;
73      for (int i=1;i<n;i++)
74      {
75         int u,v,w;
76         scanf("%d%d%d",&u,&v,&w);
77         add(u,v,w);
78         add(v,u,w);
79      }
80
81      dfs(1,-1);
82      for (int i=1;i<=n;i++) insert(0,0,ans[i]);
83      int tmp=0;
84
85      for (int i=1;i<=n;i++)
86      tmp=max(tmp,solve(0,0,ans[i]));
87      printf("%d\n",tmp);
88      }
89      return 0;
90   }

View Code

我们先要判断           01110111010

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

转载于:https://www.cnblogs.com/forgot93/p/4383735.html

codechef Polo the Penguin and the Tree相关推荐

  1. 289B. Polo the Penguin and Matrix

    B. Polo the Penguin and Matrix:题目 思路:纯暴力 #include <bits/stdc++.h> using namespace std; // #def ...

  2. 【CodeForces - 289E 】Polo the Penguin and XOR operation (数学,异或,贪心)

    题干: Little penguin Polo likes permutations. But most of all he likes permutations of integers from 0 ...

  3. 【CodeForces - 289D】Polo the Penguin and Houses (带标号的无根树,Cayley定理,Prufer编码)

    题干: Little penguin Polo loves his home village. The village has n houses, indexed by integers from 1 ...

  4. 【CodeForces - 289C】Polo the Penguin and Strings (水题,字符串,思维构造,有坑)

    题干: Little penguin Polo adores strings. But most of all he adores strings of length n. One day he wa ...

  5. Polo the Penguin and Matrix

    Little penguin Polo has an n × m matrix, consisting of integers. Let's index the matrix rows from 1 ...

  6. Codeforces Round #177 (Div. 1)C. Polo the Penguin and XOR operation【贪心】

    http://codeforces.com/contest/288/problem/C 按二进制位从大到小进行匹配 #include <cstdio> #include <cstri ...

  7. 【Codeforces】A2组刷题记录(50 / 50)完结

    目录 A1. Counterexample A2. Good Number A3. Dice Tower ★A4. Alyona and Numbers A5. Mountain Scenery rz ...

  8. BZOJ 3221: [Codechef FEB13] Obserbing the tree树上询问( 可持久化线段树 + 树链剖分 )

    树链剖分+可持久化线段树....这个一眼可以看出来, 因为可持久化所以写了标记永久化(否则就是区间修改的线段树的持久化..不会), 结果就写挂了, T得飞起...和管理员拿数据调后才发现= = 做法: ...

  9. Codechef:Path Triples On Tree

    Path Triples On Tree 题意是求树上都不相交或者都相交的路径三元组数量. 发现blog里没什么树形dp题,也没有cc题,所以来丢一道cc上的树形dp题. 比较暴力,比较恶心 #inc ...

最新文章

  1. semilogx 多条曲线_怎么让两个指数在一个坐标,matlab里怎样一个坐标上显示多个曲线,而且横轴要用指数形式的?谢谢...
  2. Android学习笔记之自定义Toast
  3. Gradient descent --梯度下降(to be continued)
  4. Cascade R-CNN的一些记录
  5. Mysql表结构升级_mysql表结构升级时根据字段是否存在执行相应操作
  6. kitten编程猫里的函数定义,函数实现和函数调用原理
  7. [蓝桥杯][2018年第九届真题]调手表-bfs
  8. [js] Number()的存储空间是多大?假如接口返回一个超过最大字节的数字怎么办?
  9. openfoam安装中出现allmake error_如何更新OpenFOAM的版本?
  10. php yaf twig,yaf-example
  11. 常用的echo和cat,这次让我折在了特殊字符丢失问题上
  12. 基站的小区号256变换
  13. 为什么你写了一万小时的代码,却没能成为架构师?| 程序员有话说
  14. input输入效果控制onfocus和onblur事件(转)
  15. PB AcceptText()函数
  16. 未完成的IT路停在回车键 2014年末总结篇
  17. 【软件测试】测试人,我们35岁焦虑怎样破?
  18. 电脑连接蓝牙耳机声音总是断断续续:
  19. 阿里生活物联平台笔记一 app配网
  20. SERC 2013 E Skyscrapers

热门文章

  1. python设置一个初始为0的计数器_python中统计计数的几种方法
  2. httpd 处理模型
  3. java学习笔记④MySql数据库--03/04 DQL查询
  4. Python 开篇及第一个Python程序
  5. C++程序设计语言(特别版) -- 一个桌面计算器
  6. install kinect driver for ARM---38
  7. scrollview 与 listView 的显示不全问题
  8. 代码视图与StoryBoard.Xib文件视图的跳转
  9. 大学四年, 专业心得
  10. 热血江湖战无止境与服务器连接不稳定,《热血江湖》V14.0“战无止境”新版玩不停...