https://www.acmicpc.net/problem/1043
μμ΄λμ΄
1. λ°λ³΅λ¬Έ
- λ¬Έμ λλμ΄ μ§μ€μ μλ μ¬λμ΄ μ’λΉκ°μ΄ κ°μΌμν€λ κ²μ΄λΌ μκ°
- κ·Έλμ μ§μ€μ μλ μ¬λκ³Ό κ°μ΄ μλ μ¬λλ€μ μ§μ€μ μλ μ¬λμΌλ‘ λ°κΏ¨λ€.
- κ·Όλ° μ΄λ¬λ©΄ μ§μ€μ μλ μ¬λλ€μ΄ λ°λμ΄μ μ 체 νν°λ₯Ό λ νμν΄μΌ νκ³ μ΄λ₯Ό κ³μ λ°λ³΅ν΄μΌ ν΄μ μ€ν¨νλ€.
2. κ·Έλν
- κ·Έλ¬λ€κ° λμμ ν λ²μ ν΄μΌ λλ€λ κ²μ κΉ¨λ«κ³ κ·Έλνλ₯Ό μ¬μ©νκΈ°λ‘ κ²°μ
- κ° νν°λ§λ€ μλ μ¬λλ€μ λ λͺ μ© μλ°©ν₯μΌλ‘ μ΄μ΄μ£Όλ κ±Έλ‘ κ·Έλνλ₯Ό λ§λ€μλ€.
- κ·Έλ¦¬κ³ μ²μ μ§μ€μ μλ μ¬λλ€λ‘ μμν΄ νμμ νλ©΄ ν λ²μ μ§μ€μ μλ μ¬λλ€μ λͺ¨λ μ°Ύμ μ μλ€.
μ 체 μ½λ
import sys
from collections import deque
from itertools import combinations
N, M = map(int,input().split())
ans = 0
truth_flag = False
party_list = []
who_know_truth = [False] * (N + 1)
know = [int(x) for x in sys.stdin.readline().rstrip().split()]
know_member = know[1:]
for k in know_member:
who_know_truth[k] = True
graph = [[] for _ in range(N + 1)]
for _ in range(M):
party = [int(x) for x in sys.stdin.readline().rstrip().split()]
party_member = party[1:]
party_list.append(party_member)
for pair in combinations(party_member,2):
if not pair[1] in graph[pair[0]]:
graph[pair[0]].append(pair[1])
if not pair[0] in graph[pair[1]]:
graph[pair[1]].append(pair[0])
def bfs(n):
deq = deque()
deq.append(n)
visited[n] = True
while deq:
x = deq.popleft()
for i in graph[x]:
if not visited[i]:
who_know_truth[i] = True
visited[i] = True
deq.append(i)
for i in know_member:
visited = [False] * (N + 1)
bfs(i)
ans = M
for party in party_list:
for p in party:
if who_know_truth[p] == True:
ans -= 1
break
print(ans)
μ½λ μ€λͺ
who_know_truth = [False] * (N + 1)
know = [int(x) for x in sys.stdin.readline().rstrip().split()]
know_member = know[1:]
for k in know_member:
who_know_truth[k] = True
μ²μμ μ§μ€μ μλ μ¬λλ€μ μ€λ‘ μ λ ₯λ°μΌλ©΄ 리μ€νΈ 맨 μμλ μ¬λ μ μ΄λ―λ‘ μ¬λΌμ΄μ± ν΄μ€λ€.
κ·Έλ¦¬κ³ μ§μ€μ μλ μ¬λμ λ²νΈλ Trueλ‘ νμν΄μ€λ€.
graph = [[] for _ in range(N + 1)]
for _ in range(M):
party = [int(x) for x in sys.stdin.readline().rstrip().split()]
party_member = party[1:]
party_list.append(party_member)
for pair in combinations(party_member,2):
if not pair[1] in graph[pair[0]]:
graph[pair[0]].append(pair[1])
if not pair[0] in graph[pair[1]]:
graph[pair[1]].append(pair[0])
κ·Έλν λ§λλ ννΈλ€.
μ¬κΈ°μλ μ¬λΌμ΄μ± ν΄μ£Όκ³ μ 체 νν° λ¦¬μ€νΈμ μΆκ°ν΄λλ€.(λμ€μ νμ)
κ·Έλ¦¬κ³ μ‘°ν© ν¨μλ₯Ό μ¬μ©ν΄μ 2λͺ μ© νμ΄λ₯Ό λ§λ€μ΄ μΈμ 리μ€νΈμ μΆκ°ν΄μ€λ€.
def bfs(n):
deq = deque()
deq.append(n)
visited[n] = True
while deq:
x = deq.popleft()
for i in graph[x]:
if not visited[i]:
who_know_truth[i] = True
visited[i] = True
deq.append(i)
for i in know_member:
visited = [False] * (N + 1)
bfs(i)
μ§μ€μ μλ μ¬λλ€μ κΈ°μ€μΌλ‘ κ·Έλν νμμ ν΄μ€λ€.
λ°©λ¬Έμ ν λλ§λ€ μ§μ€μ μλ μ¬λμΌλ‘ λ°κΏμ€λ€.(False -> True)
ans = M
for party in party_list:
for p in party:
if who_know_truth[p] == True:
ans -= 1
break
print(ans)
κ·Έλ¦¬κ³ κ° νν°λ§λ€ μ§μ€μ μλ μ¬λμ΄ ν λͺ μ΄λΌλ μ‘΄μ¬νλ©΄ break νκ³ νν° μ΄κ°μμμ -1 ν΄μ€λ€.
'𧩠Problem Solving > [λ°±μ€]' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[λ°±μ€] 13913 μ¨λ°κΌμ§ 4 (python νμ΄μ¬) (0) | 2022.07.10 |
---|---|
[λ°±μ€] 12851 μ¨λ°κΌμ§ 2 (python νμ΄μ¬) (0) | 2022.07.10 |
[λ°±μ€] 2467 μ©μ‘ (python νμ΄μ¬) (0) | 2022.07.09 |
[λ°±μ€] 6064 μΉ΄μ λ¬λ ₯ (python νμ΄μ¬) (0) | 2022.07.06 |
[λ°±μ€] 11403 κ²½λ‘μ°ΎκΈ° (python νμ΄μ¬) (0) | 2022.07.06 |