Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4-mong3125 #15

Merged
merged 2 commits into from
Feb 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions mong3125/DFS/P023_11724.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package CH05_ํƒ์ƒ‰;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class P023_11724 {
static int N, M;
static boolean[][] edges;
static boolean[] cache;

public static void main(String[] args) throws IOException {
// ์ž…๋ ฅ
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());

// ๋…ธ๋“œ์˜ ์ˆ˜, ์—ฃ์ง€์˜ ์ˆ˜ ์ž…๋ ฅ
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());

edges = new boolean[N + 1][N + 1];
cache = new boolean[N + 1];

// ์—ฃ์ง€ ์ž…๋ ฅ
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());

// ์–‘๋ฐฉํ–ฅ ์ž…๋ ฅ
edges[a][b] = true;
edges[b][a] = true;
}

int count = 0;
for (int i = 1; i <= N; i++) {
if (dfs(i)) {
count++;
}
}

System.out.println(count);

br.close();
}

public static boolean dfs(int id) {
if (cache[id]) return false;

cache[id] = true;

for (int i = 1; i <= N; i++) {
if (edges[id][i]) {
dfs(i);
}
}

return true;
}
}