-
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
3-dhlee777 #14
3-dhlee777 #14
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.
μΌμ’ μ νΈλ¦¬ λ§λ€κΈ°κ΅°μ! λ¬Έμ λ§ μ½κ³ μΌμΌμ΄ λ Έλ λ§λ€μ΄μ ν΄κ²°νλ? μΆμλλ° 1μ°¨μ λ°°μ΄μ λΆλͺ¨ κ°λ§ μ μ₯νλ©΄ λ¬Έμ κ° μλ€μ! μ½λκ° μ½κΈ° μ¬μμ μ’μμ΅λλ€ :)
μ½λ λΈλμ ```cpp μ²λΌ μΈμ΄ λͺ μ λͺ μν΄μ£Όλ©΄ μ½λ λΈλ νμ΄λΌμ΄ν λ©λλ€! μ΄λ²μ μ κ° κ³ μ³λ¨μ΄μ! |
ν κ°μ¬ν©λλ€ λ€μλΆν°λ μ½λλΈλ νμ΄λΌμ΄ν λ μμκ² μ λ κ² νκ² μ΅λλ€..! |
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.
μ λμ¨ νμΈλλ μ²μ 보λ€μ! μλ‘μ΄ μκ³ λ¦¬μ¦ μ λ³΄κ³ κ°λλ€.
π π π |
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.
Union find
λ μ°κ²°λμ΄ μλ μλ¨μ κ³΅ν΅ λΆλΆμ μ°Ύμ λΉκ΅νλ€λ μ μμ μ²μ λ΄€μμ λ λκ² ν·κ°λ € νλ μ μ΄ κΈ°μ΅μ΄ λλκ΅°μ..
λλΆλΆμ λ¬Έμ λ visitedλ‘ λ°©λ¬Ένλμ§ μνλμ§ λΉκ΅λ§ ν΄μ£Όλ©΄ λλ λ¬Έμ λ€μ΄ μ£Όλ‘ λμ€λ€κ°λ£¨νΈλ₯Ό λΉκ΅ν΄μ 곡ν΅μ μ μ°Ύλ λΆλΆμ΄ Union find
λ₯Ό νκ²λλ©΄μ μ μ νμλ κ² κ°μ΅λλ€.
λ°μ΄λ¬μ€ ν΄λΉ λ¬Έμ μμ μ λ visitedλ‘ νμ΄μ€¬μ§λ§ Union find
λ‘λ ν μ μμ΄μ μ¬λ°μμ΅λλ€. λ¬Έμ μΆμ²λ립λλ€!
μ΄λ¬ν νμλ¬Έμ λ€ μ€ heap
κ΄λ ¨ λ¬Έμ λ€κ³Ό λ¬Έμμ΄ νμμμ νΈλΌμ΄
λ¬Έμ λ€μ΄ μλλ° ν λ² μμ보μλ©΄ μΆνμ λμ΄λ μλ λ¬Έμ λ€μ νμ΄νμλλ° λμμ΄ λ κ±°λΌ μκ°ν©λλ€!
ν..μ λ |
π λ¬Έμ λ§ν¬
https://www.acmicpc.net/problem/1717
βοΈ μμλ μκ°
3μκ°
β¨ μλ μ½λ
μ λμ¨ νμΈλ(uion-find)
λ λνμ μΈ κ·Έλν μκ³ λ¦¬μ¦μΌλ‘ νΉμ λ λ Έλκ° κ°μμ§ν©μ μν΄μλμ§ νλ³ν΄μ£Όλ μκ³ λ¦¬μ¦μ΄λ€.ν¬κ² νΉμ λ μ§ν©μ ν©μΉλ
union μ°μ°
κ³Ό 루νΈλ Έλλ₯Ό μ°Ύλfind μ°μ°
μΌλ‘ μ΄λ£¨μ΄μ§λ€.μ°μ
parent λ°°μ΄
μ μ΄λ€λ ΈλμλΆλͺ¨λ Έλ
λ₯Ό μ μ₯νλ λ°°μ΄λ‘ μ΅μ’ μ μΌλ‘λ μ΄λ€λ Έλμ루νΈλ Έλ
κ° μ μ₯λλ€.find_parent(int a) ν¨μ
λ aμ 루νΈλ Έλλ₯Ό μ°Ύλ ν¨μλ‘μ¬κ·νΈμΆ
μ ν΅ν΄ aμ 루νΈλ Έλλ₯Ό μ°ΎμλΈλ€.add(int a, int b) ν¨μ
λ aμ§ν©κ³Ό bμ§ν©μ ν©μΉλ ν¨μλ‘ λ μμ루νΈλ Έλ
λ₯Ό κ°κ° μ°Ύμ μ°κ²°ν΄μ€λ€.compare(int a,int b)ν¨μ
λ λ μκ°κ°μμ§ν©
μ μν΄μλμ§ κ° λ Έλμ루νΈλ Έλ
λ₯Ό ν΅ν΄ νμΈνλ ν¨μμ΄λ€.num
,μ μ°μ°μ κ°μcalculate_num
μ κ°κ° μ λ ₯λ°λλ€.parent[j] = j
μ ν΅ν΄ μ΄κΈ°νν΄μ€λ€.0 b c
μ ννμΌ κ²½μ° ν©μ§ν©μ°μ°μ΄λ―λ‘add()
νΈμΆ1 b c
ννμΌ κ²½μ° λ μκ° κ°μμ§ν©μ μλμ§ λΉκ΅ν΄μΌνλ―λ‘compare()
νΈμΆπ μλ‘κ² μκ²λ λ΄μ©
μ μΌ μ²μμ
find_parent()
ν¨μμ λ¦¬ν΄ λΆλΆμμreturn find_parent(parent[a]);
λ₯Ό νλλμκ°μ΄κ³Ό
κ° κ³μ λ¬λ€.κ·Έμν
return parent[a]=find_parent(parent[a]);
λ‘ parent[a]μ κ²°κ³Όλ₯Ό μ μ₯νλ μ½λλ₯Ό μΆκ° νλλ μκ°μ΄κ³Όλ¬Έμ κ°ν΄κ²°λμλ€. μλ§ μ΄κ³Όμ μ ν΅ν΄
parent λ°°μ΄
μ κ°μλΆλͺ¨λ Έλ
κ°μμ루νΈλ Έλ
μ κ°μΌλ‘ λ³νμμΌμ£Όλ κ³Όμ μμμκ°μ΄κ³Όκ° ν΄κ²°λ κ² κ°λ€.