baekjoon 20260326

This commit is contained in:
songyc macbook 2026-03-26 23:16:40 +09:00
parent bbff86fc50
commit 38ed2c8f91
2 changed files with 30 additions and 0 deletions

View File

@ -0,0 +1,28 @@
export {};
const input = require("fs").readFileSync(0).toString().trim().split("\n").map(Number);
const [G, P]: number[] = [input[0], input[1]]
const gi: number[] = input.slice(2);
let roots: number[] = Array.from({length : G+1}, (_, i) => i);
const find = (g: number): number => {
if(roots[g] !== g) roots[g] = find(roots[g]);
return roots[g];
}
const union = (a: number, b: number) => {
roots[find(a)] = find(b);
}
let ans: number = 0;
for(let i=0; i<P; i++) {
const p = find(gi[i]);
if(p === 0) break;
union(p, p-1);
ans++;
}
console.log(ans);

View File

@ -0,0 +1,2 @@
export {};
console.log(require("fs").readFileSync(0).toString().trim());