From 0d3af7f7103f8c4f86d90d0cdbc31153725fe2e3 Mon Sep 17 00:00:00 2001 From: seongwon030 <105052068+seongwon030@users.noreply.github.com> Date: Fri, 5 Jul 2024 15:44:04 +0900 Subject: [PATCH] =?UTF-8?q?2024-07-05=20=EC=B5=9C=EC=86=8C=EC=8A=A4?= =?UTF-8?q?=ED=8C=A8=EB=8B=9D=ED=8A=B8=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...50\353\213\235\355\212\270\353\246\254.py" | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 "seongwon030/\354\265\234\354\206\214\354\212\244\355\214\250\353\213\235\355\212\270\353\246\254/\354\265\234\354\206\214\354\212\244\355\214\250\353\213\235\355\212\270\353\246\254.py" diff --git "a/seongwon030/\354\265\234\354\206\214\354\212\244\355\214\250\353\213\235\355\212\270\353\246\254/\354\265\234\354\206\214\354\212\244\355\214\250\353\213\235\355\212\270\353\246\254.py" "b/seongwon030/\354\265\234\354\206\214\354\212\244\355\214\250\353\213\235\355\212\270\353\246\254/\354\265\234\354\206\214\354\212\244\355\214\250\353\213\235\355\212\270\353\246\254.py" new file mode 100644 index 0000000..1f321b1 --- /dev/null +++ "b/seongwon030/\354\265\234\354\206\214\354\212\244\355\214\250\353\213\235\355\212\270\353\246\254/\354\265\234\354\206\214\354\212\244\355\214\250\353\213\235\355\212\270\353\246\254.py" @@ -0,0 +1,30 @@ +import sys + +input = sys.stdin.readline + +V,E = map(int,input().split()) +root = [i for i in range(V+1)] +edge = [] # 간선리스트 +for i in range(E): + edge.append(list(map(int,input().split()))) + +# 비용을 기준으로 오름차순 +edge.sort(key=lambda x:x[2]) + +def find(x): + if x!=root[x]: + root[x] = find(root[x]) + return root[x] + +ans = 0 +for a,b,c in edge: + aRoot = find(a) + bRoot = find(b) + if aRoot != bRoot: + if aRoot > bRoot: + root[aRoot] = bRoot + else: + root[bRoot] = aRoot + ans += c + +print(ans) \ No newline at end of file