<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Union Find on Kelly Dev</title><link>https://kelly-chui.github.io/tags/union-find/</link><description>Recent content in Union Find on Kelly Dev</description><generator>Hugo</generator><language>en-us</language><lastBuildDate>Thu, 09 Jul 2026 09:21:23 +0900</lastBuildDate><atom:link href="https://kelly-chui.github.io/tags/union-find/index.xml" rel="self" type="application/rss+xml"/><item><title>Leetcode 3532. Path Existence Queries in a Graph I</title><link>https://kelly-chui.github.io/ps/2026/ps-leetcode-3532-path-existence-queries-in-a-graph-i/</link><pubDate>Thu, 09 Jul 2026 09:21:23 +0900</pubDate><guid>https://kelly-chui.github.io/ps/2026/ps-leetcode-3532-path-existence-queries-in-a-graph-i/</guid><description>&lt;h2 id="문제"&gt;문제&lt;/h2&gt;
&lt;p&gt;&lt;a href="https://leetcode.com/problems/path-existence-queries-in-a-graph-i"&gt;https://leetcode.com/problems/path-existence-queries-in-a-graph-i&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="풀이"&gt;풀이&lt;/h2&gt;
&lt;p&gt;그래프 각 노드의 연결 조건이 주어지고, 들어오는 쿼리들에 있는 두 노드들이 연결되어있는지 확인하는 문제이다.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;queries&lt;/code&gt; 배열의 길이가 최대 10만이므로, BFS를 이용해서 풀기는 거의 불가능하다. BFS보다 더 빠르게 &amp;lsquo;연결 여부만&amp;rsquo; 확인하는 방법은 Union-Find Set을 사용하면 된다.&lt;/p&gt;
&lt;p&gt;우선 연결 조건은 두 노드의 절대 차이(노드 번호의 차이의 절대값)가 &lt;code&gt;maxDiff&lt;/code&gt; 이하여야 하는데, 문제에서 &lt;code&gt;nums&lt;/code&gt; 배열이 오름차순으로 정렬되어 있다고 했으므로, &lt;code&gt;nums[j] - nums[i] &amp;lt;= maxDiff&lt;/code&gt; 를 만족하는 연속된 범위를 투 포인터로 찾을 수 있다.&lt;/p&gt;</description></item><item><title>BOJ 4195. 친구 네트워크</title><link>https://kelly-chui.github.io/ps/2023/ps-boj-4195-friend-network/</link><pubDate>Thu, 23 Feb 2023 00:00:00 +0000</pubDate><guid>https://kelly-chui.github.io/ps/2023/ps-boj-4195-friend-network/</guid><description>&lt;h2 id="문제"&gt;문제&lt;/h2&gt;
&lt;p&gt;&lt;a href="https://www.acmicpc.net/problem/4195"&gt;https://www.acmicpc.net/problem/4195&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="풀이"&gt;풀이&lt;/h2&gt;
&lt;p&gt;전형적인 Union-Find 문제이지만, 각 원소들이 정수가 아닌 문자열로 구성되어 있는 특징이 있다. 정수를 문자열로 대응시키는 것은 Dictionary를 쓰면 쉽게 할 수 있으므로 다음과 같은 방식으로 알고리즘이 진행된다&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;입력값(문자열 2개)에 대해 각 문자열이 Dictionary에 대응되는 Value가 있는지 판단한다.&lt;/li&gt;
&lt;li&gt;1에서 없다면 Dictionary에 &lt;code&gt;[String:Integer]&lt;/code&gt; 꼴로 매핑한다. 있다면 그냥 넘어간다.&lt;/li&gt;
&lt;li&gt;매핑이 끝났다면, 들어온 두 문자열로 &lt;code&gt;union(a, b)&lt;/code&gt; 연산을 하고 그 집합 내부의 원소의 개수를 카운트한다.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;여기서 문제는 3번이다. 어떻게 해야 효율적으로 집합 내부의 원소를 카운트 할 수 있을까?&lt;/p&gt;</description></item></channel></rss>