문제 >> 골드바흐의 추측
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 |
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
class pair {
int now;
int weight;
public pair(int now, int weight) {
super();
this.now = now;
this.weight = weight;
}
}
public class Main {
static int n,m;
static boolean[][] map;
static int[] rst;
static Scanner sc = new Scanner(System.in);
static int bfs(int start) {
boolean[] visited = new boolean[n+1];
Queue<pair> q = new LinkedList<pair>();
visited[start]=true;
q.add(new pair(start,0));
int score=0;
while(!q.isEmpty()) {
int now = q.peek().now;
int weight = q.peek().weight;
score+=weight;
q.poll();
for(int i=1;i<=n;i++) {
if(map[now][i] && !visited[i]) {
visited[i]=true;
q.add(new pair(i,weight+1));
}
}
}
return score;
}
public static void main(String[] args) {
n = sc.nextInt(); // 유저의 수
m = sc.nextInt(); // 관계 수
map = new boolean[n+1][n+1];
rst = new int[n+1];
for(int i=0;i<m;i++) {
int a=sc.nextInt();
int b=sc.nextInt();
map[a][b] = true;
map[b][a] = true;
}
int min=Integer.MAX_VALUE;
int ans = 0;
for(int i=1;i<=n;i++) {
rst[i] = bfs(i);
if(rst[i]<min) {
min = rst[i];
ans = i;
}
}
System.out.println(ans);
}//end of main
}
|
cs |
'Algorithm Problem Solving' 카테고리의 다른 글
[BOJ] 1987 : 알파벳 (0) | 2019.02.08 |
---|---|
[BOJ] 10798 : 세로읽기 (0) | 2019.02.08 |
[BOJ]1389: 케빈 베이컨의 6단계 법칙 (0) | 2019.02.08 |
[정올] 1809 : 탑 (0) | 2019.01.22 |
5432. 쇠막대기 자르기 (0) | 2019.01.15 |