Skip to content

Commit fe04a81

Browse files
authored
[20250305] BOJ / G3 / K진 트리 / 신동윤
1 parent a0e3634 commit fe04a81

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
```java
2+
import java.util.*;
3+
4+
public class Main {
5+
static int K;
6+
public static void main(String[] args) {
7+
Scanner sc = new Scanner(System.in);
8+
long N = sc.nextLong();
9+
K = sc.nextInt();
10+
int Q = sc.nextInt();
11+
for (int i = 0; i < Q; i++) {
12+
long x = sc.nextLong() - 1;
13+
long y = sc.nextLong() - 1;
14+
if (K == 1) {
15+
System.out.println(Math.abs(x - y));
16+
} else {
17+
int dist = 0;
18+
while (x != y) {
19+
if (x < y) {
20+
y = (y - 1) / K; // y = y의 부모 노드
21+
} else {
22+
x = (x - 1) / K; // x = x의 부모 노드
23+
}
24+
dist++;
25+
}
26+
System.out.println(dist);
27+
}
28+
}
29+
sc.close();
30+
}
31+
}
32+
33+
```

0 commit comments

Comments
 (0)