Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- 자바
- 알고리즘
- java 올림
- Algorithm
- Math.ceil()
- java 반올림
- mysql
- Codility
- 백준 16935
- 백준 16927
- 프로그래머스 네트워크 java
- 네트워크
- 프로그래머스 숫자의 표현 java
- 프로그래머스 연속된 수의 합 java
- 코딩테스트
- sort
- 백준 18290
- 백준 17425
- 백준 15661
- Arrays
- Math.floor()
- 프로그래머스 도둑질 java
- java 내림
- java
- 프로그래머스 옹알이 java
- 백준 4375
- time complexity
- 백준 11723
- 0으로 채우기
- 백준 14391
Archives
- Today
- Total
취미처럼
[백준] 11724번 연결 요소의 개수 본문
https://www.acmicpc.net/problem/11724
11724번: 연결 요소의 개수
첫째 줄에 정점의 개수 N과 간선의 개수 M이 주어진다. (1 ≤ N ≤ 1,000, 0 ≤ M ≤ N×(N-1)/2) 둘째 줄부터 M개의 줄에 간선의 양 끝점 u와 v가 주어진다. (1 ≤ u, v ≤ N, u ≠ v) 같은 간선은 한 번만 주
www.acmicpc.net
연결요소의 개수 : 끊기지 않고 연결된 부분의 개수
import java.util.*;
import java.io.*;
public class Main {
// 정점 개수
static int N;
// 간선 개수
static int M;
static ArrayList<Integer>[] list;
static boolean[] visit;
static StringBuilder sb = new StringBuilder();
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
M = sc.nextInt();
list = new ArrayList[N+1];
for(int i=0; i < list.length; i++) {
list[i] = new ArrayList<Integer>();
}
visit = new boolean[N + 1];
for(int i = 0; i < M; i ++) {
int a = sc.nextInt();
int b = sc.nextInt();
list[a].add(b);
list[b].add(a);
}
int count = 0;
for(int i=1; i <= N; i++) {
if(!visit[i]) {
dfs(i);
count++;
}
}
System.out.println(count);
}
public static void dfs(int index) {
if(visit[index]) {
return;
} else {
visit[index] = true;
for(int i : list[index]) {
dfs(i);
}
}
}
}'Algorithm > 백준' 카테고리의 다른 글
| [백준] 2667번 단지번호붙이기 (0) | 2021.03.11 |
|---|---|
| [백준] 1707번 이분 그래프 (0) | 2021.03.11 |
| [백준] 1260번 DFS와 BFS (0) | 2021.03.09 |
| [백준] 13023번 ABCDE (0) | 2021.03.09 |
| [백준] 10866번 덱 (0) | 2021.03.09 |
Comments