Description

给定一棵带边权的树, 选择两条没有公共边的简单路径 (长度可以为 0 0),使得所有在任意一条路径上的边的异或和尽量大。

Solution

题目要求选择的两条路径不能有公共边,但是考虑若两条路径有公共边,公共部分就会被异或掉,所以这个条件就不需要考虑了。
然后O(n2)O(n^2)预处理所有路径的值,丢到一个01Trie中求最大值就行了。

Code

#include<bits/stdc++.h>using namespace std;typedef long long ll;
typedef pair<int, int> PII;
typedef vector<int> VI;
#define For(i , j , k) for (register int i = (j) , i##_end_ = (k) ; i <= i##_end_ ; ++ i)
#define Fordown(i , j , k) for (register int i = (j) , i##_end_ = (k) ; i >= i##_end_ ; -- i)
#define Set(a , b) memset(a , b , sizeof(a))
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define fir first
#define sec second
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
#ifdef hany01
#define debug(...) fprintf(stderr , __VA_ARGS__)
#else
#define debug(...)
#endiftemplate <typename T> inline bool chkmax(T &a , T b) { return a < b ? (a = b , 1) : 0; }
template <typename T> inline bool chkmin(T &a , T b) { return b < a ? (a = b , 1) : 0; }int _ , __;
char c_;
inline int read()
{for (_ = 0 , __ = 1 , c_ = getchar() ; !isdigit(c_) ; c_ = getchar()) if (c_ == '-') __ = -1;for ( ; isdigit(c_) ; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);return _ * __;
}inline void File()
{freopen("a.in" , "r" , stdin);freopen("a.out" , "w" , stdout);
}const int maxn = 2003;int n, fa[maxn], tmp, v[maxn], w[maxn], nex[maxn], beg[maxn], e, cnt, Max, lc[2096], rc[2096], rt;inline void add(int uu, int vv, int ww) { v[++ e] = vv; w[e] = ww; nex[e] = beg[uu]; beg[uu] = e; }inline void insert(int x)
{int t = 0;Fordown(i, 9, 0){if (x & (1 << i)) if (rc[t]) t = rc[t]; else t = rc[t] = ++ cnt;else if (lc[t]) t = lc[t]; else t = lc[t] = ++ cnt;}
}void dfs(int u, int dis, int fa)
{for (int i = beg[u]; i; i = nex[i]){if (v[i] == fa) continue;insert(dis ^ w[i]);dfs(v[i], dis ^ w[i], u);}
}inline bool chkin(int x)
{int t = 0;Fordown(i, 9, 0) if (x & (1 << i)) if (rc[t]) t = rc[t]; else return false;else if (lc[t]) t = lc[t]; else return false;return true;
}inline void check(int x)
{int t = 0, Ans = 0;Fordown(i, 9, 0){if (x & (1 << i)) if (lc[t]) t = lc[t], Ans |= (1 << i); else t = rc[t];else if (rc[t]) t = rc[t], Ans |= (1 << i); else t = lc[t];}chkmax(Max, Ans);
}inline void answer()
{For(i, 0, 1023) if (chkin(i)) check(i);
}void debugg(int u, int now)
{if (!lc[u] && !rc[u]){printf("%d\n", now);return ;}if (lc[u]) debugg(lc[u], now << 1);if (rc[u]) debugg(rc[u], now << 1 | 1);
}int main()
{File();n = read();For(i, 2, n) fa[i] = read() + 1;int tmp;For(i, 2, n) tmp = read(), add(fa[i], i, tmp), add(i, fa[i], tmp);insert(0);for (rt = 1; rt <= n; ++ rt) dfs(rt, 0, 0);answer();printf("%d\n", Max);return 0;
}
//今夜鄜州月,闺中只独看。
//遥怜小儿女,未解忆长安。
//香雾云鬟湿,清辉玉臂寒。
//何时倚虚幌,双照泪痕干!
//--杜甫《月夜》

SRM 719 div2 Hard (01Trie,最大异或和)相关推荐

  1. Topcoder Srm 671 Div2 1000 BearDestroysDiv2

    \(>Topcoder \space Srm \space 671 \space Div2 \space 1000 \space BearDestroysDiv2<\) 题目大意 : 有一 ...

  2. SRM 543 Div2

    决定以后多做一些TC,即使做不了比赛,也要多做一些TC上的题,顺便写一些结题报告什么的.不过像我这种在Div2混的弱菜,也写不出什么高质量的结题报告,而且1000pt的题,我基本都不用看了,尽量把25 ...

  3. topcpder SRM 664 div2 A,B,C BearCheats , BearPlays equalPiles , BearSorts (映射)

    A题,熊孩子测视力,水题,题意就是判断一下两个数对应位不相同的数字有多少个. #include<bits/stdc++.h>using namespace std;class BearCh ...

  4. SRM 542 DIV2

    一道DIV2里面500pt的题目想了3天还是没有想出来,这是什么水平....太菜了,弱爆了,该怎么办呢?怎么样才能成为高手呢? 250pt: 题意:题意很简单,就是说兔子之间的合作问题,每对兔子间有个 ...

  5. 【TopCoder SRM 551 Div2】Solutions

    [250] Beaver Bindu has some colored bricks. Each color is described by an uppercase letter. Bricks o ...

  6. SRM 721 DIV2

    Single Round Match 721 Round 1 - Division II, Level One 代码如下: public class FlightDataRecorder {publi ...

  7. TC SRM 562 div2 B 题

    题意: 给你一个矩形的画布,此画布由'B'和'.'组成,画T次每次画的时候他的左上角的起始点是确定的几位(1,1),(2,2),(3,3)......(T,T); 在画的过程中可能会出现相互覆盖求画完 ...

  8. SRM 533 DIV2

    很长时间没做TC了,再说自己做的也确实相当少,所以不是很熟+思路来的比较慢所以做得不是多么好,只做出了250pt,500pt的思路对还没敲完就结束了.话说TC,CF什么的真的很锻炼人的思维能力可就是老 ...

  9. topcoder SRM 625 DIV2 IncrementingSequence

    由于题目数据量比较小,故可以开辟一个数组存储每个index出现的次数 然后遍历即可 string canItBeDone(int k, vector<int> A){vector<i ...

最新文章

  1. 一文读懂机器学习的常用模型评价指标
  2. 《MySQL 8.0.22执行器源码分析(3.1)关于RowIterator》
  3. 201671010144 2016-2017 《java程序设计》--对象与类!
  4. 云服务器ECS登陆注意点
  5. [BJOI2019]送别——非旋转treap
  6. 个人“乱七八糟”笔记和摘要之二
  7. python和java的区别-python与java区别
  8. Linux下禁止ping最简单的方法
  9. 黑白群晖为Plex添加证书,开启HTTPS访问
  10. Spring下载文件出现:UT010029: Stream is closed
  11. python下载模块的两种方式(模块环境不一致问题解决)
  12. 数据库索引与视图实验
  13. Java 运行环境的安装、配置与运行
  14. pygame模块实现乌龟吃鱼游戏案例
  15. 智慧消防智能化管理系统综合分析
  16. Web前端页面访问权限控制总结
  17. java计算机毕业设计线上教育教学系统源码+数据库+lw文档+系统+部署
  18. 线性回归分析——高尔顿数据集更正版
  19. 风格迁移1-02:Liquid Warping GAN(Impersonator)-源码模型测试-报错解决
  20. 移动端蚂蚁组件(antd-mobile)- 解决日期组件中的语系问题

热门文章

  1. 常规调幅系统matlab结果,matlab课程设计参考题目
  2. 替换单词c语言程序,用c语言完成单词替换
  3. Java程序基础——异常
  4. 4399积分小游戏刷分工具帮助
  5. linux 驱动笔记(一)
  6. 记一次E3-1220V2+HD7450黑苹果安装10.13的过程
  7. 男宝女宝如厕训练有不同,你做对了吗?
  8. 520|测一测你和ta的夫妻相
  9. 关于选项卡的切换——JS实现
  10. iOS 中的编码方式详解(主要讲解Unicode)