File tree Expand file tree Collapse file tree 4 files changed +74
-0
lines changed
_WeeklyChallenges/W13-[Union-Find] Expand file tree Collapse file tree 4 files changed +74
-0
lines changed Original file line number Diff line number Diff line change 1+ '''
2+ BOJ #1717. μ§ν©μ νν (골λ5)
3+ https://www.acmicpc.net/problem/1717
4+ μ ν: Union-Find, Graph
5+ '''
6+
7+ # PR μ¬λ¦΄ λ κ³Όμ μ λ΅ κ³΅κ° μμ
Original file line number Diff line number Diff line change 1+ ## π3μ 1μ£Όμ°¨ (3/3) μ€ν°λ λ°μ μ£Όμ : Union Find
2+ > λ°μ μ: κΉλ―Όμ (@Mingguriguri )
3+
4+ > μ£Όμ : Union Find
5+ ### ποΈ μ€ν°λ μλ£
6+ - PDF: [ λ°λ‘κ°κΈ°
7+ ] ( ./Study_BOJ_1976.pdf )
8+
9+ [ // ] : # ( <img width="500" alt="μ€ν°λλ¬Έμ " src="https://github.com/user-attachments/assets/96863997-1cfd-454b-b1b0-634606cc4108" /> )
10+
11+ [ // ] : # ( <img width="500" alt="λ°μ λ¬Έμ " src="https://github.com/user-attachments/assets/10717940-e761-4a5c-8cf0-e75935d84633" /> )
12+
13+ ### π λ¬Έμ
14+ - [ λ°±μ€ #1976 . μ¬ν κ°μ] ( https://www.acmicpc.net/problem/1976 ) : Union-Find / 골λ4
15+ - μ λ΅ μ½λ: [ Study_BOJ_1976_μ¬νκ°μ.py] ( ./Study_BOJ_1976_μ¬νκ°μ.py )
16+
17+ ### π» κ³Όμ
18+ - [ λ°±μ€ #1717 . μ§ν©μ νν] ( https://www.acmicpc.net/problem/1717 ) : Union-Find / 골λ5
19+ - μ λ΅ μ½λ: [ Assignment_BOJ_1717_μ§ν©μνν.py] ( ./Assignment_BOJ_1717_μ§ν©μνν.py )
Original file line number Diff line number Diff line change 1+ '''
2+ BOJ #1976. μ¬ν κ°μ (골λ4)
3+ https://www.acmicpc.net/problem/1976
4+ μ ν: Union-Find, Graph
5+ '''
6+ import sys
7+ sys .setrecursionlimit (10 ** 8 )
8+ input = sys .stdin .readline
9+
10+ def union (a , b ):
11+ p_a = find (a )
12+ p_b = find (b )
13+
14+ if p_a > p_b : # aμ λνλ³΄λ€ bμ λνκ° λ μμ κ°μ κ°μ§λ©΄,
15+ parent [p_a ] = p_b # aκ° μν μ§ν©μ bμ μ§ν©μ ν©μΉκΈ°
16+ else :
17+ parent [p_b ] = p_a # κ·Έλ μ§ μμΌλ©΄ bμ μ§ν©μ aμ μ§ν©μ ν©μΉκΈ°
18+
19+ def find (a ):
20+ if a == parent [a ]: # aκ° μκΈ° μμ μ λΆλͺ¨μ΄λ©΄ λν λ
Έλ
21+ return a
22+
23+ parent [a ] = find (parent [a ]) # κ²½λ‘ μμΆμ ν΅ν΄ aμ λΆλͺ¨λ₯Ό λν λ
Έλλ‘ μ¬μ€μ
24+ return parent [a ]
25+
26+ # λμμ κ°μ nκ³Ό μ¬ν κ³νμ ν¬ν¨λ λμμ μ mμ μ
λ ₯λ°κΈ°
27+ n = int (input ())
28+ m = int (input ())
29+
30+ parent = [i for i in range (n )] # κ° λμλ μ²μμ μκΈ° μμ μ΄ λν λ
Έλ.
31+
32+ # nκ°μ μ€μ κ±Έμ³ λμ κ° μ°κ²° μ 보λ₯Ό μ
λ ₯λ°κ³ , μ°κ²°λμ΄ μμΌλ©΄ union μ°μ° μν
33+ for i in range (n ):
34+ arr = list (map (int , input ().split ()))
35+ for j in range (n ):
36+ if arr [j ]: # 1μ΄λ©΄ iμ jκ° μ°κ²°λμ΄ μμΌλ―λ‘ union μ°μ° μν
37+ union (i , j )
38+
39+ # μ¬ν κ³νμ μ
λ ₯λ°λλ€.
40+ plan = list (map (int , input ().split ()))
41+ result = "YES"
42+ # μ¬ν κ³νμ μλ λͺ¨λ λμκ° κ°μ μ§ν©(λν λ
Έλ)μ κ°μ§λμ§ νμΈ
43+ for i in range (1 , m ):
44+ if parent [plan [i ]- 1 ] != parent [plan [0 ]- 1 ]:
45+ result = "NO"
46+ break
47+
48+ print (result )
You canβt perform that action at this time.
0 commit comments