Skip to content

Commit 3c3284c

Browse files
committed
feat: 3월 1주차 발제 자료 업로드 (UnionFind)
1 parent 8348f16 commit 3c3284c

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)