-
Notifications
You must be signed in to change notification settings - Fork 1
19-dohyeondol1 #76
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
19-dohyeondol1 #76
Conversation
|
์ต์ ์คํจ๋ ํธ๋ฆฌ์, ์ ๋ฒ์ ์๊ฒ ๋ ์๋ก์ ์งํฉ์ ์๊ณ ์์ด์ ํฌ๋ฃจ์ค์นผ ์๊ณ ๋ฆฌ์ฆ์ ํตํด ๋น ๋ฅด๊ฒ ํด๊ฒฐํด๋ณผ ์ ์์์ต๋๋ค. import sys
input = sys.stdin.readline
def find(x):
while parent[x] != x:
parent[x] = parent[parent[x]]
x = parent[x]
return x
def union(a, b):
ra = find(a)
rb = find(b)
if ra != rb:
parent[rb] = ra
v, e = map(int, input().split())
parent = [i for i in range(v+1)]
edge = []
for _ in range(e):
a, b, c = map(int, input().split())
edge.append((c, a, b))
edge.sort()
ans = 0
for w, u, v in edge:
if find(u) != find(v):
union(u, v)
ans += w
print(ans) |
|
์นผ๋ฆฌ๋ทฐ ใทใท |
|
์๋ฃ๊ตฌ์กฐ ์๊ฐ์ ๋ณด์๋ MST๋ฅผ ์ฌ๊ธฐ์ ๋ณด๋ ๋ฐ๊ฐ๋ค์. ํฌ๋ฃจ์ค์นผ ์๊ณ ๋ฆฌ์ฆ์ ๊ฐ์ ์ ์ ๋ ฌํ ๋ค์ ์ต์ ๋น์ฉ ๊ฐ์ ๋ถํฐ ์ฌ์ดํด์ด ๋ฐ์ํ๋์ง ์ฒดํฌํ๋ ๋ก์ง์ด๊ธฐ ๋๋ฌธ์, ๊ฐ์ ์ ์ ๋ ฌํ๋ ๋ฐ ์๊ฐ์ ๊ฐ์ฅ ๋ง์ด ์๋ชจํ๋ ๊ฒ์ผ๋ก ์๊ณ ์์ต๋๋ค. ( ๋ฐ๋ฉด ํ๋ฆผ ์๊ณ ๋ฆฌ์ฆ์ ์ธ์ ๋ฐฐ์ด๋ก Greedyํ๊ฒ ์ ๊ทผํ๋ ๋ฐฉ๋ฒ๊ณผ ๋ํ๋์ด ๋ง์ํ์ ์ฐ์ ์์ ํ๋ก ์ ๊ทผํ๋ ๋ฐฉ๋ฒ์ด ์๋๋ฐ, ์ธ์ ๋ฐฐ์ด์ ์ฌ์ฉํ๋ฉด ๋ฐ๋ผ์ ๊ฐ์ ์ ์๊ฐ ์ ์ ๊ทธ๋ํ์ผ ๊ฒฝ์ฐ ํฌ๋ฃจ์ค์นผ ์๊ณ ๋ฆฌ์ฆ์ด ๋ ํจ์จ์ ์ด๊ณ , PS์์๋ ์ด์ฐจํผ ํ
์คํธ ์ผ์ด์ค๊ฐ ๋ค์ํ๊ฒ ๋์์ ๋ณ๋ก ์ค์ํ์ง ์์๋ฐ |
|
๊ทธ๋์ ๋ c++ stl์ priority_queue๋ ์๊ตฐ์.. ์๋ ๊ฒ์ด ์๋ค์ ใทใท |
C++์ ์ ๋ง๋ค์ด์ง STL์ด ๋ง์ ๊ฒ ๊ฐ์์ ใ ใ |
hadongun
left a comment
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.
์๋ฃ๊ตฌ์กฐ ๋ ๋ฐฐ์ ๋ ๊ฑฐ๋ผ ๊ทธ๋ฐ์ง ๊ธฐ์ต์ด ์์ํ์ง ์๋ค์ ใ
ใ
๊ทธ๋๋ ๊ธฐ์ต์ด ๋จ์ ์์ด ์กฐ๊ธ ๊ณต๋ถ ํ ํ ํ ์ ์์์ด์ฉ
ํ์ด์ฌ์ ์ฌ๊ท ํ์ ์ ํ์ด ์๋ค๋ ๊ฒ๋ ์ฒ์ ์์๋ค์..!
๊ฐ๋ฅํ ์ฌ๊ท ์ ํ์ ๋๋ ค์ ํด๊ฒฐํ์ต๋๋ค.
๋ฐ์ดํ ์คํธ๋ญ์ณ
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
V, E = map(int, input().split())
edges = []
for _ in range(E):
u, v, w = map(int, input().split())
edges.append((w, u, v))
edges.sort()
parent = [i for i in range(V+1)]
def find(x):
if parent[x] != x:
parent[x] = find(parent[x])
return parent[x]
def union(a, b):
a = find(a)
b = find(b)
if a != b:
parent[b] = a
return True
return False
result = 0
count = 0
for w, u, v in edges:
if union(u, v):
result += w
count += 1
if count == V-1:
break
print(result)
๐ ๋ฌธ์ ๋งํฌ
โ๏ธ ์์๋ ์๊ฐ
40m
โจ ์๋ ์ฝ๋
์๋์ฝ๋
์ด ๋ฌธ์ ๋ ๋ฌธ์ ์ด๋ฆ๋๋ก MST(Minimum Spanning Tree, ์ต์ ์คํจ๋ ํธ๋ฆฌ)๋ฅผ ๊ตฌํ๋ ๋ฌธ์ ์ ๋๋ค.
MST๋ฅผ ๊ตฌํ๋ ์๊ณ ๋ฆฌ์ฆ์๋ ๋ํ์ ์ผ๋ก ์ ์๋ ค์ง ๋๊ฐ์ง ๋ฐฉ๋ฒ์ด ์์ต๋๋ค.
ํ๋ฆผ ์๊ณ ๋ฆฌ์ฆ
ํ๋ฆผ ์๊ณ ๋ฆฌ์ฆ์ ์ ๊ฐ ๋ค์ต์คํธ๋ผ ์๊ณ ๋ฆฌ์ฆํ๊ณ ๊ฑฐ์ ๋๊ฐ๋ค๊ณ ๋๋ ์๊ณ ๋ฆฌ์ฆ์ ๋๋ค.
๊ธฐ๋ณธ์ ์ผ๋ก ๋ค์๊ณผ ๊ฐ์ ์์๋ฅผ ๋ฐ๋ฆ ๋๋ค.
์ด ์๊ณ ๋ฆฌ์ฆ์ ๋ค์ต์คํธ๋ผ์ ๋ง์ฐฌ๊ฐ์ง๋ก ์ฐ์ ์์ ํ๋ฅผ ์ฌ์ฉํด์ ๊ตฌํํ ์ ์์ต๋๋ค.
ํ๋ฆผ ์๊ณ ๋ฆฌ์ฆ ๊ตฌํ
ํฌ๋ฃจ์ค์นผ ์๊ณ ๋ฆฌ์ฆ
์ด์ฉ๋ฉด ํ๋ฆผ๋ณด๋ค ๋ ์ฌ์ด ์๊ณ ๋ฆฌ์ฆ์ ๋๋ค.
๋ค์๊ณผ ๊ฐ์ ์์๋ฅผ ๋ฐ๋ฆ ๋๋ค.
E์ ๋ง๋ ๋ค.x,y๋ฅผ ์ฐ๊ฒฐํ์ฌ๋ ์ฌ์ดํด์ด ๋ฐ์ํ์ง ์๋๋ค๋ฉด ์ฐ๊ฒฐํ๋ค.์ด๋ฅผ ์๋ก์ ์งํฉ, Union Find ๋ก ๊ตฌํํ ์ ์์ต๋๋ค.
ํฌ๋ฃจ์ค์นผ ์๊ณ ๋ฆฌ์ฆ ๊ตฌํ
์ ๋ ํ๋ฆผ ์๊ณ ๋ฆฌ์ฆ์ ์ฌ์ฉํด์ ์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ์์ต๋๋ค.
์ํ๋ ๊ทธ๋ํ ๋ฌธ์ ๋ฅผ ์ข ํ์ด๋ณด๋ ค๋ค ๋ณด๋ MST ๋ฌธ์ ๋ฅผ ๊ฐ์ ธ์ค๊ฒ ๋์์ต๋๋ค..
์ ์ฝ๋์์ ๋ ๊น๋ํ๊ฒ ๋ง๋ค ์ ์๊ฒ ๋ค ์ถ์ ๋ถ๋ถ์ด ์๋ค๋ฉด ๋ง๊ตฌ๋ง๊ตฌ ์๋ ค์ฃผ์ธ์ฉ~~ ๊ฐ์ฌํฉ๋๋ค
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ