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 ...
随机推荐
- JavaScript中ActiveXObject对象
JavaScript中ActiveXObject对象是启用并返回 Automation 对象的引用.使用方法: newObj = new ActiveXObject( servername.typen ...
- Python Day1
一.安装python windows 1.下载安装包 https://www.python.org/downloads/ 2.安装 默认安装到C盘下 3.配置环境变量 右键计算机属性---高级系统设置 ...
- UGUI
http://www.2fz1.com/post/unity-ugui-recttransform/ //this.transform.position 获取的是世界坐标,而 this.transfo ...
- 【bzoj3450】Tyvj1952 Easy
题目描述 某一天WJMZBMR在打osu~~~但是他太弱逼了,有些地方完全靠运气:(我们来简化一下这个游戏的规则有n次点击要做,成功了就是o,失败了就是x,分数是按comb计算的,连续a个comb就有 ...
- PHP中的魔术方法:__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep, __wakeup, __toString, __set_state, __clone and __autoload
1.__get.__set 这两个方法是为在类和他们的父类中没有声明的属性而设计的: __get( $property ) 当调用一个未定义的属性时访问此方法: __set( $property, $ ...
- jquery.validate.js插件使用
jQuery验证控件jquery.validate.js使用说明+中文API 官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-valid ...
- 07OC之KVC、KVO
在OC中,有着很多动态的特性,今天我们着重讲讲OC中的键值编码(KVC)和键值监听(KVO)特性. 一.键值编码(KVC) 在C#中,我们可以通过反射的方式动态去读写一个对象,有时候很方便,因为可以利 ...
- POJ2914 (未解决)无向图最小割|Stoer-Wagner算法|模板
还不是很懂,贴两篇学习的博客: http://www.hankcs.com/program/algorithm/poj-2914-minimum-cut.html http://blog.sina.c ...
- 兼容IE8以下浏览器input表单属性placeholder不能智能提示功能
当前很多表单提示使用了表单属性placeholder,可这属性不兼容IE8以下的浏览器,我自己写了一个兼容处理js // 兼容IE8以下浏览器input不能智能提示功能 if(navigator.ap ...
- Wireshark抓包工具
首先下载并安装Wireshark软件,最好选择中文版,因为会使你用的更顺手. 安装完毕之后,双击打开Wireshark软件,主界面还是比较清晰明了的,可是怎么用还是稀里糊涂的吧. 点击菜单栏红圈中的选 ...