Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b187f2f
commit 1315863
Showing
2 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
const fs = require('fs'); | ||
const filePath = process.platform === 'linux' ? '/dev/stdin' : './input.txt'; | ||
let input = fs.readFileSync(filePath).toString().trim().split('\n'); | ||
|
||
const [n, m] = input.shift().split(' ').map(Number); | ||
|
||
const arr = input.map((str) => str.split(' ').map(Number)); | ||
|
||
const tree = new Map(); | ||
|
||
Array.from({ length: n }, (_, i) => tree.set(i + 1, [])); | ||
|
||
arr.forEach((arr) => { | ||
const [start, end] = arr; | ||
|
||
tree.get(start).push(end); | ||
tree.get(end).push(start); | ||
}); | ||
|
||
const result = []; | ||
|
||
for (let i = 1; i <= n; i++) { | ||
const queue = [i]; | ||
const check = new Array(n + 1).fill(0); | ||
|
||
while (queue.length) { | ||
const node = queue.shift(); | ||
|
||
for (const next of tree.get(node)) { | ||
if (check[next]) continue; | ||
|
||
check[next] = check[node] + 1; | ||
queue.push(next); | ||
} | ||
} | ||
|
||
check[i] = 0; | ||
|
||
result.push(check.reduce((a, b) => a + b)); | ||
} | ||
|
||
console.log(result.indexOf(Math.min(...result)) + 1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
5 5 | ||
1 3 | ||
1 4 | ||
4 5 | ||
4 3 | ||
3 2 |