Skip to content

[Algorithm] 집합의 표현 #214

Description

@hwangJi-dev

💬 문제

https://www.acmicpc.net/problem/1717


💬 Idea

  • union-find를 이용하여 문제를 풀이했다. (공통 조상 찾기와 유사)

💬 풀이

import Foundation
func solution1717(){letnm=readLine()!.components(separatedBy:.whitespaces).map({Int($0)! })varparent=[Int](0...nm[0])func find(_ b:Int)->Int{if b !=parent[b]{parent[b]=find(parent[b])}returnparent[b]}func union(_ a:Int, _ b:Int){leta=find(a)letb=find(b)if a >= b {parent[a]= b
}else{parent[b]= a
}}for_in1...nm[1]{letcal=readLine()!.components(separatedBy:.whitespaces).map({Int($0)! })leta=cal[1]letb=cal[2]ifcal[0]==0{union(a, b)}else{iffind(a)==find(b){print("YES")}else{print("NO")}}}}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions