set维护dfs区间开始节点.

Solution

用set维护一段同色dfs区间的开始节点, 这样每次染色, 找到子树内所有的开始节点并删除, 然后换上一个新开始节点. 这样的复杂度就是均摊的了.

实现细节蛮多的.

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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

const int maxn = 1e+5 + 5;

struct edge {
int to, nxt;
}e[maxn << 1];
int ptr, lnk[maxn], n, c[maxn];
int dfn[maxn], top[maxn], siz[maxn], mxson[maxn], cnt;
int dep[maxn], fa[maxn], rev[maxn], seg[maxn << 2], b[maxn];
int out[maxn];
set<pii> st, col[maxn];

inline int rd() {
register int x = 0, c = getchar();
while (!isdigit(c)) c = getchar();
while (isdigit(c)) x = x * 10 + (c ^ 48), c = getchar();
return x;
}
inline void add(int bgn, int end) {
e[++ptr] = (edge){end, lnk[bgn]};
lnk[bgn] = ptr;
}
inline void init() {
st.clear();
ptr = 0; cnt = 0;
for (int i = 1; i < maxn; ++i)
b[i] = 0, col[i].clear();
memset(lnk, 0, sizeof lnk);
}
void dfs(int x, int f) {
dfn[x] = ++cnt;
dep[x] = dep[f] + 1;
siz[x] = 1;
fa[x] = f;
mxson[x] = 0;
for (int p = lnk[x]; p; p = e[p].nxt) {
int y = e[p].to;
if (y == f) continue;
dfs(y, x);
siz[x] += siz[y];
if (siz[y] > siz[mxson[x]]) mxson[x] = y;
}
out[x] = cnt;
}
void dfs2(int x, int init) {
top[x] = init;
//dfn[x] = ++cnt;
//rev[cnt] = x;
if (!mxson[x]) return;
dfs2(mxson[x], init);
for (int p = lnk[x]; p; p = e[p].nxt) {
int y = e[p].to;
if (y == fa[x] || y == mxson[x]) continue;
dfs2(y, y);
}
}
inline int lca(int x, int y) {
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) swap(x, y);
x = fa[top[x]];
}
return (dep[x] < dep[y]) ? x : y;
}
inline void pushdown(int cur) {
if (seg[cur]) {
seg[cur << 1] = seg[cur << 1|1] = seg[cur];
seg[cur] = 0;
}
}
void build(int cur, int l, int r) {
seg[cur] = 0;
if (l == r) {
seg[cur] = c[dfn[l]];
return;
}
int mid = (l + r) >> 1;
build(cur << 1, l, mid);
build(cur << 1|1, mid + 1, r);
}
void update(int cur, int l, int r, int L, int R, int c) {
if (L <= l && r <= R) {
seg[cur] = c;
return;
}
int mid = (l + r) >> 1;
pushdown(cur);
if (L <= mid) update(cur << 1, l, mid, L, R, c);
if (R > mid) update(cur << 1|1, mid + 1, r, L, R, c);
}
int query(int cur, int l, int r, int p) {
if (l == r) return seg[cur];
int mid = (l + r) >> 1;
pushdown(cur);
if (p <= mid) return query(cur << 1, l, mid, p);
else return query(cur << 1|1, mid + 1, r, p);
}
inline void addb(int x, int v) {
for (; x <= n; x += (x & -x))
b[x] += v;
}
inline int pre(int x) {
int ans = 0;
for (; x; x -= (x & -x))
ans += b[x];
return ans;
}


typedef set<pii>::iterator iter;
iter glbit;
inline int get(int x) {
int x1 = 0, x2 = 0;
if (glbit != col[c[x]].begin()) {
iter i = glbit; i--;
x1 = lca(x, i->second);
}
iter i = glbit;
i++;
if (i != col[c[x]].end()) x2 = lca(x, i->second);
return (dep[x1] > dep[x2]) ? x1 : x2;
}

int main() {
int T = rd();
int id = 0;
while (T--) {
printf("Case #%d:\n", ++id);
init();
n = rd(); int u, v;
for (int i = 1; i < n; ++i) {
u = rd(); v = rd();
add(u, v); add(v, u);
}
dfs(1, 0);
dfs2(1, 1);
for (int i = 1; i <= n; ++i) {
c[i] = rd();
col[c[i]].insert(make_pair(dfn[i], i));
st.insert(make_pair(dfn[i], i));
}
build(1, 1, n);
for (int i = 1; i <= n; ++i) {
int lst = 0;
for (iter it = col[i].begin(); it != col[i].end(); ++it) {
addb(it->first, 1);
if (lst) addb(dfn[lca(lst, it->second)], -1);
lst = it->second;
}
}
int q = rd(), opt;
while (q--) {
opt = rd(); u = rd();
pii tmp(dfn[u], u);
if (!opt) {
v = rd();
while (true) {
glbit = st.lower_bound(tmp);
if (glbit == st.end() || glbit->first > out[u]) break;
int uu = glbit->second;
glbit = col[c[uu]].find(*glbit);
int x1 = get(uu);
if (x1) addb(dfn[x1], 1);
addb(dfn[uu], -1);
pii tmp1 = *glbit;
st.erase(tmp1);
col[c[uu]].erase(tmp1);
c[uu] = v;
}
c[u] = v;
st.insert(tmp);
col[v].insert(tmp);
glbit = col[v].find(tmp);
int x1 = get(u);
if (x1) addb(dfn[x1], -1);
addb(dfn[u], 1);
update(1, 1, n, dfn[u], out[u], v);
} else {
int ans = pre(out[u]) - pre(dfn[u] - 1);
iter it = st.lower_bound(tmp);
if (it == st.end()) ++ans;
else if (it->second != u) {
int cr = query(1, 1, n, dfn[u]);
it = col[cr].lower_bound(tmp);
if (it == col[cr].end() || it->first > out[u]) ++ans;
}
printf("%d\n", ans);
}
}
}
return 0;
}