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 ...
随机推荐
- Emgu.CV/opencv 绘图 线面文字包括中文
绘图很简单 Emgu.CV.Image<Bgr, Byte> image; 使用image.Draw可以画各种图形和文字包括英文及数字,不支持中文 CircleF circle = ...
- CentOS Linux解决Device eth0 does not seem to be present
在VMware里克隆出来的Centos Linux.. ifconfig...没有看到eth0..然后重启网卡又报下面错误. 故障现象: 解决办法: 首先,打开/etc/udev/rules.d/70 ...
- EF唯一索引
this.Property(p => p.Name) .IsRequired() .HasMaxLength()) .HasColumnAnnotation("Index", ...
- asp.net中套用母版页之后的findcontrol
套用模板页之后,如果要在内容页中查找某个控件,需要先找到模板页中的ContentPlaceHolder,在通过ContentPlaceHolder查找代码,如下: LinkButton btn = t ...
- 探索Aspnetcore+mysql+efcore
摘要 之前尝试了,新建asp.net core站点,那么如何和mysql建立连接,如果操作mysql?本篇将尝试使用EntityFrameworkCore进行mysql的操作. 一个例子 首先新建一个 ...
- SQL 查找表名 字段名
转载:http://www.accessoft.com/article-show.asp?id=6135 经常碰到一些忘记表名称的情况,此时只记得个大概,此时可通过查询系统表Sysobjects找到所 ...
- PHP Strom 配置less 并设置编译后在远程开发模式下自动上传css文件
ctrl+alt+s ->File Watchers->add-> 其中Argument中的-x代表最后编译过后的css文件为压缩过的 此时就可以用了,但是编译过后less可以自动上 ...
- 【Eclipse】总结自己在工作中经常使用到的Eclipse快捷键
一些我觉得比较有用的快捷键,仅作参考. 1.alt + shift + c :更改方法签名. 2.三次鼠标左键单击: 选中一整行. 3.alt + shift + d/x: 再按t : 运行junit ...
- Linux From Scratch - Version 7.7-systemd (中文)
Linux From Scratch - Version 7.7-systemd: https://linux.cn/lfs/LFS-BOOK-7.7-systemd/index.html
- Hibernate POJO在序列化(JSON)时遇到的若干问题
假设某 POJO 有属性如下: private Set<User> users = new HashSet<>(0); @OneToMany(fetch = FetchType ...