-
Notifications
You must be signed in to change notification settings - Fork 2
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
55-tgyuuAn #189
55-tgyuuAn #189
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.
κ° νΈλ¦¬(μ§ν©)μ 무κ²(ν¬κΈ°)λ₯Ό μ μ₯νλ κ²μ΄ λΉλ¨ μ΄ λ¬Έμ λ§μ ν€λ μλκΈ΄ ν©λλ€.
union-find μκ³ λ¦¬μ¦μ μ΅μ ννλ 2κ°μ§ λ°©λ²μ΄ μλλ°, νλκ° μ μλ €μ§ "Path-Compression"μ΄κ³ λ€λ₯Έ νλκ° μ΄ λ¬Έμ μμ μ°μΈ "Weighted Union" μ
λλ€.
ν΅μμ μΈ union-find λ¬Έμ λ€μ path-compressionλ§ μ μ©μμΌλ μ΄μ§κ°νλ©΄ μκ° λ¬Έμ κ° λ€ ν΄κ²°λ©λλ€λ§.... ν λ²μ―€μ κ·Έλλ νμΈν΄λλ κ²λ μ’μ κ² κ°μμ :)
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
from collections import defaultdict
def input(): return sys.stdin.readline().rstrip()
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
network[a] += network[b]
return network[a]
for _ in range(int(input())):
num = int(input())
parent = defaultdict(str)
network = defaultdict(lambda : 1)
for i in range(num):
a, b = input().split()
if a not in parent: # λΆλͺ¨μ λ±λ‘λμ§ μμ κ²½μ°
parent[a] = a
if b not in parent:
parent[b] = b
print(union(a, b)) # μΉκ΅¬ κ΄κ³ ν©μΉκΈ°
graph[y] = x | ||
count[x] += count[y] | ||
count[y] = 0 |
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.
νΉμ ν©μΉλ κ³Όμ μμ count[y]λ₯Ό μ΄κΈ°ν ν΄μ£Όλ μ΄μ κ° μλμ??
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.
μ μ§ νλ² λΆλͺ¨ λ Έλλ‘ λ³ν©λ μΉκ΅¬λ€μ΄ μΆνμ λ€μ μ¬μ©λ κ² κ°λ€λ μκ°μ΄ λ€μ΄μ μ΄κΈ°νλ₯Ό νλλ° κ·Έλ΄ κ²½μ°λ μλμ ...?
μ μ€ λΉΌλ ν΅κ³ΌνκΈ΄ νλ€μ ,,,
F = int(input()) | ||
index = 1 | ||
parent = defaultdict(str) | ||
count = defaultdict(lambda : 1) |
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.
μ΄λ κ² defaultκ° μ§μ μ΄ λλκ΅°μ
network[a] = 1
λ‘ νλ€κ° λ°λ‘ μμ νμ΅λλ€ γ γ γ
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.
@H0ngJu π«‘π«‘π«‘
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 κ° λ§λμ§λ λͺ¨λ₯΄κ² λλ° λΉμ·νκ² νΌ κ² κ°μμ!
head λ₯Ό μ°Ύμμ κ·Έ headλΌλ¦¬ λΉκ΅ν΄μ λ μμ κ²μΌλ‘ count λ³ν©ν©λλ€.
μ€λͺ
νκ³ λ³΄λκΉ λκ°μ κ² κ°μλ° μ μ½λκ° μ‘°κΈ λ³΅μ‘νλ€μ©..!
import java.io.BufferedReader
import java.io.InputStreamReader
private lateinit var br: BufferedReader
fun main() {
br = BufferedReader(InputStreamReader(System.`in`))
val t = br.readLine().toInt()
repeat(t) {
makeNetwork()
}
}
private fun makeNetwork() {
val f = br.readLine().toInt()
val network = mutableMapOf<Int, Int>()
val friendIndex = mutableMapOf<String, Int>()
val count = mutableMapOf<Int, Int>()
var index = -1
repeat(f) {
val (a, b) = br.readLine().split(" ")
val aIndex: Int = friendIndex.getOrPut(a) {
index++
network[index] = index
count[index] = 1
index
}
val bIndex: Int = friendIndex.getOrPut(b) {
index++
network[index] = index
count[index] = 1
index
}
var aHead = network[aIndex]!!
while (aHead != network[aHead]) {
aHead = network[aHead]!!
}
var bHead = network[bIndex]!!
while (bHead != network[bHead]) {
bHead = network[bHead]!!
}
if (aHead == bHead) {
println(count[aHead])
} else if (aHead < bHead) {
network[bHead] = aHead
count[aHead] = count[aHead]!! + count[bHead]!!
println(count[aHead])
} else {
network[aHead] = bHead
count[bHead] = count[aHead]!! + count[bHead]!!
println(count[bHead])
}
}
}
parent = defaultdict(str) | ||
count = defaultdict(lambda : 1) |
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.
defaultdictλ₯Ό μ²μ λ³΄κ² λμ μ΄λ€κ±΄μ§ μ°Ύμλ΄€λλ°
λμ
λ리 μΈμλ₯Ό μ΄κΈ°ννμ¬ μ¬μ©νλλ° μ¬μ© λλκ΅°μ!!!
λ§€λ² κ·Έλ₯ μ΄κΈ°νν λ μ΅μ§λ‘ κ°μ λ£μ΄μ€μ μ¬μ©νλλ° μ¬μ©νλ©΄ νΈν κ² κ°λ€μ!!
μ§μμ΄ λμ΄λ¬μ΅λλ€!!!!! κ°μ¬ν©λλ€!!
text = "Life is too short, You need python."
d = dict()
for c in text:
if c not in d:
d[c] = 1
else :
d[c] += 1
print(d)
from collections import defaultdict
text = "Life is too short, You need python."
d = defaultdict(int)
for c in text:
d[c] += 1
print(dict(d))
{'L': 1, 'i': 2, 'f': 1, 'e': 3, ' ': 6, 's': 2, 't': 3, 'o': 5, 'h': 2, 'r': 1, ',': 1, 'Y': 1, 'u': 1, 'n': 2, 'd': 1, 'p': 1, 'y': 1, '.': 1}
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.
@alstjr7437 π«‘π«‘π«‘
κ·Έκ±° μμ² μμ² μ μ©ν κ±°λκΉ μ²΄ν νλλ‘νμμ§μ ~~πππ
π λ¬Έμ λ§ν¬
μΉκ΅¬ λ€νΈμν¬
βοΈ μμλ μκ°
40λΆ
β¨ μλ μ½λ
μκ³ λ¦¬μ¦ λ―Έλ¦¬λ³΄κΈ° λ°©μ§μ© λ«μν¬λ μ λλ₯
μ νμ μΌλ‘ μκ³ λ¦¬μ¦ λ ΈνΈ μ μλ μλ‘μ μ§ν© κ°λ μ μκ³ μμ΄μΌ ν©λλ€.
(νν μ λμ¨ νμΈλλΌκ³ λΆλ¦ λλ€.)
μ λμ¨ νμΈλ κ°λ μ λͺ¨λ₯΄λ©΄ ν μ μμ΅λλ€....!
μμ μκ³ λ¦¬μ¦ λ ΈνΈ λ³΄μκ³ μΆμ² λ¬Έμ λ₯Ό νμ΄λ³΄μ λ€μ μ΄ λ¬Έμ λ₯Ό νμ΄λ³΄μκΈ°λ₯Ό μΆμ²λ립λλ€!
κΈ°μ‘΄ μ λμ¨ νμΈλκ° μΈλ±μ€λ‘ λΆλͺ¨ νμ νλ³νλλΌλ©΄,
μ΄ λ¬Έμ λ ν΄μ¬ κ°μΌλ‘ λΆλͺ¨λ₯Ό νλ³ν©λλ€.
κ·Έ κ²λ§κ³ λ λ±ν λ μ΄λ €μ΄ λΆλΆμ μμ΅λλ€.
μ²μμ μλμ κ°μ΄ λΆλͺ¨ μ λ³΄κ° μλ€κ³ ν΄λ΄ μλ€.
λΉμ°ν μ΄κΈ°μλ μ λ³΄κ° μμΌλ λΉμ΄μμ κ² μ λλ€.
κ·Έ λ€μμΌλ‘
Barney
μFred
κ° μΉκ΅¬λΌλ μ λ³΄κ° μ£Όμ΄μ‘μ΅λλ€.λ μ€μ νλλ₯Ό κΈ°μ€μΌλ‘ λΆλͺ¨ μ 보λ₯Ό κ°±μ μμΌ μ€λλ€.
(μ΄ λ λλΆλΆμ μΈμ΄μμλ λ¬Έμμ΄λ min νΉμ maxκ° λ¨Ήν κ² μ λλ€.)
κ·Έ λ€μμΌλ‘
Barney
μBetty
κ° μΉκ΅¬λΌλ μ λ³΄κ° λ λ€μ΄μ΅λλ€.κ·ΈλΌ μλ κ·Έλ¦Όμ²λΌ λ κ² μ λλ€.
λ§μ§λ§μΌλ‘
wilma
μBetty
κ° μΉκ΅¬λΌλ μ λ³΄κ° λ€μ΄μ΅λλ€.κ·ΈλΌ μλ κ·Έλ¦Όμ²λΌ λ κ² μ λλ€.
μ΄ λ, κ° μΉκ΅¬ μ λ³΄κ° μ£Όμ΄μ‘μ λ λ μ¬λμ μΉκ΅¬ λ€νΈμν¬μ λͺ λͺ μ΄ μλ μ§ μ΄λ»κ² ꡬνλλ ?
맀λ²
union-find
λ₯Ό ν λ λ§λ€ λͺ¨λ νμ λ€μ Έλ³Ό κ²½μ° μκ° μ΄κ³Όκ° λ©λλ€μ΄λ»κ² μμλκ³ μ?
μ λ μκ³ μΆμ§ μμμ΄μ.
κ·Έλ κΈ° λλ¬Έμ κ° λΆλͺ¨ λ Έλλ₯Ό κΈ°μ€μΌλ‘ λͺ λͺ μ μΉκ΅¬ λ€νΈμν¬κ° νμ±λμ΄ μλ μ§λ₯Ό 미리 κ³μ°μ ν΄ λμμΌ ν©λλ€.
μ£Όμμ μ±νλμ μν΄ μ¨λμμ΄μ.
μ μ½λμ²λΌ μ΄κΈ° νμ΄ λ€μ΄κ° λ
count
μ κ° λ Έλκ° 1λ‘ μ€μ λκ² ν λ€μ,union
κ³Όμ μμ μμ λ Έλμcount
κ°μ λΆλͺ¨ λ Έλμcount
λ‘ λ³ν© μμΌμ£Όλ©΄ λ©λλ€.μ΄λ¬λ©΄ 맀 λ¨κ³λ§λ€ μΌμΌμ΄ λͺ¨λ λ Έλλ₯Ό νμν νμκ° μμ΄μ Έμ!
π μλ‘κ² μκ²λ λ΄μ©