-
Notifications
You must be signed in to change notification settings - Fork 0
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
Conversation
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.
spare table๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ์์ผ๋ก๋ ํ๋ค ๊ฒ ๊ฐ์์, ์ ๋ฐฉ์๋๋ก ํ ๋ฒ ํ์ด๋ณด๊ณ ๊ณต์ ํฉ๋๋ค.
ย ์ฒ์์๋ ์ ์๋๋ก ๋
ธ๋๋ฅผ ๊ตฌํํ๊ณ insertNode๋ฅผ ํ๋ ๋ฐฉ์์ด ๋จผ์ ๋ ์ฌ๋์ต๋๋ค.
๊ทธ๋ฐ๋ฐ 'LCA๋ฅผ ๊ตฌํ๋๋ฐ ๊ทธ๋ ๊ฒ ๋ง์ ๋
ธ๋ ฅ์ด ํ์ํ๊ฐ?' ๋ผ๋ ์๊ฐ๋ ๊ฑฐ์ ๋์์ ๋ค์์ต๋๋ค.
ย ๊ทธ๋์ ์ ๋ map์ parent-child์ ๊ด๊ณ๋ฅผ ๊ธฐ๋กํ๋ ค๊ณ ํ์ต๋๋ค.
์ด ๋ฐฉ๋ฒ ์ญ์ LCA๋ฅผ ๊ตฌํ๊ธฐ์๋ ์ ์ ํ์ง ์์ ๋ฐฉ์์ด๋ผ๋ ์๊ฐ์ด ๋ค์์ต๋๋ค.
(ํ 1์๊ฐ ๊ณ ๋ฏผํ ๊ฒ ๊ฐ๋ค์...)
ย map๋ฐฉ์์ผ๋ก tree๋ฅผ ๊ตฌํํด๋ดค์ ๋์ ๋ฌธ์ ๋
๋ถ๋ชจ ๋
ธ๋์ ๋ํ ์ ๋ณด๊ฐ ์ฌ์ฉํ๊ธฐ ์ฝ๊ฒ ์ ์ฅ๋์ง ์๋๋ค๋ ๊ฒ์
๋๋ค.
๊ทธ๋์ ๋ถ๋ชจ ๋
ธ๋ ์ ๋ณด๋ฅผ ๋ด์ ๋ฐฐ์ด์ ์ฌ์ฉํด์ LCA๋ฅผ ๊ตฌํ๊ธฐ๋ก ํ์ต๋๋ค.
ย ๋ถ๋ชจ ๋ ธ๋์ ๋ณด๋ฅผ ์๋์ ๊ฐ์ด ์ ์ฅํ๊ณ
vector<int> parent(N + 1, 0);
for (int i = 0; i < N - 1; i++) {
cin >> A >> B;
parent[B] = A;
}
ย LCA๋ฅผ ๊ตฌํ๊ณ ์ ํ๋ ๋ ๋ ธ๋๋ฅผ x, y๋ผ๊ณ ํ์ ๋ ๊ณผ์ ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
- x๋ ธ๋๋ฅผ root๊น์ง ์ด๋ ์ํค๋ฉฐ, ๋ฐฉ๋ฌธ ํ์
- y๋ ธ๋๋ฅผ visit ํ์๊ฐ ๋ ๊ณณ๊น์ง ์ด๋์ํค๊ธฐ
ย ์ด๋ ต์ง ์๊ฒ ์ดํดํ ์ ์์ ๊ฒ ์ ๋๋ค.
int getLCA(vector<int>& parent, int x, int y, int N) {
vector<bool> visit(N + 1, false);
while (x != 0) {
visit[x] = true;
x = parent[x];
}
while (visit[y] != true) {
y = parent[y];
}
return y;
}
g0rnn/ํธ๋ฆฌ/2-g0rnn.java
Outdated
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.
ย LCA๋ฌธ์ ๋ฅผ ๋ณด๋ ๋ฐ๊ฐ์ ์ต๋๋ค.
์ฒ์์๋ ์ ๊ฐ ์๋ ์๋๋๋ก ์ง์ ๋
ธ๋๋ฅผ ๊ตฌํํ๊ณ , ํฌ์ธํฐ๋ก ๋งํฌ๋ฅผ ๊ตฌํํ๋ ๋ฐฉ์์ผ๋ก ์ค๋ช
ํด์ฃผ์
์ ์ดํด๊ฐ ๋นจ๋์ต๋๋ค.
์ ๋ ์ด ์๊ณ ๋ฆฌ์ฆ์ ๋จ์ ์ ๋ค์๊ณผ ๊ฐ๋ค๊ณ ์๊ฐํ์ต๋๋ค.
- ๋ ธ๋๋ฅผ ๊ตฌํํ๋ ๊ฒ์๋ํ ์๊ฐ ๋ฌธ์
- ํฌ์ธํฐ๋ฅผ ๋ง์ด ์ฌ์ฉํ๊ธฐ์ ์ค์๊ฐ ์ฆ์ ์ ์๋ ๋ฑ์ ๋์ด๋ ๋ฌธ์
ย ์๊ฐ๋ณต์ก๋๊ฐ ๋จ์ ์ด๋ผ๊ณ ๋ ์๊ฐํ์ง ๋ชปํ์์ต๋๋ค. ๋๋ถ์ ์๋ก์ด ๋ฐฉ์์ ์๊ฒ๋์๋ค์.
ํฌ์ ํ
์ด๋ธ ์ฒ์ ๋ค์ด๋ดค์ต๋๋ค. ์ฌ์ค ํฌ์(spare)๋ผ๋ ๋ง์ ์์ ์ ๋ค์ด๋ดค๋๋ฐ ๋ป์ ์ ํํ ๋ชฐ๋์ต๋๋ค.
์ด๋ฒ์ ๊ท ํธ๋๊ป์ ํฌ์ ํ
์ด๋ธ์ ์๊ฐํด์ฃผ์๊ณ ๊ด๋ จ๋ ๋ธ๋ก๊ทธ๋ฅผ ์ถ์ฒํด์ฃผ์
์ ์ ๋ฐฐ์๊ฐ ์ ์๊ฒ ๋ค์. ๊ฐ์ฌํฉ๋๋ค.
ย ํฌ์ ํ
์ด๋ธ์ ์ฌ์ฉํ๋ ๊ฒ์ ๋ํ ์ค๋ช
๋ ์ข์์ต๋๋ค.
parent[i][j + 1] = parent[parent[i][j]][j]; ๋ถ๋ถ๋ ์ฒจ๋ถ๋ ์ด๋ฏธ์ง๋ฅผ ๋ณด๊ณ ํ ๋ฒ์ ์ดํดํ ์ ์์์ต๋๋ค.
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.
๊ฝค ๊ธด์๊ฐ์ ๊ฑธ์ณ ๋ธ๋ฃจํธํฌ์ค๋ก ํ์ด๋ดค์ต๋๋ค.
๊ทธ๋ฌ๋ ๋ฐฑ์ค์ฌ์ดํธ์์ ์๊ฐ์ด๊ณผ๋ก ์คํจ ์ฒ๋ฆฌ๋์์ต๋๋ค...
class Node:
def __init__(self, value):
self.value = value
self.children = []
def add_child(self, child_node):
self.children.append(child_node)
N = int(input())
edges = [tuple(map(int, input().split())) for _ in range(N - 1)]
nodes = {}
# ํธ๋ฆฌ ์์ฑ (๋ถ๋ชจ-์์ ๊ด๊ณ ์ค์ )
for parent, child in edges:
if parent not in nodes:
nodes[parent] = Node(parent)
if child not in nodes:
nodes[child] = Node(child)
# ๋ถ๋ชจ-์์ ๊ด๊ณ ์ค์
nodes[parent].add_child(nodes[child])
nodes[child].add_child(nodes[parent])
# ๋ถ๋ชจ์ ๊น์ด ์ ๋ณด๋ฅผ ์ ์ฅํ ํ LCA๋ฅผ ์ฐพ๋ ๋ฐฉ์
def find_parent_bruteforce(nodes, child1, child2):
parent_map = {}
# ํธ๋ฆฌ์ ๋ฃจํธ์์๋ถํฐ DFS๋ก ๋ถ๋ชจ ์ ๋ณด๋ฅผ ์ ์ฅ
def build_parent_map(node, parent=None):
if parent is not None:
parent_map[node.value] = parent
for child in node.children:
if child.value != parent: # ์ด๋ฏธ ๋ฐฉ๋ฌธํ ๋ถ๋ชจ๋ ๋ค์ ๋ฐฉ๋ฌธํ์ง ์์
build_parent_map(child, node.value)
# ๋ถ๋ชจ ์ ๋ณด์ ๊น์ด๋ฅผ ๊ธฐ๋ก (๋ฃจํธ๋ 1์ด๋ผ๊ณ ๊ฐ์ )
root = nodes[1]
build_parent_map(root)
# ๊ฐ ๋
ธ๋์ ๋ถ๋ชจ๋ฅผ ๊ฑฐ์ฌ๋ฌ ์ฌ๋ผ๊ฐ์ ๊ณตํต ์กฐ์์ ์ฐพ์
def find_ancestors(node_value):
ancestors = []
while node_value is not None:
ancestors.append(node_value)
node_value = parent_map.get(node_value, None)
return ancestors
# ๋ ์์์ ๊ณตํต ์กฐ์์ ์ฐพ๋ ๊ณผ์
ancestors1 = find_ancestors(child1)
ancestors2 = find_ancestors(child2)
# ๊ฐ์ฅ ๊ฐ๊น์ด ๊ณตํต ์กฐ์ ์ฐพ๊ธฐ
for ancestor in ancestors1:
if ancestor in ancestors2:
return ancestor
# ์ฟผ๋ฆฌ ์
๋ ฅ
M = int(input())
# ๊ฐ ์์ ๋ํด ๊ณตํต ์กฐ์์ ์ถ๋ ฅ
for _ in range(M):
node_a, node_b = map(int, input().split())
print(find_parent_bruteforce(nodes, node_a, node_b))
์ํ ๋๋๊ณ LCA์๊ณ ๋ฆฌ์ฆ์ผ๋ก ๋ค์ ํ์ด๋ณด๊ฒ ์ต๋๋ค ใ
,,
์๊ณ ๋ณด๋ ํ๋ ํฐ๋๋ฌธ์ ์๊ตฐ์๐ฅฒ
์๊ณ ๋ฆฌ์ฆ ์์
์๊ฐ ๋ชปํ์ด๋ ํผ์ ๊ณต๋ถํด๋๊ฑธ ํ๊ณ ํํ๊ฐ ๋ง์ด ๋๋ค์.. |
๐ ๋ฌธ์ ๋งํฌ
[ํธ๋ฆฌ] LCA2 : https://www.acmicpc.net/problem/11438
โ๏ธ ์์๋ ์๊ฐ
3h ์ด์..
์๋๋ LCA๋ฅผ ํ๋ ค๊ณ ํ์ผ๋ ์ฝ๊ฐ ๋งํ๋ ๋ถ๋ถ์์ ์กฐ๊ธ ์์นญํด๋ณด๋ค๊ฐ ์๊ฐ๋ณต์ก๋๊ฐ O(log n)์ธ ์๊ณ ๋ฆฌ์ฆ์ด ์๋ค๊ณ ํด์... ๊ฑฐ๊ธฐ์ ํนํด๋ฒ๋ฆฌ๋ ๋ฐ๋์ ์๊ฐ์ด ๋ฌด์ง์ฅ ์ค๋๊ฑธ๋ ธ์ต๋๋ค..
๊ทผ๋ฐ ์๊ณ ๋ณด๋ ๊ทธ ๋ฌธ์ ๋ ๐ํ๋ 4๐ ์๊ณ , ๊ทธ๊ฒ๋ ๋ชจ๋ฅด๋ ์ ๋ ๊ทธ๋ฅ ์ฃผ๊ตฌ์ฅ์ฐฝ ์๊ณ ๋ฆฌ์ฆ์ ์๊ฐํ๊ณ ์ฐ๊ตฌํ๋๊ฑฐ์ฃ ...
์๋ cpp๋ก ํ๋ ค๋ค๊ฐ ์ด๋ฒ์ java๋ก ๋ฐ๊ฟ๋ณด๋ ค๊ณ ํ๋๋ฐ ์๊ฐ๋ณด๋ค ๋ง์ ๊ฒ๋ค์ด ๋ฌ๋ผ์ ๊ฑฐ๊ธฐ์๋ ์๊ฐ์ ์ข ๋นผ์๊ฒผ์ต๋๋ค....ํ....
๋ค์๋ถํฐ๋ ์๊ฐ์ ์ข ๋ ์ฐ๋๋ก ๋ฌธ์ ์ ๋์ด๋๋ฅผ ์กฐ์ ํด๋ณด๊ฒ ์ต๋๋ค.
โจ ์๋ ์ฝ๋
โฒ๏ธ ์๊ฐ๋ณต์ก๋ O(n)
๋จผ์ ๊ธฐ์กด LCA์ ๋ํ ์๋ ์ฝ๋๋ฅผ ์ ์ด๋ณด๊ฒ ์ต๋๋ค.
ํธ๋ฆฌ๋ฅผ ๊ตฌ์ฑํ๋ค๊ณ ์น๊ณ LCA๋ง์ ๊ตฌํ๋ ์ฝ๋์ ๋๋ค. ์กฐ๊ธ๋ ์ค๋ช ์ ๋ง๋ถ์ด๊ฒ ์ต๋๋ค.
์์ ๋ฐฉ๋ฒ์ผ๋ก ์ฝ๋๋ฅผ ์ง๋ฉด O(n)์ด๋ผ๋ ์๊ฐ๋ณต์ก๋๊ฐ ๋์ต๋๋ค. ํธ๋ฆฌ์ ๊ตฌ์กฐ๋ฅผ ์๊ฐํ์ ๋์ ๋์ด๋ง์ ๋ฐ์ง๋ฉด ๋๋๊น O(log n)์ด๋ผ๊ณ ์๊ฐํ๊ธฐ ์ฝ์ง๋ง, ์ต์ ์ ๊ฒฝ์ฐ ์ผ์ง์ ํํ์ ํธ๋ฆฌ๊ตฌ์กฐ์์๋ ๋์ด๊ฐ ๊ณง ๋ ธ๋์ ๊ฐ์์ด๊ธฐ ๋๋ฌธ์ O(n)์ด๋ผ๋ ์๊ฐ๋ณต์ก๋๋ฅผ ๊ฐ์ง๊ฒ ๋ฉ๋๋ค.
๊ทธ๋ผ ๋์ฒด ์ด๋ป๊ฒ ํ์ด์ผ ํ ๊น์? ์ ๋ต์ "Sparse-Table์ ์ฌ์ฉํ๋ค."์ ๋๋ค. ์ด๋ N๋ฒ์งธ ์์ ์๋ ์์๋ฅผ ์ฐพ๊ธฐ ์ํด ์ฌ์ฉ๋๋ ํ ์ด๋ธ์ ๋๋ค. ์ด ํ ์ด๋ธ์ ๊ตฌ์ฑํ์๋ค๋ฉด ๊ธฐ์กด LCA ์๋์ฝ๋๋ฅผ ํ์ฉํ์ฌ Lowest Common Ancestor๋ฅผ ์ฐพ์ผ๋ฉด ๋ฉ๋๋ค. ๊ธฐ์กด์๋ ์์ ์ ์ง๊ณ๋ถ๋ชจ๋ฅผ ์ฐพ์๊ฐ๋ฉด์ ๋์ด(depth)๋ฅผ ๋ง์ถ๊ณ lca๋ฅผ ์ฐพ์๋๋ฐ ์ด์ ๋ sparse table์ ์ฌ์ฉํ์ฌ ๋ ๋น ๋ฅด๊ฒ ์ฐพ์ ์ ์๊ฒ ๋ฉ๋๋ค.
์ด์ ๋ณธ๊ฒฉ์ ์ธ ์๋ ์ฝ๋ ์ค๋ช ์ ๋ค์ด๊ฐ๊ฒ ์ต๋๋ค.
โฒ๏ธ ์๊ฐ๋ณต์ก๋ O(log n)
0-1. ๊ฐ ๋ ธ๋์ ๋ถ๋ชจ ๋ ธ๋๋ฅผ ์ ์ฅํ 2์ฐจ์ ๋ฐฐ์ด parent๋ฅผ -1๋ก ์ฑ์ด๋ค.
0-2. ๊ฐ ๋ ธ๋์ ๊น์ด๋ฅผ ์ ์ฅํ๋ depth ๋ฐฐ์ด๋ -1๋ก ์ฑ์ด๋ค. - ์ฐธ๊ณ ๋ก root๋ ธ๋์ ๋์ด๋ 0์ด๋ค.
1-1. adj ๋ฐฐ์ด์ ์ํํ๋ฉด์ ์์ ๋ ธ๋๋ฅผ ์ฐจ๋ก๋ก ๋ฐฉ๋ฌธํ๋ฉฐ depth๋ฅผ ๊ธฐ๋กํ๋ค. (์ด๋ฏธ ๋ฐฉ๋ฌธํ ๋ ธ๋๋ ์ ์ธ - depth๊ฐ -1์ด ์๋)
1-2. parent๋ฐฐ์ด์ ์ํ ์ค์ธ ๋ ธ๋์ ์ง๊ณ๋ถ๋ชจ๋ฅผ ์ ์ฅํ๋ค.
1-3. ์ด ํจ์๋ฅผ ๋ฐ๋ณตํ์ฌ parent์ depth๋ฅผ ์ฑ์ด๋ค.
3-1. ๋๊ฐ ๋ ๋์ ์์น์ ์๋์ง ๋ชจ๋ฅด๋๊น v๋ฅผ ๋ ๋์ ๊ฒ์ผ๋ก ์ค์
3-2. ๋์ด ์ฐจ์ด๋ฅผ 0์ผ๋ก ๋ง๋ฆ. depthDiff๊ฐ 13์ผ ๋, u๋ฅผ 1 ๋งํผ ์๋ก (diff๋ฅผ 2๋ก ๋๋๊ณ ) -> 4 ๋งํผ ์๋ก (diff๋ฅผ 2๋ก ๋๋๊ณ ) -> 8 ๋งํผ ์๋ก ์ฌ๋ฆฌ๋ฉด 13๋งํผ ์ฌ๋ผ๊ฐ์ ๋์ด๊ฐ ๊ฐ์์ง
์ฝ๋๋ฅผ ์ฝ๋ค๋ณด๋ฉด
parent[i][j + 1] = parent[parent[i][j]][j];
์ด ๋ถ๋ถ์์ ํท๊ฐ๋ฆด ์ ์๋๋ฐ ์ด๋ ์ด๋ฏธ์ง๋ฅผ ํตํด์ ์ค๋ช ํ๊ฒ ์ต๋๋ค.14๋ณด๋ค 4(2^2) ๋จ๊ณ ์์ ์๋ ๋ ธ๋๋ฅผ ์ฐพ๊ธฐ ์ํด์ 14์ 2(2^1)๋จ๊ณ ์์ ์๋ ๋ ธ๋(p1)๋ฅผ ์ฐพ๊ณ p1์ 2(2^1)๋จ๊ณ ์์ ๋ ธ๋๋ฅผ ์ฐพ์ผ๋ฉด ๋๊ธฐ ๋๋ฌธ์ ๋๋ค.
๋ง์ฝ 16๋จ๊ณ ์์ ์๋ ๋ ธ๋๋ฅผ ์ฐพ๊ณ ์ถ๋ค๋ฉด 8๋จ๊ณ ์์ ๋ ธ๋๋ฅผ ์ฐพ๊ณ ๊ทธ ๋ ธ๋์์ 8๋จ๊ณ ์ฌ๋ผ๊ฐ๋ฉด ๋ฉ๋๋ค.
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ
ํฌ์ ํ ์ด๋ธ์ ๋ํด ์๊ฒ ๋์์ต๋๋ค. ์๊ฐ๋ณด๋ค ์ข ์ด๋ ค์ด ๋ด์ฉ์ด๋ผ ๊ณจ์น ์ํ ์ง๋ง ์ฌ๋ฐ์์ต๋๋ค.
์๋ฐ๋ก... ์๊ณ ๋ฆฌ์ฆ ๋ฌธ์ ๋ฅผ ํ๋ฉด ๋ญ๊ฐ ๋ถํธํ ๋๋์ด ๋ญ๋๋ค. ๊ทธ๋๋ ์๋ฐ์ ์กฐ๊ธ ๋ ์ต์ํด์ง ๋๊น์ง ์๋ฐ๋ก ์ฝ๋๋ฅผ ์์ฑํ๊ฒ ์ต๋๋ค. (์ฐจํผ ์ฌ๋ฌ๋ถ๋ ํ์ ์ผํด์ ^^)