Skip to content

Commit cccfb29

Browse files
committed
Feat: 골드4 사회망 서비스
1 parent 64b235c commit cccfb29

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

Diff for: 4주차/박종운/골드4__사회망_서비스.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const readline = require('readline');
2+
3+
const rl = readline.createInterface({
4+
input: process.execArgv.includes("--stack-size=65536") ?
5+
process.stdin : require('fs').createReadStream(__dirname + '/input.txt'),
6+
output: process.stdout
7+
});
8+
9+
let N;
10+
let graph;
11+
let dp;
12+
let visited;
13+
14+
function dfs(cur) {
15+
visited[cur] = true;
16+
17+
for (const child of graph[cur]) {
18+
if (!visited[child]) {
19+
dfs(child);
20+
dp[cur][0] += dp[child][1];
21+
dp[cur][1] += Math.min(...dp[child]);
22+
}
23+
}
24+
}
25+
26+
rl.question('', input => {
27+
N = parseInt(input);
28+
graph = Array.from({ length: N + 1 }, () => []);
29+
dp = Array.from({ length: N + 1 }, () => [0, 1]);
30+
visited = Array(N + 1).fill(false);
31+
32+
rl.on('line', line => {
33+
const [u, v] = line.split(' ').map(Number);
34+
graph[u].push(v);
35+
graph[v].push(u);
36+
}).on('close', () => {
37+
dfs(1);
38+
console.log(Math.min(...dp[1]));
39+
40+
process.exit();
41+
});
42+
});

0 commit comments

Comments
 (0)