Skip to content

Commit b376030

Browse files
authored
Merge pull request #149 from AlgorithmStudy-Allumbus/minjeong
feat: 3μ›” 1μ£Όμ°¨ μŠ€ν„°λ”” 자료 μ—…λ‘œλ“œ (UnionFind)
2 parents 8348f16 + 3c3284c commit b376030

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'''
2+
BOJ #1717. μ§‘ν•©μ˜ ν‘œν˜„ (κ³¨λ“œ5)
3+
https://www.acmicpc.net/problem/1717
4+
μœ ν˜•: Union-Find, Graph
5+
'''
6+
7+
# PR 올릴 λ•Œ 과제 μ •λ‹΅ 곡개 μ˜ˆμ •
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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)
608 KB
Binary file not shown.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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)

0 commit comments

Comments
Β (0)