백준/분리 집합
-
백준 20040 nodejs백준/분리 집합 2022. 12. 21. 17:36
문제 https://www.acmicpc.net/problem/20040 풀이 Union-Find 알고리즘을 사용한다. (1197번 문제와 유사함) let [[n], ...I] = require('fs').readFileSync('/dev/stdin').toString().trim().split('\n').map(e => e.split(' ').map(Number)) const R = [...Array(n)].map((_, i) => i) const root = x => (x == R[x] ? x : (R[x] = root(R[x]))) let o = 0 for (const i in I) { const [a, b] = I[i] const [x, y] = [root(a), root(b)] if (x == ..