-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.js
34 lines (34 loc) · 960 Bytes
/
solution.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
const filePath = './input';
const [num, ...arr] = require('fs')
.readFileSync(filePath)
.toString()
.trim()
.split('\n');
const [n, m] = num.split(' ').map(Number);
function solution (n, m, arr) {
const graph = Array.from({ length: n + 1 }, () => []);
const indegress = new Array(n + 1).fill(0)
const result = new Array(n).fill(1)
for (let [from, to] of arr) {
graph[from].push(to);
indegress[to]++;
}
const queue = [];
for (let i = 1; i <= n; i ++) {
if (!indegress[i]) {
queue.push(i);
}
}
let j = 0;
while (j < queue.length) {
const to = queue[j++];
for (let node of graph[to]) {
if (--indegress[node] === 0) {
result[node - 1] = result[to - 1] + 1;
queue.push(node);
}
}
}
return result.join(' ');
}
console.log(solution(n, m, arr.map(v => v.split(' ').map(Number))));