• 문제 링크
21316번: 스피카
위 그림은 처녀자리 중 12개의 별을 12개의 선분으로 이어 만든 그림이다. 시은이는 임의로 각 별에 1부터 12까지의 서로 다른 정수 번호를 부여하고, 12개의 정수 쌍으로 각 선분이 어떤 두 별을
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 {
static Node[] graph = new Node[13];
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
for (int i = 1; i <= 12; i++) graph[i] = new Node(i);
for (int i = 0; i < 12; i++) {
int u = read(), v = read();
graph[u].addEdge(v);
graph[v].addEdge(u);
}
bw.write(String.valueOf(findSpica()));
bw.flush();
}
private static int findSpica() {
for (int i = 1; i <= 12; i++) {
Node node = graph[i];
if (node.adj.size() == 3) {
boolean a = false, b = false, c = false;
for (int src : node.adj) {
if (graph[src].adj.size() == 1) a = true;
else if (graph[src].adj.size() == 2) b = true;
else if (graph[src].adj.size() == 3) c = true;
}
if (a && b && c) return node.src;
}
}
return -1;
}
private static class Node {
int src;
List<Integer> adj;
Node(int src) {
this.src = src;
this.adj = new ArrayList<>();
}
public void addEdge(int tgt) {
adj.add(tgt);
}
}
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' 카테고리의 다른 글
| [백준] 26169 세 번 이내에 사과를 먹자 - Graph Theory / Java (0) | 2023.10.15 |
|---|---|
| [백준] 13237 Binary tree - Graph Theory / Java (0) | 2023.10.14 |
| [백준] 13399 Rearranging a Sequence - Data Structure / Java (0) | 2023.10.12 |
| [백준] 17287 The Deeper, The Better - Data Structure / Java (0) | 2023.10.11 |
| [백준] 28446 볼링공 찾아주기 - Data Structure / Java (0) | 2023.10.10 |
댓글