-
Notifications
You must be signed in to change notification settings - Fork 31
/
SelectOneFromEachPair.ts
327 lines (295 loc) · 7.75 KB
/
SelectOneFromEachPair.ts
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/* eslint-disable @typescript-eslint/no-non-null-assertion */
/**
* 可撤销并查集,维护连通分量为树的联通分量个数.
* Map 实现.
*/
class SelectOneFromEachPairMap {
private _part = 0
private _treeCount = 0
private readonly _data = new Map<number, number>()
private readonly _edge = new Map<number, number>()
private readonly _history: [root: number, data: number, edge: number][] = []
/**
* 从每条边中恰好选一个点, 最多能选出多少个不同的点.
* 对每个大小为m的连通块,树的贡献为m-1,环的贡献为m.
* 因此答案为`总点数-树的个数`.
*/
solve(): number {
return this._data.size - this._treeCount
}
/**
* 撤销上一次合并操作,没合并成功也要撤销.
*/
undo(): boolean {
if (!this._history.length) {
return false
}
const [small, smallData, smallEdge] = this._history.pop()!
const [big, bigData, bigEdge] = this._history.pop()!
this._data.set(small, smallData)
this._data.set(big, bigData)
this._edge.set(small, smallEdge)
this._edge.set(big, bigEdge)
if (big === small) {
if (this.isTree(big)) {
this._treeCount++
}
} else {
if (this.isTree(big) || this.isTree(small)) {
this._treeCount++
}
this._part++
}
return true
}
/**
* u所在的联通分量是否为树.
*/
isTree(u: number): boolean {
const root = this.find(u)
const vertex = this.getSize(root)
return vertex === this._edge.get(root)! + 1
}
/**
* 联通分量为树的联通分量个数(孤立点也算树).
*/
countTree(): number {
return this._treeCount
}
/**
* 添加边对(u,v).
* @alias addPair
*/
union(u: number, v: number): boolean {
u = this.find(u)
v = this.find(v)
this._history.push([u, this._data.get(u)!, this._edge.get(u)!]) // big
this._history.push([v, this._data.get(v)!, this._edge.get(v)!]) // small
if (u === v) {
if (this.isTree(u)) {
this._treeCount--
}
this._edge.set(u, this._edge.get(u)! + 1)
return false
}
if (this._data.get(u)! > this._data.get(v)!) {
u ^= v
v ^= u
u ^= v
}
if (this.isTree(u) || this.isTree(v)) {
this._treeCount--
}
this._data.set(u, this._data.get(u)! + this._data.get(v)!)
this._data.set(v, u)
this._edge.set(u, this._edge.get(u)! + this._edge.get(v)! + 1)
this._part--
return true
}
/**
* 不能路径压缩.
*/
find(u: number): number {
if (!this._data.has(u)) {
this.add(u)
return u
}
let cur = u
while ((this._data.get(cur) || -1) >= 0) {
cur = this._data.get(cur)!
}
return cur
}
getSize(x: number): number {
return -this._data.get(this.find(x))!
}
getEdge(x: number): number {
return this._edge.get(this.find(x))!
}
getGroups(): Map<number, number[]> {
const groups = new Map<number, number[]>()
for (const k of this._data.keys()) {
const root = this.find(k)
if (!groups.has(root)) {
groups.set(root, [])
}
groups.get(root)!.push(k)
}
return groups
}
add(u: number): boolean {
if (this._data.has(u)) {
return false
}
this._data.set(u, -1)
this._edge.set(u, 0)
this._part++
this._treeCount++
return true
}
get part(): number {
return this._part
}
get treeCount(): number {
return this._treeCount
}
}
/**
* 可撤销并查集,维护连通分量为树的联通分量个数.
* 数组实现.
*/
class SelectOneFromEachPairArray {
private _part: number
private _treeCount: number
private readonly _n: number
private readonly _data: Int32Array
private readonly _edge: Uint32Array
private readonly _history: [root: number, data: number, edge: number][] = []
constructor(n: number) {
this._part = n
this._treeCount = n
this._n = n
this._data = new Int32Array(n).fill(-1)
this._edge = new Uint32Array(n)
}
/**
* 从每条边中恰好选一个点, 最多能选出多少个不同的点.
* 对每个大小为m的连通块,树的贡献为m-1,环的贡献为m.
* 因此答案为`总点数-树的个数`.
*/
solve(): number {
return this._n - this._treeCount
}
/**
* 撤销上一次合并操作,没合并成功也要撤销.
*/
undo(): boolean {
if (!this._history.length) {
return false
}
const [small, smallData, smallEdge] = this._history.pop()!
const [big, bigData, bigEdge] = this._history.pop()!
this._data[small] = smallData
this._data[big] = bigData
this._edge[small] = smallEdge
this._edge[big] = bigEdge
if (big === small) {
if (this.isTree(big)) {
this._treeCount++
}
} else {
if (this.isTree(big) || this.isTree(small)) {
this._treeCount++
}
this._part++
}
return true
}
/**
* u所在的联通分量是否为树.
*/
isTree(u: number): boolean {
const root = this.find(u)
const vertex = this.getSize(root)
return vertex === this._edge[root] + 1
}
/**
* 联通分量为树的联通分量个数(孤立点也算树).
*/
countTree(): number {
return this._treeCount
}
/**
* 添加边对(u,v).
* @alias addPair
*/
union(u: number, v: number): boolean {
u = this.find(u)
v = this.find(v)
this._history.push([u, this._data[u], this._edge[u]]) // big
this._history.push([v, this._data[v], this._edge[v]]) // small
if (u === v) {
if (this.isTree(u)) {
this._treeCount--
}
this._edge[u]++
return false
}
if (this._data[u] > this._data[v]) {
u ^= v
v ^= u
u ^= v
}
if (this.isTree(u) || this.isTree(v)) {
this._treeCount--
}
this._data[u] += this._data[v]
this._data[v] = u
this._edge[u] += this._edge[v] + 1
this._part--
return true
}
/**
* 不能路径压缩.
*/
find(u: number): number {
let cur = u
while (this._data[cur] >= 0) {
cur = this._data[cur]
}
return cur
}
getSize(x: number): number {
return -this._data[this.find(x)]
}
getEdge(x: number): number {
return this._edge[this.find(x)]
}
getGroups(): Map<number, number[]> {
const groups = new Map<number, number[]>()
for (let i = 0; i < this._n; i++) {
const root = this.find(i)
if (!groups.has(root)) {
groups.set(root, [])
}
groups.get(root)!.push(i)
}
return groups
}
get part(): number {
return this._part
}
get treeCount(): number {
return this._treeCount
}
}
export { SelectOneFromEachPairMap }
/**
* 给定一棵树,每个点有两个值。
* 对于v=1,2,3,...,n,问从点1到点 v的最短路径途径的每个点中,
* 各选一个数,其不同数的个数的最大值。
* @link https://atcoder.jp/contests/abc302/tasks/abc302_h
*/
function ballCollector(n: number, edges: [number, number][], pairs: [number, number][]): number[] {
const uf = new SelectOneFromEachPairMap()
const adjList: number[][] = Array(n)
for (let i = 0; i < n; i++) adjList[i] = []
edges.forEach(([u, v]) => {
adjList[u].push(v)
adjList[v].push(u)
})
const res: number[] = Array(n).fill(0)
dfs(0, -1)
return res
function dfs(cur: number, pre: number): void {
const [a, b] = pairs[cur]
uf.union(a, b)
res[cur] = uf.solve()
adjList[cur].forEach(next => {
if (next !== pre) {
dfs(next, cur)
}
})
uf.undo()
}
}