-
[1717] 집합의 표현BOJ 2021. 9. 1. 21:22
https://www.acmicpc.net/problem/1717
1717번: 집합의 표현
첫째 줄에 n(1 ≤ n ≤ 1,000,000), m(1 ≤ m ≤ 100,000)이 주어진다. m은 입력으로 주어지는 연산의 개수이다. 다음 m개의 줄에는 각각의 연산이 주어진다. 합집합은 0 a b의 형태로 입력이 주어진다. 이는
www.acmicpc.net
<문제>
유니온파인드 문제이다.
<소스코드>
1234567891011121314151617181920212223242526272829303132#include<stdio.h>int n, m;int a[1000001];void _union(int x, int y);int _find(int x);void _union(int x, int y) {int px, py;px = _find(x);py = _find(y);if (px > py)a[px] = py;else if (px < py)a[py] = px;}int _find(int x) {if (x == a[x])return x;return a[x] = _find(a[x]);}int main(void) {int i;scanf("%d %d", &n, &m);for (i = 1; i <= n; i++)a[i] = i;while (m--) {int a, b, c;scanf("%d %d %d", &a, &b, &c);if (a == 0)_union(b, c);else {if (_find(b) == _find(c))printf("YES\n");else printf("NO\n");}}return 0;}cs 'BOJ' 카테고리의 다른 글
[10942] 팰린드롬? (0) 2021.09.01 [11053] 가장 긴 증가하는 부분 수열 (0) 2021.09.01 [10819] 차이를 최대로 (0) 2021.09.01 [2174] 로봇 시뮬레이션 (0) 2021.09.01 [13913] 숨바꼭질 4 (0) 2021.09.01