-
Notifications
You must be signed in to change notification settings - Fork 12
백준11437번LCA #77
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
Open
j2woo
wants to merge
1
commit into
Coding-Test-Study-Group:main
Choose a base branch
from
j2woo:백준11437번LCA
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
The head ref may contain hidden characters: "\uBC31\uC90011437\uBC88LCA"
Open
백준11437번LCA #77
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"type": "java", | ||
"name": "Launch Current File", | ||
"request": "launch", | ||
"mainClass": "${file}" | ||
}, | ||
{ | ||
"type": "java", | ||
"name": "Launch Main", | ||
"request": "launch", | ||
"mainClass": "Main", | ||
"projectName": "Coding-Test-Study_af8b389e" | ||
}, | ||
{ | ||
"type": "java", | ||
"name": "Launch SWEA_1206_View", | ||
"request": "launch", | ||
"mainClass": "jiWoo.SWEA_1206_View", | ||
"projectName": "Coding-Test-Study_af8b389e" | ||
}, | ||
{ | ||
"type": "java", | ||
"name": "Launch 백준_10868번_최솟값", | ||
"request": "launch", | ||
"mainClass": "jiWoo.백준_10868번_최솟값", | ||
"projectName": "Coding-Test-Study_af8b389e" | ||
}, | ||
{ | ||
"type": "java", | ||
"name": "Launch 백준_11051_이항계수2", | ||
"request": "launch", | ||
"mainClass": "jiWoo.백준_11051_이항계수2", | ||
"projectName": "Coding-Test-Study_af8b389e" | ||
}, | ||
{ | ||
"type": "java", | ||
"name": "Launch 백준_1141번_접두사", | ||
"request": "launch", | ||
"mainClass": "jiWoo.백준_1141번_접두사", | ||
"projectName": "Coding-Test-Study_af8b389e" | ||
}, | ||
{ | ||
"type": "java", | ||
"name": "Launch 백준_11437번_LCA", | ||
"request": "launch", | ||
"mainClass": "jiWoo.백준_11437번_LCA", | ||
"projectName": "Coding-Test-Study_af8b389e" | ||
}, | ||
{ | ||
"type": "java", | ||
"name": "Launch 백준_11505번_구간곱구하기", | ||
"request": "launch", | ||
"mainClass": "jiWoo.백준_11505번_구간곱구하기", | ||
"projectName": "Coding-Test-Study_af8b389e" | ||
}, | ||
{ | ||
"type": "java", | ||
"name": "Launch 백준_14226번_이모티콘", | ||
"request": "launch", | ||
"mainClass": "jiWoo.백준_14226번_이모티콘", | ||
"projectName": "Coding-Test-Study_af8b389e" | ||
}, | ||
{ | ||
"type": "java", | ||
"name": "Launch Baekjoon_11286_절댓값힙", | ||
"request": "launch", | ||
"mainClass": "sollyj.Baekjoon_11286_절댓값힙", | ||
"projectName": "Coding-Test-Study_af8b389e" | ||
}, | ||
{ | ||
"type": "java", | ||
"name": "Launch Baekjoon_11899_괄호", | ||
"request": "launch", | ||
"mainClass": "sollyj.Baekjoon_11899_괄호", | ||
"projectName": "Coding-Test-Study_af8b389e" | ||
}, | ||
{ | ||
"type": "java", | ||
"name": "Launch Baekjoon_1253_좋은수", | ||
"request": "launch", | ||
"mainClass": "sollyj.Baekjoon_1253_좋은수", | ||
"projectName": "Coding-Test-Study_af8b389e" | ||
}, | ||
{ | ||
"type": "java", | ||
"name": "Launch Baekjoon_17298_오큰수", | ||
"request": "launch", | ||
"mainClass": "sollyj.Baekjoon_17298_오큰수", | ||
"projectName": "Coding-Test-Study_af8b389e" | ||
}, | ||
{ | ||
"type": "java", | ||
"name": "Launch Baekjoon_1874_스택수열", | ||
"request": "launch", | ||
"mainClass": "sollyj.Baekjoon_1874_스택수열", | ||
"projectName": "Coding-Test-Study_af8b389e" | ||
}, | ||
{ | ||
"type": "java", | ||
"name": "Launch Baekjoon_1940_주몽", | ||
"request": "launch", | ||
"mainClass": "sollyj.Baekjoon_1940_주몽", | ||
"projectName": "Coding-Test-Study_af8b389e" | ||
}, | ||
{ | ||
"type": "java", | ||
"name": "Launch Baekjoon_2018_수들의합5", | ||
"request": "launch", | ||
"mainClass": "sollyj.Baekjoon_2018_수들의합5", | ||
"projectName": "Coding-Test-Study_af8b389e" | ||
}, | ||
{ | ||
"type": "java", | ||
"name": "Launch baekjoon_1448_삼각형만들기", | ||
"request": "launch", | ||
"mainClass": "sollyj.baekjoon_1448_삼각형만들기", | ||
"projectName": "Coding-Test-Study_af8b389e" | ||
}, | ||
{ | ||
"type": "java", | ||
"name": "Launch fingers", | ||
"request": "launch", | ||
"mainClass": "sollyj.fingers", | ||
"projectName": "Coding-Test-Study_af8b389e" | ||
}, | ||
{ | ||
"type": "java", | ||
"name": "Launch prefixSum", | ||
"request": "launch", | ||
"mainClass": "sollyj.prefixSum", | ||
"projectName": "Coding-Test-Study_af8b389e" | ||
}, | ||
{ | ||
"type": "java", | ||
"name": "Launch prefixSum_2", | ||
"request": "launch", | ||
"mainClass": "sollyj.prefixSum_2", | ||
"projectName": "Coding-Test-Study_af8b389e" | ||
}, | ||
{ | ||
"type": "java", | ||
"name": "Launch treasure", | ||
"request": "launch", | ||
"mainClass": "sollyj.treasure", | ||
"projectName": "Coding-Test-Study_af8b389e" | ||
}, | ||
{ | ||
"type": "java", | ||
"name": "Launch Main(1)", | ||
"request": "launch", | ||
"mainClass": "문자열_밀기.Main", | ||
"projectName": "Coding-Test-Study_af8b389e" | ||
} | ||
] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package jiWoo; | ||
|
||
import java.util.*; | ||
|
||
public class 백준_11437번_LCA { | ||
static ArrayList<Integer>[] tree; | ||
static int[] depth; | ||
static int[] parent; | ||
static boolean[] visited; | ||
|
||
public static void main(String[] args) { | ||
Scanner sc = new Scanner(System.in); | ||
int N = sc.nextInt(); // 정점의 수 | ||
tree = new ArrayList[N + 1]; | ||
for (int i = 1; i <= N; i++) { | ||
tree[i] = new ArrayList<Integer>(); | ||
} | ||
for (int i = 0; i < N - 1; i++) { // A인접리스트에 그래프 데이터 저장 | ||
int s = sc.nextInt(); | ||
int e = sc.nextInt(); | ||
tree[s].add(e); | ||
tree[e].add(s); | ||
} | ||
depth = new int[N + 1]; | ||
parent = new int[N + 1]; | ||
visited = new boolean[N + 1]; | ||
BFS(1); // depth를 BFS를 통하여 구하기 | ||
int M = sc.nextInt(); // 질의의 수 | ||
for (int i = 0; i < M; i++) { | ||
// 공통 조상을 구할 두 노드 | ||
int a = sc.nextInt(); | ||
int b = sc.nextInt(); | ||
int LCA = excuteLCA(a, b); | ||
System.out.println(LCA); | ||
} | ||
} | ||
|
||
static int excuteLCA(int a, int b) { | ||
if (depth[a] < depth[b]) { | ||
int temp = a; | ||
a = b; | ||
b = temp; | ||
} | ||
while (depth[a] != depth[b]) { // 두 노드의 Depth 맞춰주기 | ||
a = parent[a]; | ||
} | ||
while (a != b) { // 같은 조상이 나올 때까지 한칸씩 올려주기 | ||
a = parent[a]; | ||
b = parent[b]; | ||
} | ||
return a; | ||
} | ||
|
||
// BFS구현 | ||
private static void BFS(int node) { | ||
Queue<Integer> queue = new LinkedList<Integer>(); | ||
queue.add(node); | ||
visited[node] = true; | ||
int level = 1; | ||
int now_size = 1; | ||
int count = 0; | ||
while (!queue.isEmpty()) { | ||
int now_node = queue.poll(); | ||
for (int next : tree[now_node]) { | ||
if (!visited[next]) { | ||
visited[next] = true; | ||
queue.add(next); | ||
parent[next] = now_node; // 부모 노드 저장 | ||
depth[next] = level; // 노드 depth 저장 | ||
} | ||
} | ||
count++; | ||
if (count == now_size) { | ||
count = 0; | ||
now_size = queue.size(); | ||
level++; | ||
} | ||
} | ||
} | ||
} | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@j2woo
저도 방금전에 이 문제를 풀어보았는데요!
모든 노드의 depth를 구하기 위해 완전 탐색을 진행해야 하는 상황입니다.
너비 우선 탐색을 통해 depth(Level)를 구할 수도 있겠지만,
Level에 따라 깊게 들어가는 깊이 우선 탐색을 사용하는 것이 목적에 맞아 값을 구하기 수월할 것으로 생각이 됩니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
한 수 배워갑니다:)