CS/Algorithm

[백준:1389] 케빈 베이컨의 6단계 법칙 - JAVA

yujindonut 2021. 4. 29. 19:23
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