Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2-g0rnn #7

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
10 changes: 6 additions & 4 deletions g0rnn/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## โœ๏ธ ๊ธฐ๋ก
## โœ๏ธ ๊ธฐ๋ก

| ์ฐจ์‹œ | ๋‚ ์งœ | ๋ฌธ์ œ์œ ํ˜• | ๋งํฌ | ํ’€์ด |
| :---: | :--------: | :------: | :--------------------------------------------------------------------------: | :------------------------------------------------: |
| 1์ฐจ์‹œ | 2024.10.01 | DFS | [ํƒ€๊นƒ ๋„˜๋ฒ„](https://school.programmers.co.kr/learn/courses/30/lessons/43165) | https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/4 |
| 2์ฐจ์‹œ | 2024.10.03 | ํŠธ๋ฆฌ | [LCA2](https://school.programmers.co.kr/learn/courses/30/lessons/176963) | https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/7 |

| ์ฐจ์‹œ | ๋‚ ์งœ | ๋ฌธ์ œ์œ ํ˜• | ๋งํฌ | ํ’€์ด |
|:----:|:---------:|:----:|:-----:|:----:|
| 1์ฐจ์‹œ | 2024.10.01 | ๊ตฌํ˜„ | [์ถ”์–ต ์ ์ˆ˜](https://school.programmers.co.kr/learn/courses/30/lessons/176963)|https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/35|
---
94 changes: 94 additions & 0 deletions g0rnn/ํŠธ๋ฆฌ/2-g0rnn.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
public class Main {
static final int MAX = 18; // ๋…ธ๋“œ๋Š” ์ตœ๋Œ€ 100,000๊ฐœ
static int N, M;
static int[][] parent;
static int[] depth;
static List<Integer>[] adj;

// dfs๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ 2์ฐจ์› ๋ฐฐ์—ด parent๋ฅผ ์ดˆ๊ธฐํ™”
public static void makeTree(int curNode) {
for (int child : adj[curNode]) {
if (depth[child] == -1) {
depth[child] = depth[curNode] + 1;
parent[child][0] = curNode;
makeTree(child);
}
}
}

public static void main(String[] args) throws IOException {
//TIP Press <shortcut actionId="ShowIntentionActions"/> with your caret at the highlighted text
// to see how IntelliJ IDEA suggests fixing it.
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

N = Integer.parseInt(br.readLine());
parent = new int[N][MAX];
depth = new int[N];
adj = new ArrayList[N];

for (int i = 0; i < N; i++) adj[i] = new ArrayList<>();

for (int i = 0; i < N - 1; i++) {
String[] input = br.readLine().split(" ");
int u = Integer.parseInt(input[0]) - 1;
int v = Integer.parseInt(input[1]) - 1;
adj[u].add(v);
adj[v].add(u);
}

for (int i = 0; i < N; i++) Arrays.fill(parent[i], -1);
Arrays.fill(depth, -1);
depth[0] = 0;

makeTree(0);

for (int j = 0; j < MAX - 1; j++) {
for (int i = 1; i < N; i++) {
if (parent[i][j] != -1)
parent[i][j + 1] = parent[parent[i][j]][j];
}
}

M = Integer.parseInt(br.readLine());
for (int i = 0; i < M; i++) {
String[] nodePairs = br.readLine().split(" ");
int u = Integer.parseInt(nodePairs[0]) - 1;
int v = Integer.parseInt(nodePairs[1]) - 1;

if (depth[u] < depth[v]) {
int tmp = u;
u = v;
v = tmp;
}
int depthDiff = depth[u] - depth[v];

for (int j = 0; depthDiff > 0; j++) {
if (depthDiff % 2 == 1) u = parent[u][j];
depthDiff /= 2;
}

if (u != v) {
for (int j = MAX - 1; j >= 0; j--) {
if(parent[u][j] != -1 && parent[u][j] != parent[v][j]) {
u = parent[u][j];
v = parent[v][j];
}
}
u = parent[u][0];
}

System.out.println(u + 1);
}

br.close();
}
}