20250824 baekjoon
This commit is contained in:
parent
bd935df374
commit
d50679b5cf
27
code_study/Baekjoon/ts/11725.ts
Normal file
27
code_study/Baekjoon/ts/11725.ts
Normal 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'));
|
||||
Loading…
x
Reference in New Issue
Block a user