๐Ÿ™‡โ€โ™€๏ธUnion & Find

disjoinset ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•œ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด
์ง‘ํ•ฉ A : {1, 2}
์ง‘ํ•ฉ B : {2, 3}
์ด ์žˆ๋‹ค๊ณ  ํ•  ๋•Œ, 2๋ผ๋Š” ๊ณตํ†ต ์›์†Œ๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ {1, 2, 3}์œผ๋กœ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.

์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์•„๋ž˜์˜ ๋ฌธ์ œ์™€ ํ•จ๊ป˜ ์„ค๋ช…!

77. ์นœ๊ตฌ์ธ๊ฐ€? (Union&Find ์ž๋ฃŒ๊ตฌ์กฐ)  

์˜ค๋Š˜์€ ์ƒˆ ํ•™๊ธฐ ์ƒˆ๋กœ์šด ๋ฐ˜์—์„œ ์ฒ˜์Œ ์‹œ์ž‘ํ•˜๋Š” ๋‚ ์ด๋‹ค. 

ํ˜„์ˆ˜๋„ค ๋ฐ˜ ํ•™์ƒ์€ N๋ช…์ด๋‹ค. 

ํ˜„์ˆ˜๋Š” ๊ฐ ํ•™์ƒ๋“ค์˜ ์นœ๊ตฌ๊ด€๊ณ„๋ฅผ ์•Œ๊ณ  ์‹ถ๋‹ค.

๋ชจ๋“  ํ•™์ƒ์€ 1๋ถ€ํ„ฐ N๊นŒ์ง€ ๋ฒˆํ˜ธ๊ฐ€ ๋ถ€์—ฌ๋˜์–ด ์žˆ๊ณ , ํ˜„์ˆ˜์—๊ฒŒ๋Š” ๊ฐ๊ฐ ๋‘ ๋ช…์˜ ํ•™์ƒ์€ ์นœ๊ตฌ ๊ด€๊ณ„๊ฐ€ ๋ฒˆํ˜ธ๋กœ ํ‘œํ˜„๋œ ์ˆซ์ž์Œ์ด ์ฃผ์–ด์ง„๋‹ค. 

๋งŒ์•ฝ (1, 2), (2, 3), (3, 4)์˜ ์ˆซ์ž์Œ์ด ์ฃผ์–ด์ง€๋ฉด 1๋ฒˆ ํ•™์ƒ๊ณผ 2๋ฒˆ ํ•™์ƒ์ด ์นœ๊ตฌ์ด๊ณ , 2๋ฒˆ ํ•™์ƒ๊ณผ 3๋ฒˆ ํ•™์ƒ์ด ์นœ๊ตฌ, 3๋ฒˆ ํ•™์ƒ๊ณผ 4๋ฒˆ ํ•™์ƒ์ด ์นœ๊ตฌ์ด๋‹ค. 

๊ทธ๋ฆฌ๊ณ  1๋ฒˆ ํ•™์ƒ๊ณผ 4๋ฒˆ ํ•™์ƒ์€ 2๋ฒˆ๊ณผ 3๋ฒˆ์„ ํ†ตํ•ด์„œ ์นœ๊ตฌ๊ด€๊ณ„๊ฐ€ ๋œ๋‹ค.

ํ•™์ƒ์˜ ์นœ๊ตฌ๊ด€๊ณ„๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ˆซ์ž์Œ์ด ์ฃผ์–ด์ง€๋ฉด ํŠน์ • ๋‘ ๋ช…์ด ์นœ๊ตฌ์ธ์ง€๋ฅผ ํŒ๋ณ„ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”. 

๋‘ ํ•™์ƒ์ด ์นœ๊ตฌ์ด๋ฉด โ€œYES"์ด๊ณ , ์•„๋‹ˆ๋ฉด โ€NO"๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.


โ–ฃ ์ž…๋ ฅ์„ค๋ช…
์ฒซ ๋ฒˆ์งธ ์ค„์— ๋ฐ˜ ํ•™์ƒ์ˆ˜์ธ ์ž์—ฐ์ˆ˜ N(1<=N<=1,000)๊ณผ ์ˆซ์ž์Œ์˜ ๊ฐœ์ˆ˜์ธ M(1<=M<=3,000)์ด ์ฃผ์–ด์ง€๊ณ , ๋‹ค์Œ M๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ ์ˆซ์ž์Œ์ด ์ฃผ์–ด์ง„๋‹ค. 

๋งˆ์ง€๋ง‰ ์ค„์—๋Š” ๋‘ ํ•™์ƒ์ด ์นœ๊ตฌ์ธ์ง€ ํ™•์ธํ•˜๋Š” ์ˆซ์ž์Œ์ด ์ฃผ์–ด์ง„๋‹ค.

โ–ฃ ์ถœ๋ ฅ์„ค๋ช…
์ฒซ ๋ฒˆ์งธ ์ค„์— โ€œYES"๋˜๋Š” "NO"๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

โ–ฃ ์ž…๋ ฅ์˜ˆ์ œ 1 
9 7
1 2
2 3
3 4
4 5
6 7
7 8
8 9
3 8

โ–ฃ ์ถœ๋ ฅ์˜ˆ์ œ 1
NO

์œ„์˜ ๋ฌธ์ œ๋ฅผ ๋ณด๋ฉด ๋”ฑ ์„ค๋ช…์— ๋งž๋Š” ๊ฒฝ์šฐ์ด๋‹ค.

์œ„ ๋ฌธ์ œ์˜ ํ•ด๊ฒฐ ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include<iostream>
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;
int n, m, a, b;
int unf[1001];

int Find(int v)
{
	if (v == unf[v])
		return v;
	else
		return unf[v] = Find(unf[v]);
}

void Union(int a, int b)
{
	a = Find(a);
	b = Find(b);
	if (a != b) unf[a] = b;
}

void solve()
{
	cin >> n >> m;
	for (int i = 1; i <= n; ++i)
		unf[i] = i;

	for (int i = 1; i <= m; ++i)
	{
		cin >> a >> b;
		Union(a, b);
	}

	int fa, fb;
	cin >> a >> b;
	fa = Find(a);
	fb = Find(b);
	if (fa == fb)
		cout << "YES" << endl;
	else
		cout << "NO" << endl;
}

int main() 
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	freopen("input.txt", "rt", stdin);

	solve();

	return 0;
}

๋ถ„์„ํ•˜๊ธฐ!

Find

int Find(int v)
{
	if (v == unf[v])
		return v;
	else
		return Find(unf[v]);
}

์ด๋ ‡๊ฒŒ ๋กœ์ง์„ ์ง ๋‹ค๋ฉด ๊ณ„์†ํ•ด์„œ ๊ผฌ๋ฆฌ์— ๊ผฌ๋ฆฌ๋ฅผ ๋ฌด๋Š” ํ˜•์‹์œผ๋กœ Find๊ฐ€ ์ด๋ฃจ์–ด์ง„๋‹ค.

๊ทธ๋ž˜๋„ ๊ฐ™์€ ๊ทธ๋ฃน๋ผ๋ฆฌ ๋ฌถ์ธ๋‹ค.

Find

int Find(int v)
{
	if (v == unf[v])
		return v;
	else
		return unf[v] = Find(unf[v]);
}

์ด๋Ÿฐ์‹์œผ๋กœ ์งœ๊ฒŒ ๋œ๋‹ค๋ฉด ๋ฉ”๋ชจ์ด์ œ์ด์…˜์ด๋œ๋‹ค.
unf[v] = Find(unf[v]);์ด ๋ถ€๋ถ„์ด ์ตœ์ƒ๋‹จ ๋ถ€๋ถ„๊ณผ ๋ฐ”๋กœ ์—ฐ๊ฒฐํ•˜๊ฒŒ ๋งŒ๋“ ๋‹ค.

Union

void Union(int a, int b)
{
	a = Find(a);
	b = Find(b);
	if (a != b) unf[a] = b;
}

Union์€ ๊ฐ™์€ ๊ทธ๋ฃน์œผ๋กœ ๋งŒ๋“œ๋Š” ํ•จ์ˆ˜์ด๋‹ค. b๊ฐ€ ๋จธ๋ฆฌ a๊ฐ€ ๊ผฌ๋ฆฌ๋ฅผ ๋งก๊ฒŒ๋˜๋Š” ํ•จ์ˆ˜.

๋ฌธ์ œ ํ•ด๊ฒฐ ํ•จ์ˆ˜ ๋ถ„์„!

void solve()
{
	cin >> n >> m;
	for (int i = 1; i <= n; ++i)
		unf[i] = i; // ์ž๊ธฐ ์ž์‹ ์ด ํ•จ์ˆ˜์ด๋‹ค.

	for (int i = 1; i <= m; ++i)
	{
		cin >> a >> b;
		Union(a, b); // b๊ฐ€ ๋จธ๋ฆฌ๊ฐ€๋˜๊ณ  a๊ฐ€ ๊ผฌ๋ฆฌ๊ฐ€๋˜๋ฉฐ ํ•จ์ˆ˜ ํ•ฉ์น˜๊ธฐ
	}

	int fa, fb;
	cin >> a >> b;
	fa = Find(a);
	fb = Find(b);
	if (fa == fb)
		cout << "YES" << endl;
	else
		cout << "NO" << endl;
}

ํƒœ๊ทธ:

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

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