📙
python-algorithm
  • 🖋️알고리즘 풀이 저장소
  • 이론
    • BFS & DFS 이론
    • 다익스트라 이론
    • 최소신장트리(크루스칼) 이론
    • 백트래킹 이론
  • 유형
    • 정렬
    • 순열과 조합
    • 탐색
    • 이분 탐색
    • SHA-256
    • 투 포인터
    • 피보나치
    • Z 재귀함수
    • 재귀함수
    • 친구 네트워크
    • 찾기
    • 큐
    • 스택 수열
    • 기하학
    • 트리 순회
    • 고급 탐색
    • BFS & DFS 알고리즘
    • 다익스트라 알고리즘
    • 최소신장트리(크루스칼) 알고리즘
    • 동적 프로그래밍
    • 그리디 알고리즘
    • 백트래킹 알고리즘
  • 기타
    • 베스트셀러
    • 성
    • 키 로거
    • 음계
Powered by GitBook
On this page
  • 백준 4195번 : 친구 네트워크
  • 1. 풀이
  1. 유형

친구 네트워크

#자료 구조 #분리 집합 #해시를 사용한 집합과 맵

백준 4195번 : 친구 네트워크

1. 풀이

def find(x):
  if x == parent[x]:
    return x
  else:
    p = find(parent[x])
    parent[x] = p
    return parent[x]

def union(x, y):
  x = find(x)
  y = find(y)
  if x != y:
    parent[y] = x
    number[x] += number[y]

test_case = int(input())
for _ in range(test_case):
  parent = dict()
  number = dict()
  f = int(input())
  for _ in range(f):
      x, y = input().split(' ')
      if x not in parent:
        parent[x] = x
        number[x] = 1
      if y not in parent:
        parent[y] = y
        number[y] = 1
      union(x, y)
      print(number[find(x)])
Previous재귀함수Next찾기

Last updated 3 years ago