728x90
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main {
static int N, M;
static int[][] map;
static boolean[] visited;
static Queue<Integer> queue;
static int count = 0, answer = 1, total = 0, min = Integer.MAX_VALUE;
static int[] check;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
M = sc.nextInt();
map = new int[N + 1][N + 1];
queue = new LinkedList<Integer>();
for (int i = 0; i < M; i++) {
int a = sc.nextInt();
int b = sc.nextInt();
map[a][b] = 1;
map[b][a] = 1;
} // end of input
for (int i = 1; i < N + 1; i++) {
visited = new boolean[N + 1];
check = new int[N+1];
total = 0;
bfs(i);
if(total<min) {
min = total;
answer = i;
}
}
System.out.println(answer);
sc.close();
}
public static void bfs(int i) {
count = 0;
queue.offer(i); //add i
visited[i] = true;
while (!queue.isEmpty()) {
int len = queue.size();
count++;
for (int l = 0; l < len; l++) {
int nv = queue.poll();
for (int j = 1; j < N + 1; j++) {
if (map[nv][j] == 1 && !visited[j]) {
queue.offer(j);
visited[j] = true;
total += count;
}
}
}
}
}
}
728x90
'CS > Algorithm' 카테고리의 다른 글
[백준:2667] 단지번호 붙이기 - JAVA (0) | 2021.05.02 |
---|---|
[백준:1260번] DFS와 BFS - JAVA (0) | 2021.05.02 |
[백준:4963] 섬의 개수 - JAVA (0) | 2021.04.11 |
[백준: 2644] 촌수계산 - JAVA (0) | 2021.04.11 |
[백준:11724] 연결 요소의 개수 - JAVA (0) | 2021.04.11 |