티스토리 뷰
https://www.acmicpc.net/problem/17352
class Edge {
constructor() {
this.src = 0;
this.dest = 0;
}
}
class SubSet {
constructor(i) {
this.parent = i;
this.rank = 0;
}
}
class UF {
constructor(N) {
this.subsets = Array.from(Array(N), (_, i) => {
return new SubSet(i);
});
}
find(i) {
if (this.subsets[i].parent == i) {
return i;
} else {
return this.find(this.subsets[i].parent);
}
}
union(x, y) {
const xroot = this.find(x);
const yroot = this.find(y);
if (this.subsets[xroot].rank < this.subsets[yroot].rank) {
this.subsets[xroot].parent = yroot;
} else if (this.subsets[xroot].rank > this.subsets[yroot].rank) {
this.subsets[yroot].parent = xroot;
} else {
this.subsets[xroot].parent = yroot;
this.subsets[yroot].rank++;
}
}
}
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout,
});
let uf;
readline.on('line', function (line) {
const input = line.split(' ').map(Number);
if (input.length == 1) {
uf = new UF(input[0] + 1);
} else {
const [s, d] = input;
uf.union(s, d);
}
}).on('close', function () {
let p = uf.find(1);
for (let i = 2; i < uf.subsets.length; i++) {
if (p != uf.find(i)) {
console.log(1, i);
process.exit();
}
}
});
728x90
'자료구조 알고리즘 > 백준' 카테고리의 다른 글
Node.js) 백준 10987번: 모음의 개수 (0) | 2023.09.03 |
---|---|
Node.js) 백준 1327번: 소트 게임 (0) | 2023.09.01 |
Node.js) 백준 2800번: 괄호 제거 (0) | 2023.09.01 |
Node.js) 백준 1374번: 강의실 (0) | 2023.08.31 |
Node.js)백준 19598번: 최소 회의실 개수 (0) | 2023.08.31 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- create databases;
- 투포인터
- create db
- MySQL
- 최소공통조상
- 개발자면접
- node.js
- BFS
- 서버점검
- 다이나밍프로그래밍
- 서버개발
- MOD
- 투포인터 연습
- 다이나믹프로그래밍
- 그래프
- 면접비
- 동적프로그래밍
- 면접질문
- DB 생성
- 은둔청년체험
- 로드나인
- 롱베케이션
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함