λ¬Έμ
νμ΄
1οΈβ£ νλ‘μ΄λ μκ³ λ¦¬μ¦ μ¬μ©
π‘ λ μ€λ₯Έ Idea
μ²μμ λ¬Έμ κ°
'μ΄λ νμμ΄ λ€λ₯Έ λͺ¨λ νμκ³Ό μΉκ΅¬μ΄λ©΄, μ΄ νμμ μ μλ 1μ μ΄λ€. μ΄λ νμμ μ μκ° 2μ μ΄λ©΄, λ€λ₯Έ λͺ¨λ νμμ΄ μΉκ΅¬μ΄κ±°λ μΉκ΅¬μ μΉκ΅¬μμ λ§νλ€. λν μ΄λ νμμ μ μκ° 3μ μ΄λ©΄, λ€λ₯Έ λͺ¨λ νμμ΄ μΉκ΅¬μ΄κ±°λ, μΉκ΅¬μ μΉκ΅¬μ΄κ±°λ, μΉκ΅¬μ μΉκ΅¬μ μΉκ΅¬μμ λ§νλ€.'
λΌκ³ λμμμ΄μ μ΄ν΄νκΈ°κ° νλ€μμλ€.
κ·Όλ°, μ΄ λ¬Έμ λ₯Ό μ§κ΄μ μΌλ‘ μ΄ν΄ν΄λ³΄λ
'νΉμ νμμ΄ μμ λ κ°μ₯ μ¬μ΄κ° λ¨Ό νμμ λͺλͺ μ κ±°μ³μ κ°μΌνλκ°'
λ‘ ν΄μνλ©΄ λ¨μ μ μ μμλ€.
λ°λΌμ, νλ‘μ΄λ μκ³ λ¦¬μ¦μ ν΅ν΄ νΉμ νμμ΄ λ€λ₯Έ νμμ λλ¬νλ λΉμ©μ νλ‘μ΄λ μκ³ λ¦¬μ¦μ μ΄μ©νμ¬ κ΅¬νκ³
κ° νμ λ³ μ΅λ λΉμ©μ ꡬν λ€, κ·Έ λΉμ©μ μ΅μκ°μ ꡬνκ³ ν΄λΉ μ΅μκ°μ κ°λ νμλ€μ λͺ¨λ ꡬνλ©΄ λμ΄ λ κ² μ΄λ€.
public class Main {
public static void main(String[] args) throws IOException {
final int MAX = 100;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
int[][] graph = new int[N][N];
// μκΈ° μμ μΌλ‘ κ°λ κ²½λ‘ μΈμ μ΅λκ°μΌλ‘ λ³ν
for (int r = 0; r < N; r++) {
for (int c = 0; c < N; c++) {
if (r == c) {
graph[r][c] = 0;
} else {
graph[r][c] = MAX;
}
}
}
// κ°μ₯ κ°κΉμ΄ μ¬μ΄λ 1λ‘ λ¨Όμ μ
λ ₯ν΄μ€
while (true) {
String[] input = br.readLine().split(" ");
if (input[0].equals("-1")) {
break;
}
int u = Integer.parseInt(input[0] )- 1;
int v = Integer.parseInt(input[1]) - 1;
graph[u][v] = 1;
graph[v][u] = 1;
}
// νλ‘μ΄λ μκ³ λ¦¬μ¦ μ μ©
for (int m = 0; m < N; m++) {
for (int u = 0; u < N; u++) {
for (int v = 0; v < N; v++) {
graph[u][v] = Math.min(graph[u][v], graph[u][m] + graph[m][v]);
}
}
}
// 맡μ μ μ λ³ νμλ€μ κΈ°λ‘
Map<Integer, List<Integer>> scores = new HashMap<>();
int minScore = MAX;
for (int r = 0; r < N; r++) {
int maxScoreByMember = Arrays.stream(graph[r]).max().getAsInt();
minScore = Math.min(minScore, maxScoreByMember);
if (scores.containsKey(maxScoreByMember)) {
scores.get(maxScoreByMember).add(r + 1);
} else {
List<Integer> members = new ArrayList<>();
members.add(r + 1);
scores.put(maxScoreByMember, members);
}
}
List<Integer> members = scores.get(minScore);
StringBuilder sb = new StringBuilder();
sb.append(minScore + " " + members.size() + "\n");
for (int member : members) {
sb.append(member + " ");
}
System.out.println(sb);
}
}
κ²°κ³Ό
λ§μ§λ§μ λ°μ΄ν°λ₯Ό μ²λ¦¬νλ κ³Όμ μ μ μΈνλ©΄ νλ‘μ΄λ μκ³ λ¦¬μ¦μ μ¬μ©νλ©΄ μ½κ² ν΄κ²°ν μ μλ λ¬Έμ μλ€.