[BOJ][๐ŸŸก4][๋ฐฑ์ค€#01197] ์ตœ์†Œ ์ŠคํŒจ๋‹ ํŠธ๋ฆฌ

์ž‘์„ฑ:    

์—…๋ฐ์ดํŠธ:

์นดํ…Œ๊ณ ๋ฆฌ:

ํƒœ๊ทธ: , , ,

๋ฌธ์ œ ์ถœ์ฒ˜

BAEKJOON Online Judge #1197


๋ฌธ์ œ

๊ทธ๋ž˜ํ”„๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๊ทธ ๊ทธ๋ž˜ํ”„์˜ ์ตœ์†Œ ์ŠคํŒจ๋‹ ํŠธ๋ฆฌ๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ์ตœ์†Œ ์ŠคํŒจ๋‹ ํŠธ๋ฆฌ๋Š”, ์ฃผ์–ด์ง„ ๊ทธ๋ž˜ํ”„์˜ ๋ชจ๋“  ์ •์ ๋“ค์„ ์—ฐ๊ฒฐํ•˜๋Š” ๋ถ€๋ถ„ ๊ทธ๋ž˜ํ”„ ์ค‘์—์„œ ๊ทธ ๊ฐ€์ค‘์น˜์˜ ํ•ฉ์ด ์ตœ์†Œ์ธ ํŠธ๋ฆฌ๋ฅผ ๋งํ•œ๋‹ค.


์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์ •์ ์˜ ๊ฐœ์ˆ˜ V(1 โ‰ค V โ‰ค 10,000)์™€ ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜ E(1 โ‰ค E โ‰ค 100,000)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ E๊ฐœ์˜ ์ค„์—๋Š” ๊ฐ ๊ฐ„์„ ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์„ธ ์ •์ˆ˜ A, B, C๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ด๋Š” A๋ฒˆ ์ •์ ๊ณผ B๋ฒˆ ์ •์ ์ด ๊ฐ€์ค‘์น˜ C์ธ ๊ฐ„์„ ์œผ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค. C๋Š” ์Œ์ˆ˜์ผ ์ˆ˜๋„ ์žˆ์œผ๋ฉฐ, ์ ˆ๋Œ“๊ฐ’์ด 1,000,000์„ ๋„˜์ง€ ์•Š๋Š”๋‹ค. ๊ทธ๋ž˜ํ”„์˜ ์ •์ ์€ 1๋ฒˆ๋ถ€ํ„ฐ V๋ฒˆ๊นŒ์ง€ ๋ฒˆํ˜ธ๊ฐ€ ๋งค๊ฒจ์ ธ ์žˆ๊ณ , ์ž„์˜์˜ ๋‘ ์ •์  ์‚ฌ์ด์— ๊ฒฝ๋กœ๊ฐ€ ์žˆ๋‹ค. ์ตœ์†Œ ์ŠคํŒจ๋‹ ํŠธ๋ฆฌ์˜ ๊ฐ€์ค‘์น˜๊ฐ€ย -2,147,483,648๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 2,147,483,647๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ๋ฐ์ดํ„ฐ๋งŒ ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„๋‹ค.


์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ์ตœ์†Œ ์ŠคํŒจ๋‹ ํŠธ๋ฆฌ์˜ ๊ฐ€์ค‘์น˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.


์˜ˆ์ œ

์ž…๋ ฅ

3 3
1 2 1
2 3 2
1 3 3


์ถœ๋ ฅ

3```

<br>

## My Sol

```python
import sys
input = sys.stdin.readline
from heapq import heappop, heappush


def find_set(x):
    global p
    if x != p[x]:
        p[x] = find_set(p[x])
    return p[x]


def union(u, v, w):
    global cnt, ret, p
    a, b = find_set(u), find_set(v)
    if a != b:
        p[a] = b
        ret += w
        cnt += 1


V, E = map(int, input().split())
Q = []
for _ in range(E):
    u, v, w = map(int, input().split())
    heappush(Q, (w, u, v))

p = [i for i in range(V+1)]
ret = 0
cnt = 0
while cnt < V-1:
    w, u, v = heappop(Q)
    union(u, v, w)

print(ret)

graph ์ด๋ก ์˜ ์œ ๋‹ˆ์˜จ ํŒŒ์ธ๋“œ์™€ ํฌ๋ฃจ์Šค์นผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ™œ์šฉํ•ด ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ๋‹ค.

  1. ์ž…๋ ฅ์„ ์ฒ˜๋ฆฌํ•˜์—ฌ ํ•œ ๊ณณ์— ๊ฐ„์„  ์ •๋ณด๋ฅผ ๋ชจ๋‘ ๋ชจ์€๋‹ค.
  2. ๊ฐ€์ค‘์น˜๊ฐ€ ์งง์€ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌํ•œ๋‹ค. ๋‚˜๋Š” ์šฐ์„ ์ˆœ์œ„ ํ(heap)๋ฅผ ์‚ฌ์šฉํ•˜์˜€๋‹ค.
  3. ํ•˜๋‚˜ํ•˜๋‚˜ ๋ฝ‘์•„์„œ ๋‘ ๋…ธ๋“œ์˜ ๋ฃจํŠธ๋…ธ๋“œ๊ฐ€ ๊ฐ™์€์ง€ ํ™•์ธํ•œ๋‹ค.
  4. ๊ฐ™๋‹ค๋ฉด ์ด๋ฏธ ๊ฐ€์ค‘์น˜๊ฐ€ ๋” ์ž‘์€ ๊ฐ„์„ ์œผ๋กœ ์—ฐ๊ฒฐ๋œ ๊ฒƒ์ด๋ฏ€๋กœ ๋„˜์–ด๊ฐ„๋‹ค.
  5. ๊ฐ™์ง€ ์•Š๋‹ค๋ฉด ํ•ด๋‹น ๊ฐ€์ค‘์น˜๊ฐ€ ๋‘ ๋…ธ๋“œ๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฐ€์žฅ ์ž‘์€ ๊ฐ€์ค‘์น˜์˜ ๊ฐ„์„ ์ด๋‹ค.
  6. ์œ ๋‹ˆ์˜จ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ๊ฐ€์ค‘์น˜๋ฅผ ๊ฒฐ๊ณผ๊ฐ’์— ๋”ํ•˜๊ณ  ์นด์šดํŠธ๋ฅผ 1 ์ฆ๊ฐ€, ๋‘ ๋…ธ๋“œ์˜ ๋ฃจํŠธ ๋…ธ๋“œ๋ฅผ ๊ฐ™๊ฒŒ ์ด์‹ํ•ด์ค€๋‹ค.
  7. ์นด์šดํŠธ๊ฐ€ V-1, ์ฆ‰ ๋ชจ๋“  ๋…ธ๋“œ๊ฐ€ ์—ฐ๊ฒฐ๋˜๋ฉด ๋ฐ˜๋ณต์„ ์ข…๋ฃŒํ•œ๋‹ค.
  8. ๊ฒฐ๊ณผ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.

ํ•ญ์ƒ union ํ•จ์ˆ˜์—์„œ p[a] = b ๋ถ€๋ถ„์ด ๋„ˆ๋ฌด ํ—ท๊ฐˆ๋ ธ๋‹ค. ์ด ๋ถ€๋ถ„์„ ์ž˜ ์ˆ™์ง€ํ•˜๋„๋ก ํ•˜์ž.


๊ฒฐ๊ณผ

๋งž์•˜์Šต๋‹ˆ๋‹ค!!


๋ชจ๋ฒ”๋‹ต์•ˆ

์ถœ์ฒ˜

from sys import stdin

read = stdin.readline
V, S = map(int, read().split())

edge = []
for _ in range(S):
    a, b, w = map(int, read().split())
    edge.append((w, a, b))

edge.sort(key=lambda x: x[0])

parent = list(range(V + 1))


def union(a, b):
    a = find(a)
    b = find(b)

    if b < a:
        parent[a] = b
    else:
        parent[b] = a


def find(a):
    if a == parent[a]:
        return a
    parent[a] = find(parent[a])  # ๊ฒฝ๋กœ ์••์ถ•
    return parent[a]


sum = 0
for w, s, e in edge:
    if find(s) != find(e):
        union(s, e)
        sum += w

print(sum)

์—ฐ์‚ฐ์†๋„๊ฐ€ 20% ์ •๋„ ๋น ๋ฅธ ํ’€์ด๋ฅผ ๋ฐœ๊ฒฌํ•ด์„œ ๋ถ„์„ํ•ด๋ณด๋ ค ํ•œ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋‚ด ์ฝ”๋“œ๋ž‘ ํŠน๋ณ„ํžˆ ๋‹ค๋ฅธ ๋ถ€๋ถ„์€ ์—†๊ณ , ๋‚˜๋Š” heap์„ ์ด์šฉํ–ˆ์ง€๋งŒ ์ด ๋ถ„์€ list๋กœ ์ž…๋ ฅ์„ ์ฒ˜๋ฆฌํ•œ ๋’ค, ํ•œ ๋ฒˆ์— sort๋ฅผ ์ด์šฉํ•ด ๊ฐ€์ค‘์น˜๋ณ„๋กœ ์ •๋ ฌํ•˜์…จ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ ์กฐ๊ธˆ ๋‹ค๋ฅด๋‹ค๊ณ  ๋Š๋‚€ ์ ์€ union์—์„œ a์™€ b์˜ ํฌ๊ธฐ๋ฅผ ๋น„๊ตํ•˜์—ฌ ๋” ์ž‘์€ ๋ฃจํŠธ ๋…ธ๋“œ ๋ฒˆํ˜ธ๋กœ ๊ฐฑ์‹ ๋˜๋„๋ก ํ–ˆ๋‹ค๋Š” ์ ์ด ํŠน์ดํ–ˆ๋‹ค. ์–ด์ฐจํ”ผ ๊ฐ™์€์ง€ ์—ฌ๋ถ€๋ฅผ ํŒ๋‹จํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํฌ๊ฒŒ ์˜๋ฏธ๊ฐ€ ์—†์ง€ ์•Š์€๊ฐ€?

heap์ด ๋” ๋น ๋ฅด๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋Š”๋ฐ list์™€ ์ •๋ ฌ ๋ฐฉ์‹์ด ๋” ๋น ๋ฅด๊ธฐ ๋•Œ๋ฌธ์ด๋ผ๋Š” ์ ์ด ์—ฐ์‚ฐ์†๋„์˜ ๊ฒฐ์ •์  ์š”์ธ์ด๋ผ๊ณ  ๋Š๊ผˆ๊ณ , ์ด ๋•Œ๋ฌธ์— ๋‚ด ํ’€์ด์—์„œ list์™€ ์ •๋ ฌ์„ ์‚ฌ์šฉํ•ด ๋‹ค์‹œ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด์•˜๋‹ค.

import sys
input = sys.stdin.readline


def find_set(x):
    global p
    if x != p[x]:
        p[x] = find_set(p[x])
    return p[x]


def union(u, v, w):
    global cnt, ret, p
    a, b = find_set(u), find_set(v)
    if a != b:
        p[a] = b
        ret += w
        cnt += 1


V, E = map(int, input().split())
Q = []
for _ in range(E):
    u, v, w = map(int, input().split())
    Q.append((w, u, v))
Q.sort()

p = [i for i in range(V+1)]
ret = 0
cnt = 0
i = 0
while cnt < V-1:
    w, u, v = Q[i]
    union(u, v, w)
    i += 1

print(ret)

์—ฐ์‚ฐ์†๋„๊ฐ€ 480ms์—์„œ 380ms๋กœ 20% ์ •๋„ ๊ฐ์†Œํ•˜์˜€๋‹ค. ์—ญ์‹œ heap์ด ๋ฌธ์ œ์˜€๋‹ค.

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ