27 lines
691 B
TypeScript
27 lines
691 B
TypeScript
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')); |