[BOJ][๐ŸŸก4][๋ฐฑ์ค€#01717] ์ง‘ํ•ฉ์˜ ํ‘œํ˜„

์ž‘์„ฑ:    

์—…๋ฐ์ดํŠธ:

์นดํ…Œ๊ณ ๋ฆฌ:

ํƒœ๊ทธ: , , ,

๋ฌธ์ œ ์ถœ์ฒ˜

BAEKJOON Online Judge #1717


๋ฌธ์ œ

์ดˆ๊ธฐ์— {0}, {1}, {2}, โ€ฆ {n} ์ด ๊ฐ๊ฐ n+1๊ฐœ์˜ ์ง‘ํ•ฉ์„ ์ด๋ฃจ๊ณ  ์žˆ๋‹ค. ์—ฌ๊ธฐ์— ํ•ฉ์ง‘ํ•ฉ ์—ฐ์‚ฐ๊ณผ, ๋‘ ์›์†Œ๊ฐ€ ๊ฐ™์€ ์ง‘ํ•ฉ์— ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€๋ฅผ ํ™•์ธํ•˜๋Š” ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ์ง‘ํ•ฉ์„ ํ‘œํ˜„ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.


์ž…๋ ฅ

์ฒซ์งธ ์ค„์— n(1 โ‰ค n โ‰ค 1,000,000), m(1 โ‰ค m โ‰ค 100,000)์ด ์ฃผ์–ด์ง„๋‹ค. m์€ ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง€๋Š” ์—ฐ์‚ฐ์˜ ๊ฐœ์ˆ˜์ด๋‹ค. ๋‹ค์Œ m๊ฐœ์˜ ์ค„์—๋Š” ๊ฐ๊ฐ์˜ ์—ฐ์‚ฐ์ด ์ฃผ์–ด์ง„๋‹ค. ํ•ฉ์ง‘ํ•ฉ์€ 0 a b์˜ ํ˜•ํƒœ๋กœ ์ž…๋ ฅ์ด ์ฃผ์–ด์ง„๋‹ค. ์ด๋Š” a๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋Š” ์ง‘ํ•ฉ๊ณผ, b๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋Š” ์ง‘ํ•ฉ์„ ํ•ฉ์นœ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค. ๋‘ ์›์†Œ๊ฐ€ ๊ฐ™์€ ์ง‘ํ•ฉ์— ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€๋ฅผ ํ™•์ธํ•˜๋Š” ์—ฐ์‚ฐ์€ 1 a b์˜ ํ˜•ํƒœ๋กœ ์ž…๋ ฅ์ด ์ฃผ์–ด์ง„๋‹ค. ์ด๋Š” a์™€ b๊ฐ€ ๊ฐ™์€ ์ง‘ํ•ฉ์— ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€๋ฅผ ํ™•์ธํ•˜๋Š” ์—ฐ์‚ฐ์ด๋‹ค. a์™€ b๋Š” n ์ดํ•˜์˜ ์ž์—ฐ์ˆ˜ ๋˜๋Š” 0์ด๋ฉฐ ๊ฐ™์„ ์ˆ˜๋„ ์žˆ๋‹ค.


์ถœ๋ ฅ

1๋กœ ์‹œ์ž‘ํ•˜๋Š” ์ž…๋ ฅ์— ๋Œ€ํ•ด์„œ ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ YES/NO๋กœ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. (yes/no ๋ฅผ ์ถœ๋ ฅํ•ด๋„ ๋œ๋‹ค)


์˜ˆ์ œ

์ž…๋ ฅ

7 8
0 1 3
1 1 7
0 7 6
1 7 1
0 3 7
0 4 2
0 1 1
1 1 1


์ถœ๋ ฅ

NO
NO
YES


My Sol

import sys
input = sys.stdin.readline
sys.setrecursionlimit(100000)

def find_set(x):
    if x!=p[x]:
        p[x] = find_set(p[x])

    return p[x]

n, m = map(int, input().split())
p = [i for i in range(n+1)]
for _ in range(m):
    f, u, v = map(int, input().split())
    a, b = find_set(u), find_set(v)

    if f:
        print('YES' if a == b else 'NO')
        continue

    if a != b:
        p[a] = b

์œ ๋‹ˆ์˜จ ํŒŒ์ธ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ํ•ด๊ฒฐํ•˜๋Š” ๋ฌธ์ œ์˜€๋‹ค. ํ…œํ”Œ๋ฆฟ์„ ์‚ฌ์šฉํ•˜๊ณ  ์ผ๋ถ€ ๋ณ€ํ˜•ํ•˜๋Š”๋ฐ ์–ด๋ ต์ง€ ์•Š๊ฒŒ ํ’€ ์ˆ˜ ์žˆ์—ˆ๋‹ค.


๊ฒฐ๊ณผ

๋งž์•˜์Šต๋‹ˆ๋‹ค!!


๋ชจ๋ฒ”๋‹ต์•ˆ

์ถœ์ฒ˜

# empty

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ