HelloMinchan
처음처럼
HelloMinchan
LinkedIn
전체 방문자
오늘
어제
  • 분류 전체보기 (306)
    • Backend (4)
      • NestJS (1)
      • Express (1)
      • Spring (2)
    • Infrastructure (1)
      • AWS (1)
    • Frontend (1)
      • Next.js (1)
    • Language & Runtime (4)
      • Java (2)
      • Node.js (2)
    • Computer Science (8)
      • Computer Networks (3)
      • Operating Systems (4)
      • OOP (1)
    • 독서 (4)
      • 데이터 중심 애플리케이션 설계 (3)
      • 객체지향의 사실과 오해 (1)
    • 회고 (4)
      • Project (2)
      • Career (2)
    • Deprecated (280)

채널

  • GitHub
  • LinkedIn

최근 글

태그

  • 알고스팟Python
  • front-end
  • 데이터베이스
  • 백엔드
  • 프로그래머스C++
  • 프로그래머스Python
  • 알고스팟
  • programmers
  • back-end
  • Algospot
  • Database
  • 백준Go
  • 프로그래머스
  • 백준C++
  • 코딩
  • 프로그래밍
  • Baekjoon Online Judge
  • 백준Python
  • 개발자
  • 백준

최근 댓글

인기 글

hELLO
HelloMinchan

처음처럼

[Baekjoon Online Judge] 백준 1717번 집합의 표현(Python)
Deprecated

[Baekjoon Online Judge] 백준 1717번 집합의 표현(Python)

2020. 5. 16. 19:21

© 2020 All Rights Reserved. 주식회사 스타트링크

[Baekjoon Online Judge] 백준 1717번 집합의 표현

(Python)

(글쓴날 : 2020.05.16)

 


* Baekjoon Online Judge, 백준 1717번 문제 Python 언어 풀이입니다.

* 소스 코드의 저작권은 글쓴이에게 있습니다.


 

 

백준 1717번 집합의 표현


1) 문제

문제 링크 : https://www.acmicpc.net/problem/1717

 

1717번: 집합의 표현

첫째 줄에 n(1≤n≤1,000,000), m(1≤m≤100,000)이 주어진다. m은 입력으로 주어지는 연산의 개수이다. 다음 m개의 줄에는 각각의 연산이 주어진다. 합집합은 0 a b의 형태로 입력이 주어진다. 이는 a가 ��

www.acmicpc.net


2) 풀이 과정

* 시간 복잡도 : O(α(n), α : 아커만 함수)

 

0부터 n까지 각각 n+1개의 집합을 이루고 있을 때, 합집합 연산과 두 원소가 같은 집합에 포함되어 있는지 확인하는 연산을 수행하는 문제입니다.

 

저의 경우, 유니온 파인드를 적용하였으며, Python을 사용했습니다.

유니온 파인드 그 자체를 구현하는 기본 문제여서, 별다른 풀이 과정 없이 유니온 파인드를 구현해 문제를 해결했습니다.


3) 코드

 

* Python 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import sys
input = sys.stdin.readline
 
 
def find(target):
    if target == parent[target]:
        return target
 
    parent[target] = find(parent[target])
    return parent[target]
 
 
def union(a, b):
    a = find(a)
    b = find(b)
 
    if a < b:
        parent[b] = a
    else:
        parent[a] = b
 
 
n, m = map(int, input().split())
parent = [ i for i in range(n + 1)]
 
for _ in range(m):
    oper, a, b = map(int, input().split())
 
    if oper:
        findA = find(a)
        findB = find(b)
 
        if findA == findB:
            print("YES")
        else:
            print("NO")
    else:
        union(a, b)

 

저작자표시 비영리 변경금지 (새창열림)

'Deprecated' 카테고리의 다른 글

[Algorithm] 유니온 파인드  (0) 2020.05.16
[Baekjoon Online Judge] 백준 1976번 여행 가자(Python)  (0) 2020.05.16
[Algorithm] 슬라이딩 윈도우  (0) 2020.05.15
[Baekjoon Online Judge] 백준 11003번 최솟값 찾기(Python)  (0) 2020.05.15
[Baekjoon Online Judge] 백준 6198번 옥상 정원 꾸미기(Python)  (0) 2020.05.13
    'Deprecated' 카테고리의 다른 글
    • [Algorithm] 유니온 파인드
    • [Baekjoon Online Judge] 백준 1976번 여행 가자(Python)
    • [Algorithm] 슬라이딩 윈도우
    • [Baekjoon Online Judge] 백준 11003번 최솟값 찾기(Python)
    HelloMinchan
    HelloMinchan
    Though you should not fear failure, You should do your very best to avoid it.

    티스토리툴바