Skip to content

Commit a13e35a

Browse files
committed
Feat: 골드4 사회망 서비스
1 parent ad73e30 commit a13e35a

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const local_input = `
2+
8
3+
1 2
4+
1 3
5+
1 4
6+
2 5
7+
2 6
8+
4 7
9+
4 8
10+
`;
11+
12+
const input = process.execArgv.includes("--stack-size=65536")
13+
? require("fs").readFileSync("dev/stdin").toString()
14+
: local_input;
15+
const inputs = input
16+
.trim()
17+
.split("\n")
18+
.slice(1)
19+
.map((v) => v.split(" ").map((v2) => Number(v2)));
20+
21+
const n = Number(input.trim().split("\n")[0]);
22+
const dp = Array.from({ length: n + 1 }, () =>
23+
Array.from({ length: 2 }, () => 0)
24+
);
25+
26+
const map = {};
27+
const visited = Array.from({ length: n + 1 }, () => false);
28+
inputs.forEach((v) => {
29+
if (map[v[0]]) map[v[0]].push(v[1]);
30+
else map[v[0]] = [v[1]];
31+
if (map[v[1]]) map[v[1]].push(v[0]);
32+
else map[v[1]] = [v[0]];
33+
});
34+
35+
const dfs = (cur) => {
36+
visited[cur] = true;
37+
dp[cur][0] = 0;
38+
dp[cur][1] = 1;
39+
40+
for (const node of map[cur]) {
41+
if (visited[node]) continue;
42+
dfs(node);
43+
dp[cur][0] += dp[node][1];
44+
dp[cur][1] += Math.min(dp[node][0], dp[node][1]);
45+
}
46+
};
47+
48+
dfs(1);
49+
console.log(Math.min(dp[1][0], dp[1][1]));

0 commit comments

Comments
 (0)