1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
| #include <algorithm> #include <bitset> #include <cctype> #include <cstdio> #include <iostream>
using namespace std;
int read() { int out = 0; char c; while (!isdigit(c = getchar())); for (; isdigit(c); c = getchar()) out = out * 10 + c - '0'; return out; }
const int N = 100010; const int B = 666; const int C = 30000;
void add(int u, int v); void dfs(int u);
int head[N], nxt[N << 1], to[N << 1], cnt; int n, m, type, c[N], fa[N], dep[N], sta[N], top, tot, bl[N], key[N / B + 5], p[N], keyid[N]; bool vis[N]; bitset<C> bs[N / B + 5][N / B + 5], temp;
int main() { int i, u, v, x, y, k, lastans = 0;
n = read(); m = read(); type = read();
for (i = 1; i <= n; ++i) c[i] = read();
for (i = 1; i < n; ++i) { u = read(); v = read(); add(u, v); add(v, u); }
dfs(1);
if (!tot) ++tot; if (keyid[key[tot]] == tot) keyid[key[tot]] = 0; key[tot] = 1; keyid[1] = tot; while (top) bl[sta[top--]] = tot;
for (i = 1; i <= tot; ++i) { if (vis[key[i]]) continue; vis[key[i]] = true; temp.reset(); for (u = key[i]; u; u = fa[u]) { temp[c[u]] = 1; if (keyid[u]) { if (!p[key[i]] && u != key[i]) p[key[i]] = u; bs[keyid[key[i]]][keyid[u]] = temp; } } }
while (m--) { k = read(); temp.reset(); while (k--) { u = x = read() ^ lastans; v = y = read() ^ lastans;
while (key[bl[x]] != key[bl[y]]) { if (dep[key[bl[x]]] > dep[key[bl[y]]]) { if (x == u) { while (x != key[bl[u]]) { temp[c[x]] = 1; x = fa[x]; } } else x = p[x]; } else { if (y == v) { while (y != key[bl[v]]) { temp[c[y]] = 1; y = fa[y]; } } else y = p[y]; } }
if (keyid[x]) temp |= bs[keyid[key[bl[u]]]][keyid[x]]; if (keyid[y]) temp |= bs[keyid[key[bl[v]]]][keyid[y]];
while (x != y) { if (dep[x] > dep[y]) { temp[c[x]] = 1; x = fa[x]; } else { temp[c[y]] = 1; y = fa[y]; } } temp[c[x]] = true; } int ans1 = temp.count(), ans2 = (~temp)._Find_first(); printf("%d %d\n", ans1, ans2); lastans = (ans1 + ans2) * type; }
return 0; }
void dfs(int u) { int i, v, t = top; for (i = head[u]; i; i = nxt[i]) { v = to[i]; if (v == fa[u]) continue; fa[v] = u; dep[v] = dep[u] + 1; dfs(v); if (top - t >= B) { key[++tot] = u; if (!keyid[u]) keyid[u] = tot; while (top > t) bl[sta[top--]] = tot; } } sta[++top] = u; }
void add(int u, int v) { nxt[++cnt] = head[u]; head[u] = cnt; to[cnt] = v; }
|