240910 K진 트리

Jongleee·2024년 9월 10일
0

TIL

목록 보기
674/737
public static void main(String[] args) throws IOException {
	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	StringTokenizer st = new StringTokenizer(br.readLine());

	st.nextToken();
	int childSize = Integer.parseInt(st.nextToken());
	int findCount = Integer.parseInt(st.nextToken());

	StringBuilder result = new StringBuilder();
	while (findCount-- > 0) {
		st = new StringTokenizer(br.readLine());
		long node1 = Long.parseLong(st.nextToken());
		long node2 = Long.parseLong(st.nextToken());

		if (childSize == 1) {
			result.append(Math.abs(node1 - node2));
		} else {
			result.append(findDistance(node1, node2, childSize));
		}
		result.append("\n");
	}

	System.out.println(result.toString());
}

private static long findDistance(long node1, long node2, int childSize) {
	long distance = 0;
	while (node1 != node2) {
		distance++;
		if (node1 > node2) {
			node1 = getParent(node1, childSize);
		} else {
			node2 = getParent(node2, childSize);
		}
	}
	return distance;
}

private static long getParent(long node, int childSize) {
	return (node - 2) / childSize + 1;
}

출처:https://www.acmicpc.net/problem/11812

0개의 댓글