[SCOI 2016]幸运数字

Description

A 国共有 n 座城市,这些城市由 n-1 条道路相连,使得任意两座城市可以互达,且路径唯一。每座城市都有一个幸运数字,以纪念碑的形式矗立在这座城市的正中心,作为城市的象征。一些旅行者希望游览 A 国。旅行者计划乘飞机降落在 x 号城市,沿着 x 号城市到 y 号城市之间那条唯一的路径游览,最终从 y 城市起飞离开 A 国。
在经过每一座城市时,游览者就会有机会与这座城市的幸运数字拍照,从而将这份幸运保存到自己身上。然而,幸运是不能简单叠加的,这一点游览者也十分清楚。他们迷信着幸运数字是以异或的方式保留在自己身上的。例如,游览者拍了 3 张照片,幸运值分别是 5,7,11,那么最终保留在自己身上的幸运值就是 9(5 xor 7 xor 11)。
有些聪明的游览者发现,只要选择性地进行拍照,便能获得更大的幸运值。例如在上述三个幸运值中,只选择 5 和 11 ,可以保留的幸运值为 14 。现在,一些游览者找到了聪明的你,希望你帮他们计算出在他们的行程安排中可以保留的最大幸运值是多少。

Input

第一行包含 2 个正整数 n ,q,分别表示城市的数量和旅行者数量。第二行包含 n 个非负整数,其中第 i 个整数 Gi 表示 i 号城市的幸运值。随后 n-1 行,每行包含两个正整数 x ,y,表示 x 号城市和 y 号城市之间有一条道路相连。随后 q 行,每行包含两个正整数 x ,y,表示这名旅行者的旅行计划是从 x 号城市到 y 号城市。N<=20000,Q<=200000,Gi<=2^60

Output

 输出需要包含 q 行,每行包含 1 个非负整数,表示这名旅行者可以保留的最大幸运值。

Sample Input

4 2
11 5 7 9
1 2
1 3
1 4
2 3
1 4

Sample Output

14
11

题解

线性基+点分治。

这两个都是最近才接触的,正好有道题结合起来了。

我们每次找到重心,处理与重心相关的路径。

处理时我们将重心到每个节点这一段的$xor$存起来,然后找到所有经过重心的路径。

我是在遍历以重心$G$为根的一个子树过程中,找到与这棵子树中节点$u$有关的询问$(u,v)$,判断是否在之前遍历过的以重心为根的其他子树中出现过,如果出现过,我们可以将$G->u$和$G->v$这两段的线性基合并。找到合并后的线性基中的最大值就好了。对于合并,直接暴力拆开一个线性基,一个一个插入到另一个线性基中。

处理完这棵子树之后,再遍历一遍,打上访问过的标记。这样保证所有询问都只计算过一次。

注意,处理完这个重心之后,清空标记时,不要$memset$,直接再遍历一遍就好了,快得多。

值得注意的是,要单独考虑与$G$有关的询问如$(G,v)$,因为重心$G$没标记访问过。(当然了,你可以找完重心后就马上把重心标记访问,效果是一样的,这个询问也只会遍历一次。因为重心是不会被访问的)

另外注意单独考虑$(u,u)$的询问。

  1 //It is made by Awson on 2017.9.22
  2 #include <set>
  3 #include <map>
  4 #include <ctime>
  5 #include <cmath>
  6 #include <queue>
  7 #include <stack>
  8 #include <vector>
  9 #include <cstdio>
 10 #include <string>
 11 #include <cstring>
 12 #include <cstdlib>
 13 #include <iostream>
 14 #include <algorithm>
 15 #define LL long long
 16 #define Max(a, b) ((a) > (b) ? (a) : (b))
 17 #define Min(a, b) ((a) < (b) ? (a) : (b))
 18 #define sqr(x) ((x)*(x))
 19 using namespace std;
 20 const int N = 20000;
 21 const int Q = 200000;
 22 const int INF = ~0u>>1;
 23 LL st[64];
 24 int Read() {
 25     int sum = 0;
 26     char ch = getchar();
 27     while (ch < '0' || ch > '9') ch = getchar();
 28     while (ch >= '0' && ch <= '9') sum = (sum<<3)+(sum<<1)+ch-48, ch = getchar();
 29     return sum;
 30 }
 31 
 32 struct base {
 33     LL a[64];
 34     void insert(LL x) {
 35     for (int i = 62; i >= 0; i--)
 36         if (x&st[i]) {
 37         if (!a[i]) {
 38             a[i] = x;
 39             break;
 40         }
 41         else x ^= a[i];
 42         }
 43     }
 44     LL getmax() {
 45     LL maxn = 0;
 46     for (int i = 62; i >= 0; i--)
 47         maxn = Max(maxn, (maxn^a[i]));
 48     return maxn;
 49     }
 50     void clean() {
 51     for (int i = 62; i >= 0; i--)
 52         a[i] = 0;
 53     }
 54     void copy(base b) {
 55     for (int i = 62; i >= 0; i--)
 56         a[i] = b.a[i];
 57     }
 58     void merge(base b) {
 59     for (int i = 62; i >= 0; i--) {
 60         insert(b.a[i]);
 61     }
 62     }
 63 }ba[N+5], tmp;
 64 int n, q, u, v;
 65 LL g[N+5];
 66 struct tt {
 67     int to, next;
 68 }edge[N*2+5];
 69 int path[N+5], top;
 70 struct question {
 71     int v, id, next;
 72 }que[Q*2+5];
 73 int pathq[N+5], topq;
 74 LL ans[Q+5];
 75 int size[N+5], mx[N+5], minsize, root;
 76 bool vis[N+5];
 77 bool judge[N+5];
 78 
 79 void add(int u, int v) {
 80     edge[++top].to = v;
 81     edge[top].next = path[u];
 82     path[u] = top;
 83 }
 84 void addq(int u, int v, int id) {
 85     que[++topq].v = v;
 86     que[topq].id = id;
 87     que[topq].next = pathq[u];
 88     pathq[u] = topq;
 89 }
 90 void get_size(int u, int fa) {
 91     size[u] = 1, mx[u] = 0;
 92     for (int i = path[u]; i; i = edge[i].next)
 93     if (edge[i].to != fa && !vis[edge[i].to]) {
 94         get_size(edge[i].to, u);
 95         size[u] += size[edge[i].to];
 96         mx[u] = Max(mx[u], size[edge[i].to]);
 97     }
 98 }
 99 void get_root(int r, int u, int fa) {
100     mx[u] = Max(mx[u], size[r]-size[u]);
101     if (mx[u] < minsize) minsize = mx[u], root = u;
102     for (int i = path[u]; i; i = edge[i].next)
103     if (edge[i].to != fa && !vis[edge[i].to])
104         get_root(r, edge[i].to, u);
105 }
106 void get_ans(int r, int u, int fa) {
107     ba[u].copy(ba[fa]);
108     ba[u].insert(g[u]);
109     for (int i = pathq[u]; i; i = que[i].next)
110     if (judge[que[i].v]) {
111         tmp.copy(ba[u]);
112         tmp.merge(ba[que[i].v]);
113         ans[que[i].id] = tmp.getmax();
114     }
115     else if (que[i].v == r) {
116         ans[que[i].id] = ba[u].getmax();
117     }
118     for (int i = path[u]; i; i = edge[i].next)
119     if (edge[i].to != fa && !vis[edge[i].to])
120         get_ans(r, edge[i].to, u);
121 }
122 void get_update(int u, int fa) {
123     judge[u] = !judge[u];
124     for (int i = path[u]; i; i = edge[i].next)
125     if (edge[i].to != fa && !vis[edge[i].to])
126         get_update(edge[i].to, u);
127 }
128 void doit(int x) {
129     minsize = INF;
130     get_size(x, 0);
131     get_root(x, x, 0);
132     vis[root] = 1;
133     ba[root].clean();
134     ba[root].insert(g[root]);
135     for (int i = path[root]; i; i = edge[i].next)
136     if (!vis[edge[i].to]) {
137         get_ans(root, edge[i].to, root);
138         get_update(edge[i].to, root);
139     }
140     for (int i = path[root]; i; i = edge[i].next)
141     if (!vis[edge[i].to])
142         get_update(edge[i].to, root);
143     for (int i = path[root]; i; i = edge[i].next)
144     if (!vis[edge[i].to])
145         doit(edge[i].to);
146 }
147 void work() {
148     n = Read(), q = Read();
149     for (int i = 1; i <= n; i++) scanf("%lld", &g[i]);
150     for (int i = 1; i < n; i++) {
151     u = Read(), v = Read();
152     add(u, v), add(v, u);
153     }
154     for (int i = 1; i <= q; i++) {
155     u = Read(), v = Read();
156     if (u != v) addq(u, v, i), addq(v, u, i);
157     else ans[i] = g[u];
158     }
159     doit(1);
160     for (int i = 1; i <= q; i++) printf("%lld\n", ans[i]);
161 }
162 int main() {
163     st[0] = 1;
164     for (int i = 1; i < 63; i++)
165     st[i] = st[i-1]<<1;
166     work();
167     return 0;
168 }

 

posted @ 2017-09-22 17:20  NaVi_Awson  阅读(279)  评论(0编辑  收藏  举报