From 98f68a7c7e21d6b046e9c7a7060150b5027a6833 Mon Sep 17 00:00:00 2001 From: songyc macbook Date: Sat, 26 Jul 2025 18:44:39 +0900 Subject: [PATCH] 20250726 baekjoon --- code_study/Baekjoon/ts/11724.ts | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 code_study/Baekjoon/ts/11724.ts diff --git a/code_study/Baekjoon/ts/11724.ts b/code_study/Baekjoon/ts/11724.ts new file mode 100644 index 0000000..095d3d5 --- /dev/null +++ b/code_study/Baekjoon/ts/11724.ts @@ -0,0 +1,28 @@ +export {}; + +function dfs(now: number, link: number[][], visited: boolean[]): boolean[]{ + if(!visited[now]) visited[now] = true; + for(let v of link[now]){ + if(!visited[v]) dfs(v, link, visited); + } + return visited; +} + +const input: string[] = require("fs").readFileSync(0, "utf8").toString().trim().split('\n'); +const [N, M] = input[0].split(' ').map(Number); +let visited: boolean[] = new Array(N+1).fill(false); +let link: number[][] = Array.from({length: N+1}, () => []); +for(let i=1; i<=M; i++) { + const [u,v] = input[i].split(' ').map(Number); + link[u].push(v); + link[v].push(u); +} + +let ConnectedComponentCount = 0 +for(let now = 1 ; now<=N; now++){ + if(!visited[now]){ + ConnectedComponentCount++; + dfs(now, link, visited); + } +} +console.log(ConnectedComponentCount);