Skip to content
Open
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
161 changes: 161 additions & 0 deletions .vscode/launch.json
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"
}
]
}
80 changes: 80 additions & 0 deletions jiWoo/백준_11437번_LCA.java
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++;
}
}
}
}
Comment on lines +54 to +80
Copy link
Contributor

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에 따라 깊게 들어가는 깊이 우선 탐색을 사용하는 것이 목적에 맞아 값을 구하기 수월할 것으로 생각이 됩니다.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

한 수 배워갑니다:)