Skip to content

Commit 4244fc3

Browse files
authored
Create 1719-number-of-ways-to-reconstruct-a-tree.js
1 parent 8d648f7 commit 4244fc3

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
class PriorityQueue {
2+
constructor(comparator = (a, b) => a > b) {
3+
this.heap = []
4+
this.top = 0
5+
this.comparator = comparator
6+
}
7+
size() {
8+
return this.heap.length
9+
}
10+
isEmpty() {
11+
return this.size() === 0
12+
}
13+
peek() {
14+
return this.heap[this.top]
15+
}
16+
push(...values) {
17+
values.forEach((value) => {
18+
this.heap.push(value)
19+
this.siftUp()
20+
})
21+
return this.size()
22+
}
23+
pop() {
24+
const poppedValue = this.peek()
25+
const bottom = this.size() - 1
26+
if (bottom > this.top) {
27+
this.swap(this.top, bottom)
28+
}
29+
this.heap.pop()
30+
this.siftDown()
31+
return poppedValue
32+
}
33+
replace(value) {
34+
const replacedValue = this.peek()
35+
this.heap[this.top] = value
36+
this.siftDown()
37+
return replacedValue
38+
}
39+
40+
parent = (i) => ((i + 1) >>> 1) - 1
41+
left = (i) => (i << 1) + 1
42+
right = (i) => (i + 1) << 1
43+
greater = (i, j) => this.comparator(this.heap[i], this.heap[j])
44+
swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]])
45+
siftUp = () => {
46+
let node = this.size() - 1
47+
while (node > this.top && this.greater(node, this.parent(node))) {
48+
this.swap(node, this.parent(node))
49+
node = this.parent(node)
50+
}
51+
}
52+
siftDown = () => {
53+
let node = this.top
54+
while (
55+
(this.left(node) < this.size() && this.greater(this.left(node), node)) ||
56+
(this.right(node) < this.size() && this.greater(this.right(node), node))
57+
) {
58+
let maxChild =
59+
this.right(node) < this.size() &&
60+
this.greater(this.right(node), this.left(node))
61+
? this.right(node)
62+
: this.left(node)
63+
this.swap(node, maxChild)
64+
node = maxChild
65+
}
66+
}
67+
}
68+
/**
69+
* @param {number[][]} pairs
70+
* @return {number}
71+
*/
72+
const checkWays = function (pairs) {
73+
const adj = {}
74+
for (let i = 0; i < pairs.length; i++) {
75+
if (adj[pairs[i][0]] == null) adj[pairs[i][0]] = new Set()
76+
if (adj[pairs[i][1]] == null) adj[pairs[i][1]] = new Set()
77+
adj[pairs[i][0]].add(pairs[i][1])
78+
adj[pairs[i][1]].add(pairs[i][0])
79+
}
80+
81+
const q = new PriorityQueue((a, b) => a[0] < b[0])
82+
Object.keys(adj).forEach((k) => {
83+
q.push([-adj[k].size, +k])
84+
})
85+
86+
const n = q.size()
87+
let multiple = false
88+
const seen = new Set()
89+
while (!q.isEmpty()) {
90+
let [sz, v] = q.peek()
91+
q.pop()
92+
sz = -sz
93+
let u = 0
94+
let usz = n + 1
95+
if (seen.size) {
96+
for (let x of adj[v]) {
97+
if (adj[x].size < usz && seen.has(x)) {
98+
u = x
99+
usz = adj[x].size
100+
}
101+
}
102+
}
103+
104+
seen.add(v)
105+
if (u === 0) {
106+
if (sz !== n - 1) {
107+
return 0
108+
}
109+
continue
110+
}
111+
112+
for (let x of adj[v]) {
113+
if (x == u) {
114+
continue
115+
}
116+
117+
if (!adj[u].has(x)) {
118+
return 0
119+
}
120+
}
121+
122+
if (usz == sz) {
123+
multiple = true
124+
}
125+
}
126+
127+
if (multiple) {
128+
return 2
129+
}
130+
131+
return 1
132+
}

0 commit comments

Comments
 (0)