20250824 baekjoon

This commit is contained in:
songyc macbook 2025-08-24 21:58:15 +09:00
parent bd935df374
commit d50679b5cf

View File

@ -0,0 +1,27 @@
export {};
const input: string[] = require("fs").readFileSync(0, 'utf8').toString().trim().split('\n');
const N: number = Number(input[0]);
let linked: number[][] = Array.from({length: N+1}, () => []);
for(let i=1; i<N; i++) {
const [u, v] : number[] = input[i].split(' ').map(Number);
linked[u].push(v);
linked[v].push(u);
}
let root: number[] = Array.from({length: N+1}, () => 0);
root[1] = -1;
let queue: number[] = [1];
while(queue.length !== 0) {
const current: number = queue.shift()!;
for(let v of linked[current]) {
if(root[v] === 0) {
root[v] = current;
queue.push(v);
}
}
}
console.log(root.slice(2).join('\n'));