-
Notifications
You must be signed in to change notification settings - Fork 1
/
P1167.js
40 lines (40 loc) · 1.3 KB
/
P1167.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const rl = require('readline').createInterface(process.stdin)
let v, list = [], x
rl.on('line', line => {
if (!v) {
x = v = +line
list.length = v + 1
} else {
const edges = line.split(' ')
const conn = []
for (let i = 2; i < edges.length; i += 2) {
conn.push([+edges[i - 1], +edges[i]])
}
list[+edges[0]] = conn
if (--x === 0) {
const dfs = from => {
const found = Array.from({ length: v + 1 }, () => false)
found[from] = true
const next = [[from, 0]]
let farthest = from, distance = 0
while (next.length > 0) {
const [cur, len] = next.pop()
if (len > distance) {
farthest = cur
distance = len
}
for (const [node, d] of list[cur]) {
if (!found[node]) {
found[node] = true
next.push([node, len + d])
}
}
}
return [farthest, distance]
}
const [x, _d] = dfs(1)
const [_y, dist] = dfs(x)
console.log(dist)
}
}
})