2016中国大学生程序设计竞赛 - 网络选拔赛 J. Alice and Bob

时间:2021-01-07 12:46:52

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

Problem Description
As you know, Alice and Bob always play game together, and today they get a tree.

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?

 
Input
In the first line there is an integer T, indicating the number of test cases.

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
For each test case:

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.

 
Sample Input
1
3
1 2 3
1 2
2 3
3
1
2
3
 
Sample Output
Case #1: 2 1 -1
 
题意:
给出一颗树,每次有两人在某个子树进行博弈。
每次博弈两人轮流分别选取一个节点,得分为两者选取的节点的权值的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 ;
}