<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Union-Find Archives - 어제와 내일의 나 그 사이의 이야기</title>
	<atom:link href="https://lycos7560.com/tag/union-find/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>생각의 흐름을 타고 다니며 만드는 블로그</description>
	<lastBuildDate>Fri, 14 Jul 2023 22:06:49 +0000</lastBuildDate>
	<language>ko-KR</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0</generator>

<image>
	<url>https://lycos7560.com/wp-content/uploads/2022/11/cropped-cropped-cropped-log-1-150x150-1-80x80.png</url>
	<title>Union-Find Archives - 어제와 내일의 나 그 사이의 이야기</title>
	<link></link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>백준 17352번 (여러분의 다리가 되어 드리겠습니다!, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-17352%eb%b2%88-%ec%97%ac%eb%9f%ac%eb%b6%84%ec%9d%98-%eb%8b%a4%eb%a6%ac%ea%b0%80-%eb%90%98%ec%96%b4-%eb%93%9c%eb%a6%ac%ea%b2%a0%ec%8a%b5%eb%8b%88%eb%8b%a4-c-baekjoon/33762/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-17352%eb%b2%88-%ec%97%ac%eb%9f%ac%eb%b6%84%ec%9d%98-%eb%8b%a4%eb%a6%ac%ea%b0%80-%eb%90%98%ec%96%b4-%eb%93%9c%eb%a6%ac%ea%b2%a0%ec%8a%b5%eb%8b%88%eb%8b%a4-c-baekjoon/33762/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Mon, 06 Mar 2023 12:32:40 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[17352]]></category>
		<category><![CDATA[17352번]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[Union-Find]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[그래프 이론]]></category>
		<category><![CDATA[그래프 탐색]]></category>
		<category><![CDATA[기본]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 17352]]></category>
		<category><![CDATA[백준 17352번]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[여러분의 다리가 되어 드리겠습니다!]]></category>
		<category><![CDATA[유니온 파인드]]></category>
		<category><![CDATA[자료구조]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<category><![CDATA[회의준비]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=33762</guid>

					<description><![CDATA[<p>백준 17352번 '여러분의 다리가 되어 드리겠습니다!' 문제에 대한 글입니다. union find 알고리즘을 이용하여 해결하였습니다. (This is Baekjoon 17352 about the question, "We will be your legs!" The solution was solved using the union find algorithm.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-17352%eb%b2%88-%ec%97%ac%eb%9f%ac%eb%b6%84%ec%9d%98-%eb%8b%a4%eb%a6%ac%ea%b0%80-%eb%90%98%ec%96%b4-%eb%93%9c%eb%a6%ac%ea%b2%a0%ec%8a%b5%eb%8b%88%eb%8b%a4-c-baekjoon/33762/">백준 17352번 (여러분의 다리가 되어 드리겠습니다!, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<!-- HorizontalAD -->
<ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-5698326622209671" data-ad-slot="6908948342" data-ad-format="auto" data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<div style="height:62px" aria-hidden="true" class="wp-block-spacer"></div>


				<div class="wp-block-uagb-table-of-contents uagb-toc__align-left uagb-toc__columns-1  uagb-block-f324320a      "
					data-scroll= "1"
					data-offset= "30"
					style=""
				>
				<div class="uagb-toc__wrap">
						<div class="uagb-toc__title">
							목차 테이블						</div>
																						<div class="uagb-toc__list-wrap ">
						<ol class="uagb-toc__list"><li class="uagb-toc__list"><a href="#여러분의-다리가-되어-드리겠습니다" class="uagb-toc-link__trigger">여러분의 다리가 되어 드리겠습니다!</a><li class="uagb-toc__list"><a href="#접근-방법" class="uagb-toc-link__trigger">접근 방법</a><li class="uagb-toc__list"><a href="#통과된-코드" class="uagb-toc-link__trigger">통과된 코드</a></ol>					</div>
									</div>
				</div>
			


<div style="height:62px" aria-hidden="true" class="wp-block-spacer"></div>



<h1 class="wp-block-heading">여러분의 다리가 되어 드리겠습니다!</h1>



<p class="has-medium-font-size wp-block-paragraph"><a href="https://www.acmicpc.net/problem/17352" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/17352</a></p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure id="problem-info" class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th class="has-text-align-left" data-align="left">시간 제한</th><th class="has-text-align-left" data-align="left">메모리 제한</th><th class="has-text-align-left" data-align="left">제출</th><th class="has-text-align-left" data-align="left">정답</th><th class="has-text-align-left" data-align="left">맞힌 사람</th><th class="has-text-align-left" data-align="left">정답 비율</th></tr></thead><tbody><tr><td class="has-text-align-left" data-align="left">1 초</td><td class="has-text-align-left" data-align="left">512 MB</td><td class="has-text-align-left" data-align="left">2632</td><td class="has-text-align-left" data-align="left">1332</td><td class="has-text-align-left" data-align="left">1041</td><td class="has-text-align-left" data-align="left">51.791%</td></tr></tbody></table></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">문제</h2>



<p class="has-medium-font-size wp-block-paragraph">선린월드에는&nbsp;<em>N</em>개의 섬이 있다. 섬에는 1, 2, &#8230;,&nbsp;<em>N</em>의 번호가 하나씩 붙어 있다.&nbsp;</p>



<p class="has-medium-font-size wp-block-paragraph">그 섬들을&nbsp;<em>N&nbsp;</em>&#8211; 1개의 다리가 잇고 있으며, 어떤 두 섬 사이든 다리로 왕복할 수 있다.</p>



<p class="has-medium-font-size wp-block-paragraph"><strong>어제까지는 그랬다.</strong></p>



<p class="has-medium-font-size wp-block-paragraph">&#8220;왜 다리가&nbsp;<em>N&nbsp;</em>&#8211; 1개밖에 없냐, 통행하기 불편하다&#8221;며 선린월드에 불만을 갖던 욱제가 다리 하나를 무너뜨렸다! </p>



<p class="has-medium-font-size wp-block-paragraph">안 그래도 불편한 통행이 더 불편해졌다. 서로 왕복할 수 없는 섬들이 생겼기 때문이다. </p>



<p class="has-medium-font-size wp-block-paragraph">일단 급한 대로 정부는 선린월드의 건축가를 고용해, </p>



<p class="has-medium-font-size wp-block-paragraph">서로 다른 두 섬을 다리로 이어서 다시 어떤 두 섬 사이든 왕복할 수 있게 하라는 지시를 내렸다.</p>



<p class="has-medium-font-size wp-block-paragraph">그런데 그 건축가가 당신이다! 안 그래도 천하제일 코딩대회에 참가하느라 바쁜데&#8230;</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">입력</h2>



<p class="has-medium-font-size wp-block-paragraph">첫 줄에 정수&nbsp;<em>N</em>이 주어진다. (2&nbsp;≤&nbsp;<em>N</em>&nbsp;≤ 300,000)</p>



<p class="has-medium-font-size wp-block-paragraph">그 다음&nbsp;<em>N</em>&nbsp;&#8211; 2개의 줄에는 욱제가 무너뜨리지 않은 다리들이 잇는 두 섬의 번호가 주어진다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">출력</h2>



<p class="has-medium-font-size wp-block-paragraph">다리로 이을 두 섬의 번호를 출력한다. </p>



<p class="has-medium-font-size wp-block-paragraph">여러 가지 방법이 있을 경우 그 중 아무거나&nbsp;한 방법만 출력한다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 1</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">4
1 2
1 3</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 1</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">1 4</pre>



<div style="height:15px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size wp-block-paragraph">&#8220;4 1&#8243;이나 &#8220;2 4&#8221;, &#8220;4 3&#8221; 등 가능한 정답은 많이 있지만, 아무거나 하나만 출력해야 한다.</p>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 2</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">2</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 2</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">1 2</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 3</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">5
1 2
2 3
4 5</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 3</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">3 4</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">출처</h2>



<p class="wp-block-paragraph"><a href="https://www.acmicpc.net/category/97" target="_blank" rel="noreferrer noopener">High School</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/394" target="_blank" rel="noreferrer noopener">선린인터넷고등학교</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/detail/2052" target="_blank" rel="noreferrer noopener">제3회 천하제일 코딩대회</a>&nbsp;D번</p>



<ul class="wp-block-list">
<li>문제를 만든 사람:&nbsp;<a href="https://www.acmicpc.net/user/jh05013" target="_blank" rel="noreferrer noopener">jh05013</a></li>
</ul>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">알고리즘 분류</h2>



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/175" target="_blank" rel="noreferrer noopener">자료 구조</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/7" target="_blank" rel="noreferrer noopener">그래프 이론</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/11" target="_blank" rel="noreferrer noopener">그래프 탐색</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/81" target="_blank" rel="noreferrer noopener">분리 집합</a></li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:var(--wp--preset--spacing--80);margin-bottom:var(--wp--preset--spacing--80)"/>



<h1 class="wp-block-heading">접근 방법</h1>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img fetchpriority="high" decoding="async" width="1600" height="1244" src="https://lycos7560.com/wp-content/uploads/2023/03/Notes_230306_212602.jpg" alt="" class="wp-image-33769" srcset="https://lycos7560.com/wp-content/uploads/2023/03/Notes_230306_212602.jpg 1600w, https://lycos7560.com/wp-content/uploads/2023/03/Notes_230306_212602-300x233.jpg 300w, https://lycos7560.com/wp-content/uploads/2023/03/Notes_230306_212602-768x597.jpg 768w, https://lycos7560.com/wp-content/uploads/2023/03/Notes_230306_212602-1536x1194.jpg 1536w" sizes="(max-width: 1600px) 100vw, 1600px" /></figure>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<h1 class="wp-block-heading">통과된 코드</h1>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<div style="height:0px" aria-hidden="true" class="wp-block-spacer"></div>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>

using namespace std;

constexpr int MAX = 300001;

int N, v, u;

int parent[MAX];

void MakeSet(int n)
{
	for (int i = 1; i &lt;= n; i++)  parent[i] = i;
}

int Find(int x) {

	if (parent[x] != x) parent[x] = Find(parent[x]); // Path compression

	return parent[x];
}

void Unite(int x, int y) {
	int rootX = Find(x);
	int rootY = Find(y);
	parent[rootY] = rootX; // Make rootX the parent of rootY
}

int main()
{
	ios_base::sync_with_stdio(false); // scanf와 동기화를 비활성화
	// cin.tie(null); 코드는 cin과 cout의 묶음을 풀어줍니다.
	cin.tie(NULL);
	cout.tie(NULL);

	cin >> N;
	MakeSet(N);
	for (int i = 0; i &lt; N - 2; i++) {
		cin >> v >> u;
		Unite(v, u);
	}

	int first, second = -1;
	first = Find(1);
	for (int i = 2; i &lt;= N; i++) {
		if (Find(i) != first) {
			second = Find(i);
			break;
		}
	}

	cout &lt;&lt; first &lt;&lt; " " &lt;&lt; second;

	return 0;
}</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="1031" height="185" src="https://lycos7560.com/wp-content/uploads/2023/03/image-24.png" alt="" class="wp-image-33765" srcset="https://lycos7560.com/wp-content/uploads/2023/03/image-24.png 1031w, https://lycos7560.com/wp-content/uploads/2023/03/image-24-300x54.png 300w, https://lycos7560.com/wp-content/uploads/2023/03/image-24-768x138.png 768w" sizes="(max-width: 1031px) 100vw, 1031px" /></figure>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5698326622209671" data-ad-slot="4245812909"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-17352%eb%b2%88-%ec%97%ac%eb%9f%ac%eb%b6%84%ec%9d%98-%eb%8b%a4%eb%a6%ac%ea%b0%80-%eb%90%98%ec%96%b4-%eb%93%9c%eb%a6%ac%ea%b2%a0%ec%8a%b5%eb%8b%88%eb%8b%a4-c-baekjoon/33762/">백준 17352번 (여러분의 다리가 되어 드리겠습니다!, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-17352%eb%b2%88-%ec%97%ac%eb%9f%ac%eb%b6%84%ec%9d%98-%eb%8b%a4%eb%a6%ac%ea%b0%80-%eb%90%98%ec%96%b4-%eb%93%9c%eb%a6%ac%ea%b2%a0%ec%8a%b5%eb%8b%88%eb%8b%a4-c-baekjoon/33762/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>알고리즘 – 유니온 파인드(Union find) &#038; 서로소 집합(Disjoint Set)</title>
		<link>https://lycos7560.com/etc/%ec%95%8c%ea%b3%a0%eb%a6%ac%ec%a6%98-%ec%9c%a0%eb%8b%88%ec%98%a8-%ed%8c%8c%ec%9d%b8%eb%93%9cunion-find-%ec%84%9c%eb%a1%9c%ec%86%8c-%ec%a7%91%ed%95%a9disjoint-set/33666/</link>
					<comments>https://lycos7560.com/etc/%ec%95%8c%ea%b3%a0%eb%a6%ac%ec%a6%98-%ec%9c%a0%eb%8b%88%ec%98%a8-%ed%8c%8c%ec%9d%b8%eb%93%9cunion-find-%ec%84%9c%eb%a1%9c%ec%86%8c-%ec%a7%91%ed%95%a9disjoint-set/33666/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Sun, 05 Mar 2023 10:21:46 +0000</pubDate>
				<category><![CDATA[기타]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[Disjoint Set]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[Union-Find]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[기본]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[서로소 집합]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[유니온 파인드]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=33666</guid>

					<description><![CDATA[<p>유니온 파인드(Union find) &#038; 서로소 집합(Disjoint Set) 알고리즘에 대한 글입니다. (This article is about the Union find &#038; Disjoint Set algorithm.)</p>
<p>The post <a href="https://lycos7560.com/etc/%ec%95%8c%ea%b3%a0%eb%a6%ac%ec%a6%98-%ec%9c%a0%eb%8b%88%ec%98%a8-%ed%8c%8c%ec%9d%b8%eb%93%9cunion-find-%ec%84%9c%eb%a1%9c%ec%86%8c-%ec%a7%91%ed%95%a9disjoint-set/33666/">알고리즘 – 유니온 파인드(Union find) &#038; 서로소 집합(Disjoint Set)</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<!-- HorizontalAD -->
<ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-5698326622209671" data-ad-slot="6908948342" data-ad-format="auto" data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>


				<div class="wp-block-uagb-table-of-contents uagb-toc__align-left uagb-toc__columns-1  uagb-block-f818938d      "
					data-scroll= "1"
					data-offset= "30"
					style=""
				>
				<div class="uagb-toc__wrap">
						<div class="uagb-toc__title">
							목차 테이블						</div>
																						<div class="uagb-toc__list-wrap ">
						<ol class="uagb-toc__list"><li class="uagb-toc__list"><a href="#유니온-파인드union-find-서로소-집합disjoint-set" class="uagb-toc-link__trigger">유니온 파인드(Union find) &amp; 서로소 집합(Disjoint Set)?</a><li class="uagb-toc__list"><a href="#예제-코드-c" class="uagb-toc-link__trigger">예제 코드 C++</a><li class="uagb-toc__list"><a href="#백준-union-find-algorithm-문제" class="uagb-toc-link__trigger">백준 Union find Algorithm 문제</a></ol>					</div>
									</div>
				</div>
			


<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<h1 class="wp-block-heading has-large-font-size">유니온 파인드(Union find) &amp; 서로소 집합(Disjoint Set)?</h1>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide"/>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size wp-block-paragraph">Union find는 대표적인 그래프 알고리즘으로</p>



<p class="has-medium-font-size wp-block-paragraph"><strong>집합 내의 요소의 연결성을 효율적으로 추적하는 데 사용되는 데이터 구조 및 알고리즘</strong>입니다.</p>



<p class="has-medium-font-size wp-block-paragraph">역으로 서로 연결되어 있지 않은 노드를 판별할 수도 있기 때문에&nbsp;<strong>서로소 집합 (Disjoint-set)</strong>이라고도 합니다.</p>



<p class="has-medium-font-size wp-block-paragraph">알고리즘은 union과 find의 두 가지 연산으로 구성되어 있습니다.</p>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size wp-block-paragraph">-&gt; union 은 각 노드가 속한 집합을 1개로 합치는 연산</p>



<p class="has-medium-font-size wp-block-paragraph">-&gt; find  특정 노드 A에 관해 A가 속한 대표 노드를 반환하는 연산</p>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size wp-block-paragraph">Union-Find 알고리즘은 집합 내 요소의 연결성을 추적하는 데 유용한 데이터 구조 및 알고리즘입니다. </p>



<p class="has-medium-font-size wp-block-paragraph">이것은 많은 그래프 알고리즘에서 사용될 수 있으며 경로 압축 최적화로 O(log n) ~ O(n)의 시간 복잡도를 가집니다.</p>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="wp-block-paragraph"></p>



<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="알고리즘 코딩테스트 핵심이론 강의 - 유니온파인드" width="1778" height="1000" src="https://www.youtube.com/embed/klBh4ZglHYo?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>
</div></figure>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<h1 class="wp-block-heading has-large-font-size">예제 코드 C++</h1>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide"/>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>
#include &lt;vector>

using namespace std;

// Initialize the parent array
void makeSet(int parent[], int n) {
    for (int i = 0; i &lt; n; i++) {
        parent[i] = i;
    }
}

// Find the root of the set containing node x
int find(int parent[], int x) {
    if (parent[x] != x) {
        parent[x] = find(parent, parent[x]); // Path compression
    }
    return parent[x];
}

// Merge the sets containing nodes x and y
void unite(int parent[], int x, int y) {
    int rootX = find(parent, x);
    int rootY = find(parent, y);
    parent[rootY] = rootX; // Make rootX the parent of rootY
}

int n = 5; // Number of nodes
int parent[5]; // Number of nodes

int main() {


    makeSet(parent, n); // Initialize the parent array

    // Merge sets containing nodes 2 and 4
    unite(parent, 2, 4);

    // Merge sets containing nodes 1 and 5
    unite(parent, 1, 5);

    // Merge sets containing nodes 1 and 2
    unite(parent, 1, 2);

    // Find the set of node 4
    int root4 = find(parent, 4);

    // Print the parent array
    for (int i = 0; i &lt; n; i++) {
        cout &lt;&lt; parent[i] &lt;&lt; " ";
    }
    cout &lt;&lt; endl;

    // Print the root of the set containing node 4
    cout &lt;&lt; "The root of the set containing node 4 is " &lt;&lt; root4 &lt;&lt; endl;

    return 0;
}
</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full is-resized"><img decoding="async" src="https://lycos7560.com/wp-content/uploads/2023/03/image-22.png" alt="" class="wp-image-33750" width="1292" height="129" srcset="https://lycos7560.com/wp-content/uploads/2023/03/image-22.png 771w, https://lycos7560.com/wp-content/uploads/2023/03/image-22-300x30.png 300w, https://lycos7560.com/wp-content/uploads/2023/03/image-22-768x77.png 768w" sizes="(max-width: 1292px) 100vw, 1292px" /></figure>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size wp-block-paragraph">백준 예제</p>



<p class="has-medium-font-size wp-block-paragraph"><a href="https://www.acmicpc.net/problem/1717" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/1717</a></p>



<div style="height:38px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full is-resized"><img decoding="async" src="https://lycos7560.com/wp-content/uploads/2023/03/image-21.png" alt="" class="wp-image-33737" width="1240" height="1226" srcset="https://lycos7560.com/wp-content/uploads/2023/03/image-21.png 1050w, https://lycos7560.com/wp-content/uploads/2023/03/image-21-300x297.png 300w, https://lycos7560.com/wp-content/uploads/2023/03/image-21-768x759.png 768w" sizes="(max-width: 1240px) 100vw, 1240px" /></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>

using namespace std;

constexpr int Max = 1000001;

// 유니온 root노드 배열
int uArr[Max];

int N, M, u, b, c;

//부모를 찾는 함수
//모든 경로가 부모를 가르키게 함
//상수 시간의 복잡도
int Find(int x)
{
    if (uArr[x] == x) return x;
    return uArr[x] = Find(uArr[x]);
}

//두 노드를 연결 시키는 것
//기준을 정해서 연결시키는 것이 헷갈리지 않음
//작은쪽이 부모 or 큰쪽이 부모
void Union(int x, int y)
{
    x = Find(x);
    y = Find(y);

    if (x != y) {
        if (x &lt; y) uArr[y] = x;
        else uArr[x] = y;
    }
}

int main()
{
    ios_base::sync_with_stdio(false); // scanf와 동기화를 비활성화
    // cin.tie(null); 코드는 cin과 cout의 묶음을 풀어줍니다.
    cin.tie(NULL);
    cout.tie(NULL);

    cin >> N >> M;
    // 유니온 배열 초기화
    for (int i = 0; i &lt;= N; i++) uArr[i] = i;

    for (int i = 0; i &lt; M; i++) {

        cin >> u >> b >> c;

        if (u == 0) { // 합집합 연산
            Union(b, c);
        }
        else { // 질의
            if (Find(b) != Find(c)) cout &lt;&lt; "NO\n";
            else cout &lt;&lt; "YES\n";
        }
    }

    return 0;
}</pre>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<h1 class="wp-block-heading">백준 Union find Algorithm 문제</h1>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide"/>



<p class="has-medium-font-size wp-block-paragraph"><a href="https://www.acmicpc.net/problemset?sort=ac_desc&amp;algo=81" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problemset?sort=ac_desc&amp;algo=81</a></p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



			<div class="wp-block-uagb-post-carousel uagb-post-grid  uagb-post__image-position-top uagb-post__image-enabled uagb-block-bfa50e23     uagb-post__arrow-outside uagb-post__items uagb-post__columns-5 is-carousel uagb-post__columns-tablet-2 uagb-post__columns-mobile-1" data-total="1" style="">

												<article class="uagb-post__inner-wrap">								<div class='uagb-post__image'>
									<a href="https://lycos7560.com/etc/%ec%95%8c%ea%b3%a0%eb%a6%ac%ec%a6%98-%ec%9c%a0%eb%8b%88%ec%98%a8-%ed%8c%8c%ec%9d%b8%eb%93%9cunion-find-%ec%84%9c%eb%a1%9c%ec%86%8c-%ec%a7%91%ed%95%a9disjoint-set/33666/" target="_self" rel="bookmark noopener noreferrer"><img decoding="async" width="1024" height="640" src="https://lycos7560.com/wp-content/uploads/algorithm-wallpaper.jpg" class="attachment-large size-large" alt="" srcset="https://lycos7560.com/wp-content/uploads/algorithm-wallpaper.jpg 1024w, https://lycos7560.com/wp-content/uploads/algorithm-wallpaper-300x188.jpg 300w, https://lycos7560.com/wp-content/uploads/algorithm-wallpaper-768x480.jpg 768w" sizes="(max-width: 1024px) 100vw, 1024px" />					</a>
									</div>
						<h4 class="uagb-post__title uagb-post__text">
				<a href="https://lycos7560.com/etc/%ec%95%8c%ea%b3%a0%eb%a6%ac%ec%a6%98-%ec%9c%a0%eb%8b%88%ec%98%a8-%ed%8c%8c%ec%9d%b8%eb%93%9cunion-find-%ec%84%9c%eb%a1%9c%ec%86%8c-%ec%a7%91%ed%95%a9disjoint-set/33666/" target="_self" rel="bookmark noopener noreferrer">알고리즘 – 유니온 파인드(Union find) &#038; 서로소 집합(Disjoint Set)</a>
			</h4>
						<div class='uagb-post__text uagb-post-grid-byline'>
							</div>
						<div class="uagb-post__text uagb-post__cta wp-block-button">
				<a class="wp-block-button__link uagb-text-link" style="" href="https://lycos7560.com/etc/%ec%95%8c%ea%b3%a0%eb%a6%ac%ec%a6%98-%ec%9c%a0%eb%8b%88%ec%98%a8-%ed%8c%8c%ec%9d%b8%eb%93%9cunion-find-%ec%84%9c%eb%a1%9c%ec%86%8c-%ec%a7%91%ed%95%a9disjoint-set/33666/" target="_self" rel="bookmark noopener noreferrer">더 읽기</a>
			</div>
			
									</article>
																<article class="uagb-post__inner-wrap">								<div class='uagb-post__image'>
									<a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-17352%eb%b2%88-%ec%97%ac%eb%9f%ac%eb%b6%84%ec%9d%98-%eb%8b%a4%eb%a6%ac%ea%b0%80-%eb%90%98%ec%96%b4-%eb%93%9c%eb%a6%ac%ea%b2%a0%ec%8a%b5%eb%8b%88%eb%8b%a4-c-baekjoon/33762/" target="_self" rel="bookmark noopener noreferrer"><img decoding="async" width="1920" height="1003" src="https://lycos7560.com/wp-content/uploads/boj-og.png" class="attachment-large size-large" alt="" srcset="https://lycos7560.com/wp-content/uploads/boj-og.png 1920w, https://lycos7560.com/wp-content/uploads/boj-og-300x157.png 300w, https://lycos7560.com/wp-content/uploads/boj-og-768x401.png 768w, https://lycos7560.com/wp-content/uploads/boj-og-1536x802.png 1536w, https://lycos7560.com/wp-content/uploads/boj-og-2048x1070.png 2048w" sizes="(max-width: 1920px) 100vw, 1920px" />					</a>
									</div>
						<h4 class="uagb-post__title uagb-post__text">
				<a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-17352%eb%b2%88-%ec%97%ac%eb%9f%ac%eb%b6%84%ec%9d%98-%eb%8b%a4%eb%a6%ac%ea%b0%80-%eb%90%98%ec%96%b4-%eb%93%9c%eb%a6%ac%ea%b2%a0%ec%8a%b5%eb%8b%88%eb%8b%a4-c-baekjoon/33762/" target="_self" rel="bookmark noopener noreferrer">백준 17352번 (여러분의 다리가 되어 드리겠습니다!, C++) [BAEKJOON]</a>
			</h4>
						<div class='uagb-post__text uagb-post-grid-byline'>
							</div>
						<div class="uagb-post__text uagb-post__cta wp-block-button">
				<a class="wp-block-button__link uagb-text-link" style="" href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-17352%eb%b2%88-%ec%97%ac%eb%9f%ac%eb%b6%84%ec%9d%98-%eb%8b%a4%eb%a6%ac%ea%b0%80-%eb%90%98%ec%96%b4-%eb%93%9c%eb%a6%ac%ea%b2%a0%ec%8a%b5%eb%8b%88%eb%8b%a4-c-baekjoon/33762/" target="_self" rel="bookmark noopener noreferrer">더 읽기</a>
			</div>
			
									</article>
																<article class="uagb-post__inner-wrap">								<div class='uagb-post__image'>
									<a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1717%eb%b2%88-%ec%a7%91%ed%95%a9%ec%9d%98-%ed%91%9c%ed%98%84-c-union-find-baekjoon/33671/" target="_self" rel="bookmark noopener noreferrer"><img decoding="async" width="1024" height="535" src="https://lycos7560.com/wp-content/uploads/2022/10/boj-og-1-2048x1070-1-1024x535.png" class="attachment-large size-large" alt="" srcset="https://lycos7560.com/wp-content/uploads/2022/10/boj-og-1-2048x1070-1-1024x535.png 1024w, https://lycos7560.com/wp-content/uploads/2022/10/boj-og-1-2048x1070-1-300x157.png 300w, https://lycos7560.com/wp-content/uploads/2022/10/boj-og-1-2048x1070-1-768x401.png 768w, https://lycos7560.com/wp-content/uploads/2022/10/boj-og-1-2048x1070-1-1536x803.png 1536w, https://lycos7560.com/wp-content/uploads/2022/10/boj-og-1-2048x1070-1.png 2048w" sizes="(max-width: 1024px) 100vw, 1024px" />					</a>
									</div>
						<h4 class="uagb-post__title uagb-post__text">
				<a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1717%eb%b2%88-%ec%a7%91%ed%95%a9%ec%9d%98-%ed%91%9c%ed%98%84-c-union-find-baekjoon/33671/" target="_self" rel="bookmark noopener noreferrer">백준 1717번 (집합의 표현, C++, Union-Find) [BAEKJOON]</a>
			</h4>
						<div class='uagb-post__text uagb-post-grid-byline'>
							</div>
						<div class="uagb-post__text uagb-post__cta wp-block-button">
				<a class="wp-block-button__link uagb-text-link" style="" href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1717%eb%b2%88-%ec%a7%91%ed%95%a9%ec%9d%98-%ed%91%9c%ed%98%84-c-union-find-baekjoon/33671/" target="_self" rel="bookmark noopener noreferrer">더 읽기</a>
			</div>
			
									</article>
																<article class="uagb-post__inner-wrap">								<div class='uagb-post__image'>
									<a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2610%eb%b2%88-%ed%9a%8c%ec%9d%98%ec%a4%80%eb%b9%84-c-baekjoon/33706/" target="_self" rel="bookmark noopener noreferrer"><img decoding="async" width="1024" height="535" src="https://lycos7560.com/wp-content/uploads/2022/10/boj-og-1-2048x1070-1-1024x535.png" class="attachment-large size-large" alt="" srcset="https://lycos7560.com/wp-content/uploads/2022/10/boj-og-1-2048x1070-1-1024x535.png 1024w, https://lycos7560.com/wp-content/uploads/2022/10/boj-og-1-2048x1070-1-300x157.png 300w, https://lycos7560.com/wp-content/uploads/2022/10/boj-og-1-2048x1070-1-768x401.png 768w, https://lycos7560.com/wp-content/uploads/2022/10/boj-og-1-2048x1070-1-1536x803.png 1536w, https://lycos7560.com/wp-content/uploads/2022/10/boj-og-1-2048x1070-1.png 2048w" sizes="(max-width: 1024px) 100vw, 1024px" />					</a>
									</div>
						<h4 class="uagb-post__title uagb-post__text">
				<a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2610%eb%b2%88-%ed%9a%8c%ec%9d%98%ec%a4%80%eb%b9%84-c-baekjoon/33706/" target="_self" rel="bookmark noopener noreferrer">백준 2610번 (회의준비, C++) [BAEKJOON]</a>
			</h4>
						<div class='uagb-post__text uagb-post-grid-byline'>
							</div>
						<div class="uagb-post__text uagb-post__cta wp-block-button">
				<a class="wp-block-button__link uagb-text-link" style="" href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2610%eb%b2%88-%ed%9a%8c%ec%9d%98%ec%a4%80%eb%b9%84-c-baekjoon/33706/" target="_self" rel="bookmark noopener noreferrer">더 읽기</a>
			</div>
			
									</article>
											</div>
			


<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5698326622209671" data-ad-slot="4245812909"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
<p>The post <a href="https://lycos7560.com/etc/%ec%95%8c%ea%b3%a0%eb%a6%ac%ec%a6%98-%ec%9c%a0%eb%8b%88%ec%98%a8-%ed%8c%8c%ec%9d%b8%eb%93%9cunion-find-%ec%84%9c%eb%a1%9c%ec%86%8c-%ec%a7%91%ed%95%a9disjoint-set/33666/">알고리즘 – 유니온 파인드(Union find) &#038; 서로소 집합(Disjoint Set)</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/etc/%ec%95%8c%ea%b3%a0%eb%a6%ac%ec%a6%98-%ec%9c%a0%eb%8b%88%ec%98%a8-%ed%8c%8c%ec%9d%b8%eb%93%9cunion-find-%ec%84%9c%eb%a1%9c%ec%86%8c-%ec%a7%91%ed%95%a9disjoint-set/33666/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 2610번 (회의준비, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2610%eb%b2%88-%ed%9a%8c%ec%9d%98%ec%a4%80%eb%b9%84-c-baekjoon/33706/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2610%eb%b2%88-%ed%9a%8c%ec%9d%98%ec%a4%80%eb%b9%84-c-baekjoon/33706/#comments</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Sat, 04 Mar 2023 16:13:32 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[2610]]></category>
		<category><![CDATA[2610번]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[Floyd-Warshall]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[Union-Find]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[그래프 이론]]></category>
		<category><![CDATA[그래프 탐색]]></category>
		<category><![CDATA[기본]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 2610]]></category>
		<category><![CDATA[백준 2610번]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[유니온 파인드]]></category>
		<category><![CDATA[자료구조]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<category><![CDATA[플로이드]]></category>
		<category><![CDATA[플로이드 워셜]]></category>
		<category><![CDATA[회의준비]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=33706</guid>

					<description><![CDATA[<p>백준(BAEKJOON) 2610번 '회의준비' 문제에 대한 글입니다. 'Floyd-Warshall' &#038; 'Union Find' 알고리즘을 사용하여 해결하였습니다. (This is an article on the 'meeting preparation' problem in BAEKJOON No. 2610. Solved using 'Floyd-Warshall' &#038; 'UnionFind' algorithms.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2610%eb%b2%88-%ed%9a%8c%ec%9d%98%ec%a4%80%eb%b9%84-c-baekjoon/33706/">백준 2610번 (회의준비, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<!-- HorizontalAD -->
<ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-5698326622209671" data-ad-slot="6908948342" data-ad-format="auto" data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<div style="height:62px" aria-hidden="true" class="wp-block-spacer"></div>


				<div class="wp-block-uagb-table-of-contents uagb-toc__align-left uagb-toc__columns-1  uagb-block-46c59bdb      "
					data-scroll= "1"
					data-offset= "30"
					style=""
				>
				<div class="uagb-toc__wrap">
						<div class="uagb-toc__title">
							목차 테이블						</div>
																						<div class="uagb-toc__list-wrap ">
						<ol class="uagb-toc__list"><li class="uagb-toc__list"><a href="#회의준비" class="uagb-toc-link__trigger">회의준비</a><ul class="uagb-toc__list"><li class="uagb-toc__list"><a href="#추가-반례" class="uagb-toc-link__trigger">추가 반례</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#통과된-코드" class="uagb-toc-link__trigger">통과된 코드</a></ul></ol>					</div>
									</div>
				</div>
			


<div style="height:62px" aria-hidden="true" class="wp-block-spacer"></div>



<h1 class="wp-block-heading">회의준비</h1>



<p class="has-medium-font-size wp-block-paragraph"><a href="https://www.acmicpc.net/problem/2610" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/2610</a></p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure id="problem-info" class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th class="has-text-align-left" data-align="left">시간 제한</th><th class="has-text-align-left" data-align="left">메모리 제한</th><th class="has-text-align-left" data-align="left">제출</th><th class="has-text-align-left" data-align="left">정답</th><th class="has-text-align-left" data-align="left">맞힌 사람</th><th class="has-text-align-left" data-align="left">정답 비율</th></tr></thead><tbody><tr><td class="has-text-align-left" data-align="left">1 초</td><td class="has-text-align-left" data-align="left">128 MB</td><td class="has-text-align-left" data-align="left">7575</td><td class="has-text-align-left" data-align="left">2180</td><td class="has-text-align-left" data-align="left">1640</td><td class="has-text-align-left" data-align="left">28.616%</td></tr></tbody></table></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">문제</h3>



<p class="has-medium-font-size wp-block-paragraph">KOI 준비를 위해 회의를 개최하려 한다. </p>



<p class="has-medium-font-size wp-block-paragraph">주최측에서는 회의에 참석하는 사람의 수와 참석자들 사이의 관계를 따져 하나 이상의 위원회를 구성하려고 한다. </p>



<p class="has-medium-font-size wp-block-paragraph">위원회를 구성하는 방식은 다음과 같다.</p>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size wp-block-paragraph">1. 서로 알고 있는 사람은 반드시 같은 위원회에 속해야 한다.</p>



<p class="has-medium-font-size wp-block-paragraph">2. 효율적인 회의 진행을 위해 위원회의 수는 최대가 되어야 한다.</p>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size wp-block-paragraph">이런 방식으로 위원회를 구성한 후에 각 위원회의 대표를 한 명씩 뽑아야 한다. </p>



<p class="has-medium-font-size wp-block-paragraph">각 위원회의 대표만이 회의 시간 중 발언권을 가지며, </p>



<p class="has-medium-font-size wp-block-paragraph">따라서 회의 참석자들이 자신의 의견을 말하기 위해서는 자신이 속한 위원회의 대표에게 자신의 의견을 전달해야 한다. </p>



<p class="has-medium-font-size wp-block-paragraph">그런데 각 참석자는 자신이 알고 있는 사람에게만 의견을 전달할 수 있어 대표에게 의견을 전달하기 위해서는 때로 여러 사람을 거쳐야 한다. </p>



<p class="has-medium-font-size wp-block-paragraph">대표에게 의견을 전달하는 경로가 여러 개 있을 경우에는 가장 적은 사람을 거치는 경로로 의견을 전달하며 </p>



<p class="has-medium-font-size wp-block-paragraph">이때 거치는 사람의 수를 참석자의 의사전달시간이라고 한다.</p>



<p class="has-medium-font-size wp-block-paragraph">위원회에서 모든 참석자들의 의사전달시간 중 최댓값이 최소가 되도록 대표를 정하는 프로그램을 작성하시오.</p>



<p class="has-medium-font-size wp-block-paragraph">예를 들어 1번, 2번, 3번 세 사람으로 구성되어 있는 위원회에서 1번과 2번, 2번과 3번이 서로 알고 있다고 하자. </p>



<p class="has-medium-font-size wp-block-paragraph">1번이 대표가 되면 3번이 대표인 1번에게 의견을 전달하기 위해서 2번을 거쳐야만 한다. </p>



<p class="has-medium-font-size wp-block-paragraph">반대로 3번이 대표가 되어도 1번이 대표인 3번에게 의견을 전달하려면 2번을 거쳐야만 한다. </p>



<p class="has-medium-font-size wp-block-paragraph">하지만 2번이 대표가 되면 1번과 3번 둘 다 아무도 거치지 않고 대표에게 직접 의견을 전달 할 수 있다. </p>



<p class="has-medium-font-size wp-block-paragraph">따라서 이와 같은 경우 2번이 대표가 되어야 한다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">입력</h3>



<p class="has-medium-font-size wp-block-paragraph">첫째 중에 회의에 참석하는 사람의 수 N이 주어진다. </p>



<p class="has-medium-font-size wp-block-paragraph">참석자들은 1부터 N까지의 자연수로 표현되며 회의에 참석하는 인원은 100 이하이다. </p>



<p class="has-medium-font-size wp-block-paragraph">둘째 줄에는 서로 알고 있는 관계의 수 M이 주어진다. </p>



<p class="has-medium-font-size wp-block-paragraph">이어 M개의 각 줄에는 서로 아는 사이인 참석자를 나타내는 두개의 자연수가 주어진다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">출력</h3>



<p class="has-medium-font-size wp-block-paragraph">첫째 줄에는 구성되는 위원회의 수 K를 출력한다. </p>



<p class="has-medium-font-size wp-block-paragraph">다음 K줄에는 각 위원회의 대표 번호를 작은 수부터 차례로 한 줄에 하나씩 출력한다. </p>



<p class="has-medium-font-size wp-block-paragraph">한 위원회의 대표가 될 수 있는 사람이 둘 이상일 경우 그중 한 명만 출력하면 된다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 입력 1</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">8
7
1 2
2 3
4 5
5 6
4 6
6 7
7 4</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 출력 1</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">3
2
4
8</pre>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading has-large-font-size">추가 반례</h2>



<h3 class="wp-block-heading has-large-font-size">예제 입력 A</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">100
0</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 출력 A</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">100
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 입력 B</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">9
8
1 2
2 3
3 4
4 5
4 6
4 7
4 8
4 9</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 출력 B</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">1
3</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 입력 C</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">5
4
5 1
1 2
2 3
3 4</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 출력 C</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">1
2</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">출처</h3>



<p class="wp-block-paragraph"><a href="https://www.acmicpc.net/category/2" target="_blank" rel="noreferrer noopener">Olympiad</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/55" target="_blank" rel="noreferrer noopener">한국정보올림피아드</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/57" target="_blank" rel="noreferrer noopener">한국정보올림피아드시․도지역본선</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/74">지</a><a href="https://www.acmicpc.net/category/74" target="_blank" rel="noreferrer noopener">역</a><a href="https://www.acmicpc.net/category/74">본선 2004</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/detail/380" target="_blank" rel="noreferrer noopener">중등부</a>&nbsp;3번</p>



<p class="wp-block-paragraph"><a href="https://www.acmicpc.net/category/2" target="_blank" rel="noreferrer noopener">Olympiad</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/55" target="_blank" rel="noreferrer noopener">한국정보올림피아드</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/57" target="_blank" rel="noreferrer noopener">한국정보올림피아드시․도지역본선</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/74">지역</a><a href="https://www.acmicpc.net/category/74" target="_blank" rel="noreferrer noopener">본</a><a href="https://www.acmicpc.net/category/74">선 2004</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/detail/381" target="_blank" rel="noreferrer noopener">고등부</a>&nbsp;3번</p>



<ul class="wp-block-list">
<li>데이터를 추가한 사람:&nbsp;<a href="https://www.acmicpc.net/user/eightco" target="_blank" rel="noreferrer noopener">eightco</a></li>



<li>문제의 오타를 찾은 사람:&nbsp;<a href="https://www.acmicpc.net/user/sky1357" target="_blank" rel="noreferrer noopener">sky1357</a></li>
</ul>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">알고리즘 분류</h3>



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/175" target="_blank" rel="noreferrer noopener">자료 구조</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/7" target="_blank" rel="noreferrer noopener">그래프 이론</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/11" target="_blank" rel="noreferrer noopener">그래프 탐색</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/81" target="_blank" rel="noreferrer noopener">분리 집합</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/31" target="_blank" rel="noreferrer noopener">플로이드–워셜</a></li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:var(--wp--preset--spacing--80);margin-bottom:var(--wp--preset--spacing--80)"/>



<h2 class="wp-block-heading has-large-font-size">통과된 코드</h2>



<p class="has-medium-font-size wp-block-paragraph">Floyd-Warshall 알고리즘을 이용하여  의사전달 시간의 최솟값을 구한다.</p>



<p class="has-medium-font-size wp-block-paragraph">그 후에 Union-Find를 이용하여 위원회의 개수와 대표를 구하면 된다.</p>



<p class="has-medium-font-size wp-block-paragraph">(분리 집합 + 플로이드 와샬 문제)</p>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>
#include &lt;vector>
#include &lt;set>

using namespace std;

// N : 참석하는 사람
int N, K, f, b;

constexpr int INF = INT32_MAX/2;

constexpr int MAX = 101;

vector&lt;pair&lt;int, int>> graph[MAX];

int disArr[MAX][MAX];

int sumArr[MAX];

// 유니온 root노드 배열
int uArr[MAX];

set&lt;int> checkSet;

//부모를 찾는 함수
//모든 경로가 부모를 가르키게 함
//상수 시간의 복잡도
int Find(int x)
{
	if (uArr[x] == x) return x;
	else return uArr[x] = Find(uArr[x]);
}

//두 노드를 연결 시키는 것
//기준을 정해서 연결시키는 것이 헷갈리지 않음
//작은쪽이 부모 or 큰쪽이 부모
void Union(int x, int y)
{
	x = Find(x);
	y = Find(y);
	if (x != y) {
		if (sumArr[x] &lt;= sumArr[y]) uArr[y] = x;
		else uArr[x] = y;
	}
}

int main()
{
	ios_base::sync_with_stdio(false); // scanf와 동기화를 비활성화
	// cin.tie(null); 코드는 cin과 cout의 묶음을 풀어줍니다.
	cin.tie(NULL);
	cout.tie(NULL);

	cin >> N >> K;

	// 유니온 배열 초기화
	for (int i = 1; i &lt;= N; i++) uArr[i] = i;

	// 서로를 알고 있음(양방향)
	for (int i = 0; i &lt; K; i++) {
		cin >> f >> b;
		graph[f].push_back(make_pair(b, 1));
		graph[b].push_back(make_pair(f, 1));
	}

	// 최단 거리 배열 disArr 배열을 INF 초기화
	for (int i = 1; i &lt;= N; i++) {
		for (int j = 1; j &lt;= N; j++) {
			disArr[i][j] = INF;
		}
	}

	for (int i = 1; i &lt;= N; i++) {
		for (int j = 0; j &lt; graph[i].size(); j++) {
			int v = graph[i][j].first;
			int weight = graph[i][j].second;
			disArr[i][v] = weight;
		}
	}

	// Floyd-Warshall 알고리즘
	for (int k = 1; k &lt;= N; k++) {
		for (int i = 1; i &lt;= N; i++) {
			for (int j = 1; j &lt;= N; j++) {
				if (i == j) disArr[i][j] = 0;
				else disArr[i][j] = min(disArr[i][j], disArr[i][k] + disArr[k][j]);
			}
		}
	}

	for (int i = 1; i &lt;= N; i++) {
		for (int j = 1; j &lt;= N; j++) {
			
			if (disArr[i][j] == INF) continue;
			// 의사전달시간 중 최대값이 최소가 되도록 대표를 선출
			sumArr[i] = max(sumArr[i], disArr[i][j]); 
		}
	}

	for (int i = 1; i &lt;= N; i++) {
		for (int j = 0; j &lt; graph[i].size(); j++) {
			// Union Find를 사용하여 위원회를 구성
			Union(i, graph[i][j].first);
		}
	}

	// 결과를 출력
	// Set을 이용하여 위원회의 개수와 대표를 저장
	for (int i = 1; i &lt;= N; i++) checkSet.insert(Find(i));
	
	cout &lt;&lt; checkSet.size() &lt;&lt; "\n";
	for (auto&amp; it : checkSet) cout &lt;&lt; it &lt;&lt; "\n";

	return 0;
}</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="913" height="171" src="https://lycos7560.com/wp-content/uploads/2023/03/image-20.png" alt="" class="wp-image-33710" srcset="https://lycos7560.com/wp-content/uploads/2023/03/image-20.png 913w, https://lycos7560.com/wp-content/uploads/2023/03/image-20-300x56.png 300w, https://lycos7560.com/wp-content/uploads/2023/03/image-20-768x144.png 768w" sizes="(max-width: 913px) 100vw, 913px" /></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5698326622209671" data-ad-slot="4245812909"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2610%eb%b2%88-%ed%9a%8c%ec%9d%98%ec%a4%80%eb%b9%84-c-baekjoon/33706/">백준 2610번 (회의준비, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2610%eb%b2%88-%ed%9a%8c%ec%9d%98%ec%a4%80%eb%b9%84-c-baekjoon/33706/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>백준 1717번 (집합의 표현, C++, Union-Find) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1717%eb%b2%88-%ec%a7%91%ed%95%a9%ec%9d%98-%ed%91%9c%ed%98%84-c-union-find-baekjoon/33671/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1717%eb%b2%88-%ec%a7%91%ed%95%a9%ec%9d%98-%ed%91%9c%ed%98%84-c-union-find-baekjoon/33671/#comments</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Sat, 04 Mar 2023 12:52:39 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[1717]]></category>
		<category><![CDATA[1717번]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[Disjoint Set]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[Union-Find]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[기본]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 1717]]></category>
		<category><![CDATA[백준 1717번]]></category>
		<category><![CDATA[서로소 집합]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[유니온 파인드]]></category>
		<category><![CDATA[집합의 표현]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=33671</guid>

					<description><![CDATA[<p>백준(BAEKJOON) 1717번 '집합의 표현' 문제에 대한 글입니다. Union-Find 알고리즘을 이용하여 해결하였습니다.  (This article is about the problem of 'expression of a set' in BAEKJOON 1717. It was solved using the Union-Find algorithm.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1717%eb%b2%88-%ec%a7%91%ed%95%a9%ec%9d%98-%ed%91%9c%ed%98%84-c-union-find-baekjoon/33671/">백준 1717번 (집합의 표현, C++, Union-Find) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<!-- HorizontalAD -->
<ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-5698326622209671" data-ad-slot="6908948342" data-ad-format="auto" data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<div style="height:62px" aria-hidden="true" class="wp-block-spacer"></div>


				<div class="wp-block-uagb-table-of-contents uagb-toc__align-left uagb-toc__columns-1  uagb-block-4e6bf4a0      "
					data-scroll= "1"
					data-offset= "30"
					style=""
				>
				<div class="uagb-toc__wrap">
						<div class="uagb-toc__title">
							목차 테이블						</div>
																						<div class="uagb-toc__list-wrap ">
						<ol class="uagb-toc__list"><li class="uagb-toc__list"><a href="#집합의-표현" class="uagb-toc-link__trigger">집합의 표현</a><li class="uagb-toc__list"><a href="#접근-방법" class="uagb-toc-link__trigger">접근 방법</a><li class="uagb-toc__list"><a href="#통과된-코드" class="uagb-toc-link__trigger">통과된 코드</a></ol>					</div>
									</div>
				</div>
			


<div style="height:62px" aria-hidden="true" class="wp-block-spacer"></div>



<h1 class="wp-block-heading">집합의 표현</h1>



<p class="has-medium-font-size wp-block-paragraph"><a href="https://www.acmicpc.net/problem/1717" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/1717</a></p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure id="problem-info" class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th class="has-text-align-left" data-align="left">시간 제한</th><th class="has-text-align-left" data-align="left">메모리 제한</th><th class="has-text-align-left" data-align="left">제출</th><th class="has-text-align-left" data-align="left">정답</th><th class="has-text-align-left" data-align="left">맞힌 사람</th><th class="has-text-align-left" data-align="left">정답 비율</th></tr></thead><tbody><tr><td class="has-text-align-left" data-align="left">2 초</td><td class="has-text-align-left" data-align="left">128 MB</td><td class="has-text-align-left" data-align="left">75296</td><td class="has-text-align-left" data-align="left">23857</td><td class="has-text-align-left" data-align="left">14435</td><td class="has-text-align-left" data-align="left">28.135%</td></tr></tbody></table></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">문제</h2>



<p class="has-medium-font-size wp-block-paragraph">초기에 n+1개의 집합 {0}, {1}, {2}, … , {n}이 있다.</p>



<p class="has-medium-font-size wp-block-paragraph">여기에 합집합 연산과, 두 원소가 같은 집합에 포함되어 있는지를 확인하는 연산을 수행하려고 한다.</p>



<p class="has-medium-font-size wp-block-paragraph">집합을 표현하는 프로그램을 작성하시오.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">입력</h2>



<p class="has-medium-font-size wp-block-paragraph">첫째 줄에 n, m이 주어진다.</p>



<p class="has-medium-font-size wp-block-paragraph">m은 입력으로 주어지는 연산의 개수이다.</p>



<p class="has-medium-font-size wp-block-paragraph">다음 m개의 줄에는 각각의 연산이 주어진다.</p>



<p class="has-medium-font-size wp-block-paragraph">합집합은 0 a b의 형태로 입력이 주어진다.</p>



<p class="has-medium-font-size wp-block-paragraph">이는 a가 포함되어 있는 집합과, b가 포함되어 있는 집합을 합친다는 의미이다.</p>



<p class="has-medium-font-size wp-block-paragraph">두 원소가 같은 집합에 포함되어 있는지를 확인하는 연산은 1 a b의 형태로 입력이 주어진다.</p>



<p class="has-medium-font-size wp-block-paragraph">이는 a와 b가 같은 집합에 포함되어 있는 지를 확인하는 연산이다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">출력</h2>



<p class="has-medium-font-size wp-block-paragraph">1로 시작하는 입력에 대해서&nbsp;a와&nbsp;b가 같은 집합에 포함되어 있으면 &#8220;<code>YES</code>&#8221; 또는 &#8220;<code>yes</code>&#8220;를, </p>



<p class="has-medium-font-size wp-block-paragraph">그렇지 않다면 &#8220;<code>NO</code>&#8221; 또는 &#8220;<code>no</code>&#8220;를 한 줄에 하나씩 출력한다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">제한</h2>



<ul class="wp-block-list">
<li>1 ≤ n ≤ 1,000,000 </li>



<li>1 ≤ m ≤ 100,000 </li>



<li>0 ≤ a, b ≤ n </li>



<li>a, b는 정수</li>



<li>a와 b는 같을 수도 있다.</li>
</ul>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 1</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 1</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">NO
NO
YES</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">출처</h2>



<ul class="wp-block-list">
<li>잘못된 데이터를 찾은 사람:&nbsp;<a href="https://www.acmicpc.net/user/Apple_Cplus" target="_blank" rel="noreferrer noopener">Apple_Cplus</a></li>



<li>문제를 번역한 사람:&nbsp;<a href="https://www.acmicpc.net/user/author5" target="_blank" rel="noreferrer noopener">author5</a></li>



<li>데이터를 추가한 사람:&nbsp;<a href="https://www.acmicpc.net/user/njw1204" target="_blank" rel="noreferrer noopener">njw1204</a></li>
</ul>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">알고리즘 분류</h2>



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/175" target="_blank" rel="noreferrer noopener">자료 구조</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/81" target="_blank" rel="noreferrer noopener">분리 집합</a></li>
</ul>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:var(--wp--preset--spacing--80);margin-bottom:var(--wp--preset--spacing--80)"/>



<h1 class="wp-block-heading">접근 방법</h1>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="1266" height="1920" src="https://lycos7560.com/wp-content/uploads/2023/03/Notes_230304_214804.jpg" alt="" class="wp-image-33680" srcset="https://lycos7560.com/wp-content/uploads/2023/03/Notes_230304_214804.jpg 1266w, https://lycos7560.com/wp-content/uploads/2023/03/Notes_230304_214804-198x300.jpg 198w, https://lycos7560.com/wp-content/uploads/2023/03/Notes_230304_214804-768x1165.jpg 768w, https://lycos7560.com/wp-content/uploads/2023/03/Notes_230304_214804-1013x1536.jpg 1013w" sizes="(max-width: 1266px) 100vw, 1266px" /></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h1 class="wp-block-heading">통과된 코드</h1>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>

using namespace std;

constexpr int Max = 1000001;

// 유니온 root노드 배열
int uArr[Max];

int N, M, u, b, c;

//부모를 찾는 함수
//모든 경로가 부모를 가르키게 함
//상수 시간의 복잡도
int Find(int x)
{
    if (uArr[x] == x) return x;
    return uArr[x] = Find(uArr[x]);
}

//두 노드를 연결 시키는 것
//기준을 정해서 연결시키는 것이 헷갈리지 않음
//작은쪽이 부모 or 큰쪽이 부모
void Union(int x, int y)
{
    x = Find(x);
    y = Find(y);

    if (x != y) {
        if (x &lt; y) uArr[y] = x;
        else uArr[x] = y;
    }
}

int main()
{
    ios_base::sync_with_stdio(false); // scanf와 동기화를 비활성화
    // cin.tie(null); 코드는 cin과 cout의 묶음을 풀어줍니다.
    cin.tie(NULL);
    cout.tie(NULL);

    cin >> N >> M;
    // 유니온 배열 초기화
    for (int i = 0; i &lt;= N; i++) uArr[i] = i;

    for (int i = 0; i &lt; M; i++) {

        cin >> u >> b >> c;

        if (u == 0) { // 합집합 연산
            Union(b, c);
        }
        else { // 질의
            if (Find(b) != Find(c)) cout &lt;&lt; "NO\n";
            else cout &lt;&lt; "YES\n";
        }
    }

    return 0;
}</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="912" height="131" src="https://lycos7560.com/wp-content/uploads/2023/03/image-17.png" alt="" class="wp-image-33676" srcset="https://lycos7560.com/wp-content/uploads/2023/03/image-17.png 912w, https://lycos7560.com/wp-content/uploads/2023/03/image-17-300x43.png 300w, https://lycos7560.com/wp-content/uploads/2023/03/image-17-768x110.png 768w" sizes="(max-width: 912px) 100vw, 912px" /></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5698326622209671" data-ad-slot="4245812909"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1717%eb%b2%88-%ec%a7%91%ed%95%a9%ec%9d%98-%ed%91%9c%ed%98%84-c-union-find-baekjoon/33671/">백준 1717번 (집합의 표현, C++, Union-Find) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1717%eb%b2%88-%ec%a7%91%ed%95%a9%ec%9d%98-%ed%91%9c%ed%98%84-c-union-find-baekjoon/33671/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
