Description
Solution
考虑倍增LCA,每次跳的时候合并两个线性基,最后求最大值即可。
当然树剖和点分治都可以做,但是树剖会多个
log2
l
o
g
2
20分钟码完编译直接过样例,以为要1A了。结果,结果,调了好久,发现输入权值时没有用%lld
。。。
Source
/*******************************************
* Au: Hany01
* Prob: [BZOJ4568][SCOI2016] 幸运数字
* Date: Mar 10th, 2018
* Email: hany01@foxmail.com
*******************************************/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;
#define Rep(i , j) for (register int i = 0 , i##_end_ = j; i < i##_end_ ; ++ i)
#define For(i , j , k) for (register int i = (j) , i##_end_ = (k) ; i <= i##_end_ ; ++ i)
#define Fordown(i , j , k) for (register int i = (j) , i##_end_ = (k) ; i >= i##_end_ ; -- i)
#define Set(a , b) memset(a , b , sizeof(a))
#define SZ(a) ((int)(a.size()))
#define ALL(a) a.begin(), a.end()
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
#define y1 wozenmezhemecaia
#ifdef hany01
#define debug(...) fprintf(stderr , __VA_ARGS__)
#else
#define debug(...)
#endif
inline void File() {
#ifdef hany01
freopen("bzoj4568.in" , "r" , stdin);
freopen("bzoj4568.out" , "w" , stdout);
#endif
}
template<typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template<typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
inline int read() {
register char c_; register int _ , __;
for (_ = 0 , __ = 1 , c_ = getchar() ; !isdigit(c_) ; c_ = getchar()) if (c_ == '-') __ = -1;
for ( ; isdigit(c_) ; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
return _ * __;
}
const int maxn = 20005;
int n, q, beg[maxn], nex[maxn << 1], v[maxn << 1], e, fa[maxn][18], log2n, dep[maxn];
LL val[maxn];
struct LinearBasis
{
LL a[63];
LinearBasis() { Set(a, 0); }
}base[maxn][18];
typedef LinearBasis LB;
inline void Insert(LB& A, LL x)
{
if (!x) return ;
Fordown(i, 61, 0) if (x >> i & 1) {
if (!A.a[i]) { A.a[i] = x; return; }
x ^= A.a[i];
}
}
inline LB Merge(LB A, LB B)
{
register LB C = A;
For(i, 0, 61) Insert(C, B.a[i]);
return C;
}
inline LL getMax(LB A)
{
LL Ans = 0;
Fordown(i, 61, 0) chkmax(Ans, Ans ^ A.a[i]);
return Ans;
}
inline void add(int uu, int vv) { v[++ e] = vv, nex[e] = beg[uu], beg[uu] = e; }
void dfs(int u, int pa)
{
fa[u][0] = pa, Insert(base[u][0], val[u]);
For(i, 1, log2n)
fa[u][i] = fa[fa[u][i - 1]][i - 1], base[u][i] = Merge(base[u][i - 1], base[fa[u][i - 1]][i - 1]);
for (register int i = beg[u]; i; i = nex[i]) if (v[i] != pa)
dep[v[i]] = dep[u] + 1, dfs(v[i], u);
}
inline void Init()
{
static int uu, vv;
n = read(), q = read();
For(i, 1, n) scanf("%lld", &val[i]);
For(i, 2, n) uu = read(), vv = read(), add(uu, vv), add(vv, uu);
log2n = ceil(log(n * 1.) / log(2.0));
dfs(1, 0);
}
inline void Solve()
{
static int x, y;
while (q --) {
register LB Ans;
x = read(), y = read();
if (dep[x] < dep[y]) swap(x, y);
Fordown(i, log2n, 0) if ((1 << i) <= dep[x] - dep[y])
Ans = Merge(Ans, base[x][i]), x = fa[x][i];
Fordown(i, log2n, 0) if (fa[x][i] != fa[y][i])
Ans = Merge(Merge(Ans, base[x][i]), base[y][i]), x = fa[x][i], y = fa[y][i];
if (x != y) Ans = Merge(Merge(Ans, base[x][0]), base[y][0]), x = fa[x][0], y = fa[y][0];
Insert(Ans, val[x]);
printf("%lld\n", getMax(Ans));
}
}
int main()
{
File();
Init();
Solve();
return 0;
}
//南国有佳人,容华若桃李。
// -- 曹植《杂诗·南国有佳人》