2025. 10. 10. 15:27 Baekjoon(Python)/단계별로 풀어보기(Python)

Baekjoon(Python)/단계별로 풀어보기(Python)

[백준][18단계 심화 2] 26069번 /붙임성 좋은 총총이 (파이썬/Python)

junslee 2025. 10. 10. 15:27

1. 문제 설명

2. 코드

import sys
input = sys.stdin.readline

N = int(input().strip())
dance = {'ChongChong'}
for _ in range(N):
    a,b = input().split()
    if a in dance or b in dance:
        dance.add(a)
        dance.add(b)
print(len(dance))     

3. 풀이 과정

집합을 set으로 해서 'ChongChong'을 만났을 때, 

입력받은 a,b를 전부 집합에 추가한 후, 중복을 제거된 값들의 개수를 len()으로 출력하면된다.

4. 자투리 개념

  • input()은 기본형으로 문자열로 받기 때문에 따로 이름을 입력받을 때는 따로 형변환을 하지 않아도 된다.
  • set과 dict
    {} -> dict
    {"a"} -> set
    {"a":1,"b":2} -> dict
반응형