• 문제 링크
22035번: Bus Lines
If it is not possible to construct a graph with the given properties, print "-1". Otherwise, print $m$ lines where the $i$'th line contains two integers $a_i$, $b_i$, the endpoints of the $i$'th edge. If there are many possible solutions, any one of them w
www.acmicpc.net
• 풀이 코드
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = read(), m = read();
bw.write(m < n - 1 ? "-1" : connect(n, m));
bw.flush();
}
private static String connect(int n, int m) {
List<Edge> list = new ArrayList<>();
for (int i = 2; i <= n; i++) list.add(new Edge(1, i));
for (int i = 2; i < n; i++) list.add(new Edge(i, n));
return list.size() < m ? "-1" : extract(list, m);
}
private static String extract(List<Edge> list, int m) {
StringBuilder sb = new StringBuilder();
while (m-- > 0) sb.append(list.get(m).extract()).append("\n");
return sb.toString();
}
private static class Edge {
int u, v;
Edge(int u, int v) {
this.u = u;
this.v = v;
}
String extract() {
return u + " " + v;
}
}
private static int read() throws IOException {
int c, n = System.in.read() & 15;
while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);
return n;
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 9329 패스트 푸드 상금 - Greedy / Java (0) | 2024.03.11 |
---|---|
[백준] 9372 상근이의 여행 - Graph Theory / Java (0) | 2024.03.10 |
[백준] 5247 Mining Maps - Graph Theory / Java (0) | 2024.03.08 |
[백준] 20914 QWERTY 자판 - Graph Theory / Java (0) | 2024.03.07 |
[백준] 6179 Oh Those Rollers - Graph Theory / Java (0) | 2024.03.06 |
댓글