Skip to content

Commit ce7ef04

Browse files
committed
Set repr / 심화
1 parent 248d885 commit ce7ef04

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const readline = require("readline")
2+
const rl = readline.createInterface({
3+
input: process.stdin,
4+
output: process.stdout,
5+
})
6+
7+
const input = []
8+
rl.on("line", (line) => {
9+
input.push(line)
10+
}).on("close", () => {
11+
const [n, m] = input[0].split(" ").map(Number)
12+
const parent = Array.from({ length: n + 1 }, (_, idx) => idx)
13+
14+
const findParent = (x, parent) => {
15+
if (parent[x] !== x) {
16+
parent[x] = findParent(parent[x], parent)
17+
}
18+
return parent[x]
19+
}
20+
const union = (a, b) => {
21+
a = findParent(a, parent)
22+
b = findParent(b, parent)
23+
if (a < b) parent[b] = a
24+
else parent[a] = b
25+
}
26+
27+
let result = ""
28+
for (let i = 1; i <= m; i++) {
29+
let [op, a, b] = input[i].split(" ").map(Number)
30+
if (op === 0) {
31+
union(a, b)
32+
} else {
33+
if (findParent(a, parent) === findParent(b, parent)) result += "YES\n"
34+
else result += "NO\n"
35+
}
36+
}
37+
38+
console.log(result.trim())
39+
process.exit()
40+
})
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
7 8
2+
0 1 3
3+
1 1 7
4+
0 7 6
5+
1 7 1
6+
0 3 7
7+
0 4 2
8+
0 1 1
9+
1 1 1

0 commit comments

Comments
 (0)