Skip to content
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

17-seongwon030 #67

Merged
merged 1 commit into from
Jul 22, 2024
Merged

17-seongwon030 #67

merged 1 commit into from
Jul 22, 2024

Conversation

seongwon030
Copy link
Collaborator

@seongwon030 seongwon030 commented Jul 5, 2024

πŸ”— 문제 링크

μ΅œμ†ŒμŠ€νŒ¨λ‹νŠΈλ¦¬

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

1μ‹œκ°„ 30λΆ„

✨ μˆ˜λ„ μ½”λ“œ

크루슀칼 μ•Œκ³ λ¦¬μ¦˜

κ°€μ€‘μΉ˜κ°€ 높은 간선을 μ œκ±°ν•˜λ©΄μ„œ μ΅œμ†Œ λΉ„μš© μ‹ μž₯ 트리λ₯Ό λ§Œλ“œλŠ” μ•Œκ³ λ¦¬μ¦˜

μˆœμ„œ

μ˜ˆμ‹œ

  1. 초기 μƒνƒœλŠ” κ·Έλž˜ν”„μ˜ κ°€μ€‘μΉ˜λ₯Ό 따라 λ‚΄λ¦Όμ°¨μˆœμœΌλ‘œ μ •λ ¬ν•œλ‹€.

  1. κ°€μž₯ 높은 κ°€μ€‘μΉ˜μ˜ 간선을 μ œκ±°ν•œλ‹€.
    => κ°€μ€‘μΉ˜κ°€ 17인 κ°„μ„ (A,C) 제거


2. λ‹€μŒμœΌλ‘œ 높은 κ°€μ€‘μΉ˜μ˜ 간선을 μ œκ±°ν•œλ‹€.
=> κ°€μ€‘μΉ˜κ°€ 14인 κ°„μ„ (F,G)제거


3. 남은 κ°„μ„  쀑 κ°€μ€‘μΉ˜κ°€ κ°€μž₯ 높은 κ°„μ„  (B,G)λ₯Ό μ œκ±°ν•œλ‹€.

4. 남은 κ°„μ„  쀑 κ°€μ€‘μΉ˜κ°€ κ°€μž₯ 높은 κ°„μ„  (C,E)λ₯Ό μ œκ±°ν•œλ‹€.

5.남은 κ°„μ„  쀑 κ°€μ€‘μΉ˜κ°€ κ°€μž₯ 높은 κ°„μ„  (D,E)λ₯Ό μ œκ±°ν•˜λ©΄ κ·Έλž˜ν”„κ°€ λΆ„λ¦¬λ˜μ–΄ λ‹¨μ ˆ κ·Έλž˜ν”„κ°€ λœλ‹€. κ·Έ λ‹€μŒμœΌλ‘œ 높은 κ°€μ€‘μΉ˜μ˜ κ°„μ„  (C,F)λ₯Ό μ œκ±°ν•œλ‹€. ν•˜μ§€λ§Œ (C,F)λ₯Ό μ œκ±°ν•˜λ©΄ 정점 Cκ°€ λΆ„λ¦¬λ˜λ―€λ‘œ μ œκ±°ν•  수 μ—†λ‹€. κ·ΈλŸ¬λ―€λ‘œ λ‹€μŒμœΌλ‘œ κ°€μ€‘μΉ˜κ°€ 높은 κ°„μ„  (A,D)λ₯Ό μ œκ±°ν•œλ‹€.

6. ν˜„μž¬ 남은 κ°„μ„  μˆ˜κ°€ 6κ°œμ΄λ―€λ‘œ 정점 7개, κ°„μ„  6개둜 μ•Œκ³ λ¦¬μ¦˜ μˆ˜ν–‰μ„ μ’…λ£Œν•˜λ©΄ μ‹ μž₯ νŠΈλ¦¬κ°€ μ™„μ„±λœλ‹€.

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) 

findν•¨μˆ˜

주어진 λ…Έλ“œ x의 μ΅œμƒμœ„ λΆ€λͺ¨(λ£¨νŠΈλ…Έλ“œ)λ₯Ό μ°ΎλŠ”λ‹€. 경둜 압좕을 톡해 트리의 높이λ₯Ό μ€„μ΄λŠ” 역할을 ν•œλ‹€.

ν¬λ£¨μŠ€μΉΌμ„ μ΄μš©ν•œ MST

  • κ°„μ„  리슀트λ₯Ό μˆœνšŒν•˜λ©°, 각 간선이 두 정점(a,b)λ₯Ό μ—°κ²°ν•œλ‹€. 각 μ •μ μ˜ λΆ€λͺ¨ λ…Έλ“œλ₯Ό μ°Ύκ³ , λΆ€λͺ¨ λ…Έλ“œκ°€ λ‹€λ₯΄λ©΄ 두 λ…Έλ“œλ₯Ό ν•©μΉœλ‹€. μ΄λ•Œ κ°€μ€‘μΉ˜ cλ₯Ό ans에 λ”ν•œλ‹€.
  • 두 μ •μ μ˜ λΆ€λͺ¨κ°€ λ‹€λ₯΄λ©΄, 사이클을 μƒμ„±ν•˜μ§€ μ•ŠμœΌλ―€λ‘œ 간선을 MST에 ν¬ν•¨μ‹œν‚¨λ‹€.
  • 큰 root값을 μž‘μ€ rootκ°’μœΌλ‘œ λ§Œλ“œλŠ” 건 큰 κ°€μ€‘μΉ˜μ˜ κ°„μ„ λΆ€ν„° μ‚­μ œν•˜λŠ” 크루슀칼의 νŠΉμ§•μ„ 보여쀀닀.

μƒˆλ‘­κ²Œ μ•Œκ²Œ 된 점

자료ꡬ쑰 배울 λ•Œ μ•„ μ΄λŸ°κ±°κ΅¬λ‚˜ ν•˜κ³  λ„˜μ–΄κ°”μ—ˆλŠ”λ° μ œλŒ€λ‘œ λ°°μš°λ‹ˆκΉŒ μž¬λ°Œλ„€μš”

Copy link
Collaborator

@InSange InSange left a comment

Choose a reason for hiding this comment

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

γ…‹γ…‹γ…‹γ…‹ μ„€λ§ˆ 책에 있던 것을 κ·ΈλŒ€λ‘œ..?

μ΅œμ†Œ μ‹ μž₯ νŠΈλ¦¬λŠ” λŒ€ν‘œμ μœΌλ‘œ ν”„λ¦Ό, 크루슀칼 μ•Œκ³ λ¦¬μ¦˜μ΄ μ‘΄μž¬ν•˜μ£ 
κ·Έλž˜ν”„λ₯Ό 이을 λ•Œ 거리λ₯Ό κΈ°μ€€μœΌλ‘œ μ—°κ²°ν•  것이냐? μ•„λ‹ˆλ©΄ μ–΄λŠ ν•œ λ…Έλ“œμ—μ„œ 짧은 간선듀을 μ—°κ²°ν•˜μ—¬ μ—°κ²°ν•  κ²ƒμ΄λƒλ‘œ λ‚˜λ‰˜κ²Œ 되죡
μ—¬κΈ°μ„œ 더 λ‚˜μ•„κ°€λ©΄ union-find도 μ μš©ν•  수 μžˆλŠ”λ° 이것듀 λͺ¨λ‘ λ‹€ κ²½ν—˜ν•΄μ„œ λ§ˆμŠ€ν„°ν•΄λ³΄λŠ” 것을 μΆ”μ²œλ“œλ¦Όλ‹ˆλ‹€!

Copy link
Collaborator

@yuyu0830 yuyu0830 left a comment

Choose a reason for hiding this comment

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

닡을 달아주지 μ•ŠλŠ”κ΅°μš”.. 졜근 MST에 빠지신 것 κ°™λ„€μš” γ…Žγ…Ž 이 참에 μ™„μ „ 정볡 ν•˜μ‹œκΈΈ

edge.append(list(map(int,input().split())))

# λΉ„μš©μ„ κΈ°μ€€μœΌλ‘œ μ˜€λ¦„μ°¨μˆœ
edge.sort(key=lambda x:x[2])
Copy link
Collaborator

Choose a reason for hiding this comment

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

μ—¬κΈ°λŠ” x[2] λ₯Ό κΈ°μ€€μœΌλ‘œ μ •λ ¬ν•œλ‹€λŠ” μ˜λ―ΈμΈκ°€μš”?

Copy link
Collaborator

Choose a reason for hiding this comment

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

λ‹΅ν•΄μ€˜

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ν—‰ μ™œ 닡을 μ•ˆν–ˆμ§€! λ„€ λ§žμŠ΅λ‹ˆλ‹Ή

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

예둜

 (lambda x,y: x + y)(10, 20)

λŠ” 10κ³Ό 20을 λ”ν•œ 값인 30이 λ‚˜μ˜€λŠ”λ°

lambda λ§€κ°œλ³€μˆ˜ : ν‘œν˜„μ‹ 

으둜 νŒŒμ΄μ¬μ—μ„œλŠ” ν‘œν˜„ν•΄μš”.

Copy link
Collaborator

Choose a reason for hiding this comment

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

λžŒλ‹€ν•¨μˆ˜λŠ” 봐도봐도 ν—·κ°ˆλ¦¬λ„€μš”..

@seongwon030 seongwon030 merged commit 645a3da into main Jul 22, 2024
1 check passed
@seongwon030 seongwon030 deleted the 17-seongwon030 branch July 22, 2024 14:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants