Skip to content

Commit

Permalink
08.09: disjoint set(집합의 표현)
Browse files Browse the repository at this point in the history
  • Loading branch information
luckylooky2 committed Aug 9, 2024
1 parent d831142 commit ee500bc
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions baekjoon/1717.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// 집합의 표현 : 분리 집합
const input = require("fs")
.readFileSync(0, "utf-8")
.toString()
.trim()
.split("\n")
.map((v) => v.split(" ").map((v) => Number(v)));
const [n, operationCount] = input.shift();
const operation = input;
const answer = [];
const [OP_INTEGRATE, OP_CHECK] = [0, 1];
const parents = new Array(n + 1).fill(0).map((_v, i) => i);

function getParents(x) {
if (parents[x] === x) {
return x;
}

return (parents[x] = getParents(parents[x]));
}

function union(x, y) {
x = getParents(x);
y = getParents(y);

if (x > y) {
parents[x] = y;
} else {
parents[y] = x;
}
}

function isSameParent(x, y) {
return getParents(x) === getParents(y);
}

for (const [op, a, b] of operation) {
if (op === OP_CHECK) {
answer.push(isSameParent(a, b) ? "YES" : "NO");
}
if (op === OP_INTEGRATE) {
union(a, b);
}
}

console.log(answer.join("\n"));

// 0, 1, 2, 3, 4, 5, 6, 7
// {1, 3}, 2, 4, 5, 6, 7
// {1, 3}, 2, 4, 5, {6, 7}
// {1, 3, 6, 7}, 2, 4, 5
// {1, 3, 6, 7}, {2, 4}, 5

// require("fs").readFileSync("/dev/stdin") 대신 require("fs").readFileSync(0, "utf-8")를 사용해야 권한 문제가 발생하지 않음

0 comments on commit ee500bc

Please sign in to comment.