https://www.acmicpc.net/problem/2644
우리 나라는 가족 혹은 친척들 사이의 관계를 촌수라는 단위로 표현하는 독특한 문화를 가지고 있다. 이러한 촌수는 다음과 같은 방식으로 계산된다. 기본적으로 부모와 자식 사이를 1촌으로 정의하고 이로부터 사람들 간의 촌수를 계산한다. 예를 들면 나와 아버지, 아버지와 할아버지는 각각 1촌으로 나와 할아버지는 2촌이 되고, 아버지 형제들과 할아버지는 1촌, 나와 아버지 형제들과는 3촌이 된다.
여러 사람들에 대한 부모 자식들 간의 관계가 주어졌을 때, 주어진 두 사람의 촌수를 계산하는 프로그램을 작성하시오.
사람들은 1, 2, 3, …, n (1 ≤ n ≤ 100)의 연속된 번호로 각각 표시된다. 입력 파일의 첫째 줄에는 전체 사람의 수 n이 주어지고,
둘째 줄에는 촌수를 계산해야 하는 서로 다른 두 사람의 번호가 주어진다. 그리고 셋째 줄에는 부모 자식들 간의 관계의 개수 m이 주어진다.
넷째 줄부터는 부모 자식간의 관계를 나타내는 두 번호 x,y가 각 줄에 나온다.
이때 앞에 나오는 번호 x는 뒤에 나오는 정수 y의 부모 번호를 나타낸다.
각 사람의 부모는 최대 한 명만 주어진다.
입력에서 요구한 두 사람의 촌수를 나타내는 정수를 출력한다.
어떤 경우에는 두 사람의 친척 관계가 전혀 없어 촌수를 계산할 수 없을 때가 있다. 이때에는 -1을 출력해야 한다.
9
7 3
7
1 2
1 3
2 7
2 8
2 9
4 5
4 6
3
9
8 6
7
1 2
1 3
2 7
2 8
2 9
4 5
4 6
-1
5
1 5
4
1 2
2 3
3 4
4 5
4
5
1 5
4
1 2
1 3
2 4
3 5
2

package graph_search;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main_2644 {
private static int totalNumber;
private static int countOfRelation;
private static ArrayList<Integer>[] adjacencyList;
private static boolean[] visited;
private static int result = Integer.MAX_VALUE;
private static Person firstPerson;
private static Person secondPerson;
static class Person {
int number;
int count;
public Person(int number, int count) {
this.number = number;
this.count = count;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
public static void main(String[] args) throws IOException {
input();
process();
output();
}
private static void input() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
totalNumber = Integer.parseInt(st.nextToken());
adjacencyList = new ArrayList[totalNumber + 1]; // 1 ~ totalNumber
visited = new boolean[totalNumber + 1];
for (int i = 1; i <= totalNumber; i++) {
adjacencyList[i] = new ArrayList<>();
}
st = new StringTokenizer(br.readLine());
firstPerson = new Person(Integer.parseInt(st.nextToken()),0);
secondPerson = new Person(Integer.parseInt(st.nextToken()), 0);
st = new StringTokenizer(br.readLine());
countOfRelation = Integer.parseInt(st.nextToken());
for (int i = 0; i < countOfRelation; i++) {
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
adjacencyList[a].add(b);
adjacencyList[b].add(a);
}
}
private static void process() {
bfs();
if (result == Integer.MAX_VALUE) {
result = -1;
}
}
private static void bfs() {
Queue<Person> queue = new LinkedList<>();
queue.add(firstPerson);
visited[firstPerson.getNumber()] = true;
while (!queue.isEmpty()) {
Person nextPerson = queue.poll();
for (Integer personNumber : adjacencyList[nextPerson.getNumber()]) {
if (visited[personNumber]) {
continue;
}
if (personNumber == secondPerson.getNumber()) {
result = nextPerson.getCount() + 1;
return;
}
queue.add(new Person(personNumber, nextPerson.getCount() + 1));
visited[personNumber] = true;
}
}
}
private static void output() {
System.out.println(result);
}
}