联赛day3

T1

直接异或 \(HASH\) 判断集合是否正确

Code

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
#include <cstdio>
#include <cstring>
#include <algorithm>
#define lowbit(x) (x & -x)

using namespace std;

typedef long long ll;
const int maxn = 2e5 + 10;
const int mod = 998244353;
const int inf = 0x3f3f3f3f;

inline int __read()
{
int x(0), t(1);
char o (getchar());
while (o < '0' || o > '9') {
if (o == '-') t = -1;
o = getchar();
}
for (; o >= '0' && o <= '9'; o = getchar()) {
x = (x << 1) + (x << 3) + (o ^ 48);
}
return x * t;
}

int n, m, a[maxn], dfn[maxn], size[maxn], f[maxn][25], depth[maxn];
int id, cur, head[maxn], edge[maxn << 1], Prev[maxn << 1];
ll reflect[maxn], pre[maxn];

inline void addedge(int u, int v)
{
Prev[++cur] = head[u];
head[u] = cur;
edge[cur] = v;
}

ll t[maxn];

inline void add(int x, ll d)
{
while (x <= n) {
t[x] ^= d;
x += lowbit(x);
}
}

inline ll query(int x)
{
ll ans(0);
while (x) {
ans ^= t[x];
x -= lowbit(x);
}
return ans;
}

inline ll rnd()
{
return rand() * 114514ll * 233ll % mod;
}

void dfs(int u, int fa)
{
dfn[u] = ++id, depth[u] = depth[fa] + 1;
size[u] = 1, f[u][0] = fa;
for (int i = 1; i <= 18; ++i)
f[u][i] = f[f[u][i - 1]][i - 1];
for (int i = head[u]; i; i = Prev[i]) {
if (edge[i] == fa) continue;
dfs(edge[i], u);
size[u] += size[edge[i]];
}
}

inline int lca(int x, int y)
{
if (depth[x] < depth[y]) swap(x, y);
for (int i = 18; ~i; --i)
if (depth[f[x][i]] >= depth[y]) x = f[x][i];
if (x == y) return x;
for (int i = 18; ~i; --i)
if (f[x][i] ^ f[y][i]) x = f[x][i], y = f[y][i];
return f[x][0];
}

inline void solve()
{
id = cur = 0;
n = __read(), m = __read();
for (int i = 1; i <= n; ++i)
for (int j = 0; j <= 18; ++j) f[i][j] = 0;
for (int i = 1; i <= n; ++i)
a[i] = __read(), t[i] = head[i] = 0;
for (int i = 1; i <= n; ++i)
reflect[i] = rnd(), pre[i] = pre[i - 1] ^ reflect[i];

for (int i = 1; i < n; ++i) {
int u = __read(), v = __read();
addedge(u, v), addedge(v, u);
}

dfs(1, 0);
for (int i = 1; i <= n; ++i) add(dfn[i], reflect[a[i]]), add(dfn[i] + size[i], reflect[a[i]]);

int o, x, y;
while (m--) {
o = __read(), x = __read(), y = __read();
if (o == 1) {
ll tmp = query(dfn[x]) ^ query(dfn[y]);
int rca = lca(x, y), len = depth[x] + depth[y] - 2 * depth[rca] + 1;
tmp ^= reflect[a[rca]];
if (tmp == pre[len]) puts("Yes");
else puts("No");
} else {
add(dfn[x], reflect[a[x]] ^ reflect[y]);
add(dfn[x] + size[x], reflect[a[x]] ^ reflect[y]);
a[x] = y;
}
}
}

int main()
{
srand(time(0));
int t = __read();
while (t--) solve();
}