2016中国大学生程序设计竞赛 - 网络选拔赛 J. Alice and Bob
Alice and Bob
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 147 Accepted Submission(s): 22
The tree consists of n vertices, and vertex 1 is the root of this tree.
There is a number w[i] written on the ith vectex. Alice and Bob want to play a game on a subtree of this tree. (Note that there are only n subtrees, since the tree is rooted.)
Firstly Alice will choose a vertex in this subtree, and Bob must to choose a different vertex in this subtree. (So, Bob knows which vertex Alice chosen.)
At last they will get a result number equals the XOR sum of the number written on the two vertices which they chosen.
But the problem is that Alice wants the result number to be as maximal as possible while Bob wants the result number to be as minimal as possible, and of course they are clever enough.
Now we are interested in the result number, can you tell us?
For each test case:
The first line includes an integer n.
The second line includes n integers w[i], indicating the number written on the ith vertex.
For the next n−1 lines, each line includes two integers u and v, which means an edge in the tree.
The next line includes an integer m, which means the number of our queries.
The next m lines, each line includes an integer u, indicating Alice and Bob play game on the subtree rooted on the vertex u, and we want to know the result number.
1≤n,m≤100000,0≤w[i]≤100000.
Output “Case #k:”(without quotes) one line first, where k means the case number count from 1.
Then output m lines, each line must include the answer of the corresponding query. If Alice and Bob can’t choose two different vertices, output -1 instead.
3
1 2 3
1 2
2 3
3
1
2
3
题意:
给出一颗树,每次有两人在某个子树进行博弈。
每次博弈两人轮流分别选取一个节点,得分为两者选取的节点的权值的xor值。
先手希望得分尽量大,后手希望得分尽量小,问每次博弈的最终得分。
题解:
显然对于一次询问,如果将某个子树的节点权值的01TRIE建出来,那么是可以查询出答案的。
考虑一颗已经建好的01权值Trie,每个节点都可以处理出在这个点向下进行题意所述的博弈的答案。
考虑这个节点某个孩子对这个节点的贡献.
若孩子的size>1,那么如果先手选择走这边(即选择的权值在这一个二进制位上是0或者1),另一个人也会走这边,那么用孩子的答案更新父亲。
若孩子的size为1,那么只能暴力往另一边走,看看xor的最小值是什么,然后更新答案。
这个暴力的对于每个size为1的孩子都要进行,每次代价是O(logn)的。 那么考虑整棵树,如果孩子已经建好了,那么每次用启发式合并,将孩子合并起来。
每次插入一条链,对01Trie的影响可以用上述方法一次更新。
共需要O(nlogn)次的插入。每次插入需要O(logn)的复杂度。
那么每次更新的复杂度:
如果子树大小>1,更新是O(1)的,否则为O(logn)。
每次插入都有可能得到size == 1的孩子,共插入O(nlogn),每次更新需要O(logn)的时间更新。
注意,每次插入,对于size==1的孩子,之后暴力一次,再往上显然就不需要暴力了。
因为能够暴力的前提是另一个孩子的size != 0,所以当前节点的size > 1。 总体复杂度O(nlognlogn)。
const int N = , M = , MAXBIT = ; int n, m;
int w[N];
int ans[N]; struct AdjacencyList {
int head[N], son[N * ], nex[N * ], tot; inline void init(int n) {
for(int i = ; i < n; ++i) head[i] = -;
tot = ; for(int i = ; i < n; ++i) visit[i] = false;
} inline void addEdge(int u, int v) {
son[tot] = v, nex[tot] = head[u];
head[u] = tot++;
} bool visit[N];
inline void bfs(int st, int que[]) {
int len = ;
visit[que[len++] = st] = true;
for(int idx = ; idx < len; idx++) {
int u = que[idx];
for(int tab = head[u], v; tab != -; tab = nex[tab])
if(!visit[v = son[tab]])
visit[v] = true, que[len++] = v;
}
}
} edges; struct Trie {
static struct Node {
static int tot; int child[], size; int f; inline void init() {
child[] = child[] = -, size = f = ;
} } tr[N * M * M]; #define child(x, y) tr[x].child[y]
#define lch(x) child(x, 0)
#define rch(x) child(x, 1)
#define size(x) (x == -1 ? 0 : tr[x].size)
#define f(x) tr[x].f int rot; inline int bruteForce(int u, int v, int d) {
int ret = ;
while(d >= ) {
int t = size(lch(v)) == ;
int _t = t;
if(size(child(u, _t)) == ) _t ^= , ret += ( << d);
u = child(u, _t), v = child(v, t), --d;
}
return ret;
} inline void updata(int u, int d) {
f(u) = -;
if(size(u) > ) {
f(u) = ;
for(int t = ; t < ; ++t) {
int v = child(u, t);
if(size(v) > ) f(u) = max(f(u), f(v));
else if(size(v) == ) {
int xorValue = bruteForce(child(u, t ^ ), v, d - );
xorValue += ( << d);
f(u) = max(f(u), xorValue);
}
}
}
} inline void addVal(int &x, int val, int w, int d) {
if(x == -) tr[x = Node::tot++].init();
tr[x].size += w;
if(d >= ) addVal(child(x, (val & ( << d)) > ), val, w, d - ); updata(x, d);
} inline void add(int val, int w) {
addVal(rot, val, w, MAXBIT - );
} inline void traverse(int x, int now, int d, Trie &pro) {
if(x == -) return;
if(d < ) pro.add(now, size(x));
else {
if(size(lch(x)) > )
traverse(lch(x), now, d - , pro);
if(size(rch(x)) > )
traverse(rch(x), now + ( << d), d - , pro);
}
} inline void operator +=(Trie &t) {
if(size(rot) < size(t.rot)) swap(rot, t.rot);
t.traverse(t.rot, , MAXBIT - , *this);
} inline int getAnswer() const {
return f(rot);
} } tr[N];
int Trie::Node::tot;
Trie::Node Trie::tr[N * M * M]; int bfsList[N]; inline void init() {
edges.init(n);
Trie::Node::tot = ;
for(int i = ; i < n; ++i) tr[i].rot = -;
} inline void solve() {
edges.bfs(, bfsList); for(int i = n - ; i >= ; --i) {
int u = bfsList[i];
tr[u].add(w[u], );
for(int tab = edges.head[u], v; tab != -; tab = edges.nex[tab])
if(tr[v = edges.son[tab]].rot != -)
tr[u] += tr[v];
ans[u] = tr[u].getAnswer();
} scanf("%d", &m);
while(m--) {
int x;
scanf("%d", &x);
printf("%d\n", ans[x - ]);
}
} int main() {
int testCase;
scanf("%d", &testCase);
for(int testIndex = ; testIndex <= testCase; ++testIndex) {
scanf("%d", &n);
init();
for(int i = ; i < n; ++i) scanf("%d", &w[i]);
for(int i = , u, v; i < n; ++i) {
scanf("%d%d", &u, &v);
--u, --v;
edges.addEdge(u, v), edges.addEdge(v, u);
}
printf("Case #%d:\n", testIndex);
solve();
}
return ;
}
2016中国大学生程序设计竞赛 - 网络选拔赛 J. Alice and Bob的更多相关文章
- 2016中国大学生程序设计竞赛 - 网络选拔赛 C. Magic boy Bi Luo with his excited tree
Magic boy Bi Luo with his excited tree Problem Description Bi Luo is a magic boy, he also has a migi ...
- 2016中国大学生程序设计竞赛 网络选拔赛 I This world need more Zhu
This world need more Zhu Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- 2016中国大学生程序设计竞赛 - 网络选拔赛 1004 Danganronpa
Problem Description Chisa Yukizome works as a teacher in the school. She prepares many gifts, which ...
- 2016中国大学生程序设计竞赛 - 网络选拔赛 1011 Lweb and String
Problem Description Lweb has a string S. Oneday, he decided to transform this string to a new sequen ...
- 2016中国大学生程序设计竞赛 - 网络选拔赛 1001 A water problem (大数取余)
Problem Descripton Two planets named Haha and Xixi in the universe and they were created with the un ...
- 2018中国大学生程序设计竞赛 - 网络选拔赛 1009 - Tree and Permutation 【dfs+树上两点距离和】
Tree and Permutation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- HDU 6150 - Vertex Cover | 2017 中国大学生程序设计竞赛 - 网络选拔赛
思路来自 ICPCCamp /* HDU 6150 - Vertex Cover [ 构造 ] | 2017 中国大学生程序设计竞赛 - 网络选拔赛 题意: 给了你一个贪心法找最小覆盖的算法,构造一组 ...
- 2018中国大学生程序设计竞赛 - 网络选拔赛 1001 - Buy and Resell 【优先队列维护最小堆+贪心】
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6438 Buy and Resell Time Limit: 2000/1000 MS (Java/O ...
- 2018中国大学生程序设计竞赛 - 网络选拔赛 1010 YJJ's Salesman 【离散化+树状数组维护区间最大值】
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6447 YJJ's Salesman Time Limit: 4000/2000 MS (Java/O ...
随机推荐
- Linux快速体验
目录结构: /:根目录,根目录下一般只存放子目录,不存放文件./bin: 存放可执行的二进制文件,如常用的命令ls. tar.mv. cat等./boot: 存放linux系统启动时需要的一些文件./ ...
- WinForm------SimpleButton去掉点击时的边框
设置属性
- 一、基于hadoop的nginx访问日志分析---解析日志篇
前一阵子,搭建了ELK日志分析平台,用着挺爽的,再也不用给开发拉各种日志,节省了很多时间. 这篇博文是介绍用python代码实现日志分析的,用MRJob实现hadoop上的mapreduce,可以直接 ...
- Java Web学习笔记8
上下文参数(context-param) 由于init-param是配置在<servlet>标签里的,只能有这个Servlet来读取,因此它不是全局的参数,不能被其他的Servlet读取. ...
- 探索 OpenStack 之(8):Neutron 深入探索之 OVS + GRE 之 完整网络流程 篇
前两篇博文分别研究了Compute节点和Neutron节点内部的网络架构.本文通过一些典型流程案例来分析具体网络流程过程. 0. 环境 同 学习OpenStack之(7):Neutron 深入学习之 ...
- ThinkPHP中疑难笔记
不但要记住核心的东西, 还要记住 相关的 东西: 如php cli的版本是 5.6.14 bulit: sep 30, 2015 tp中, 通常说的系统就是框架; 项目就是 "应用程序&qu ...
- css3动画简介以及动画库animate.css的使用
在这个年代,你要是不懂一点点css3的知识,你都不好意思说你是个美工.美你妹啊,请叫我前端工程师好不好.呃..好吧,攻城尸...呵呵,作为一个攻城尸,没有点高端大气上档次的东西怎么能行呢,那么css3 ...
- [译]git的那些flag
git add -p console有一个交互式的界面(如下图),让你一个一个文件的选择是add还是不add.注意这些文件必须是tracked过的, 也就是说如果你的新的文件从来没有add过,那么他不 ...
- [译]git log进阶
格式化log输出 oneline --oneline标记将每个commit压缩成一行. 默认情况下显示一个commit ID和commit描述的第一行. 输出如下: 0e25143 Merge bra ...
- C# 使用Silverlight toolkit Chart
一.基础介绍 Silverlight ToolKit是微软发布的基于Microsoft-Public License(MS-PL)许可协议的控件集.MS-PL许可协议允许商业或非商业的发布,所以我们可 ...