<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="zh-Hans-CN">
	<id>https://www.33dai.wiki/index.php?action=history&amp;feed=atom&amp;title=05-%E7%AE%97%E6%B3%95%E6%A8%A1%E6%9D%BF%2F08-%E5%B9%B6%E6%9F%A5%E9%9B%86</id>
	<title>05-算法模板/08-并查集 - 版本历史</title>
	<link rel="self" type="application/atom+xml" href="https://www.33dai.wiki/index.php?action=history&amp;feed=atom&amp;title=05-%E7%AE%97%E6%B3%95%E6%A8%A1%E6%9D%BF%2F08-%E5%B9%B6%E6%9F%A5%E9%9B%86"/>
	<link rel="alternate" type="text/html" href="https://www.33dai.wiki/index.php?title=05-%E7%AE%97%E6%B3%95%E6%A8%A1%E6%9D%BF/08-%E5%B9%B6%E6%9F%A5%E9%9B%86&amp;action=history"/>
	<updated>2026-05-20T23:08:58Z</updated>
	<subtitle>本wiki上该页面的版本历史</subtitle>
	<generator>MediaWiki 1.45.1</generator>
	<entry>
		<id>https://www.33dai.wiki/index.php?title=05-%E7%AE%97%E6%B3%95%E6%A8%A1%E6%9D%BF/08-%E5%B9%B6%E6%9F%A5%E9%9B%86&amp;diff=359&amp;oldid=prev</id>
		<title>33DAI：​导入1个版本</title>
		<link rel="alternate" type="text/html" href="https://www.33dai.wiki/index.php?title=05-%E7%AE%97%E6%B3%95%E6%A8%A1%E6%9D%BF/08-%E5%B9%B6%E6%9F%A5%E9%9B%86&amp;diff=359&amp;oldid=prev"/>
		<updated>2026-05-20T18:12:11Z</updated>

		<summary type="html">&lt;p&gt;导入1个版本&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;zh-Hans-CN&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;←上一版本&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;2026年5月20日 (三) 18:12的版本&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-notice&quot; lang=&quot;zh-Hans-CN&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;（没有差异）&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>33DAI</name></author>
	</entry>
	<entry>
		<id>https://www.33dai.wiki/index.php?title=05-%E7%AE%97%E6%B3%95%E6%A8%A1%E6%9D%BF/08-%E5%B9%B6%E6%9F%A5%E9%9B%86&amp;diff=358&amp;oldid=prev</id>
		<title>-&gt;Importer：​批量导入三三文档</title>
		<link rel="alternate" type="text/html" href="https://www.33dai.wiki/index.php?title=05-%E7%AE%97%E6%B3%95%E6%A8%A1%E6%9D%BF/08-%E5%B9%B6%E6%9F%A5%E9%9B%86&amp;diff=358&amp;oldid=prev"/>
		<updated>2026-05-20T16:50:33Z</updated>

		<summary type="html">&lt;p&gt;批量导入三三文档&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;zh-Hans-CN&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;←上一版本&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;2026年5月20日 (三) 16:50的版本&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;4&quot; class=&quot;diff-notice&quot; lang=&quot;zh-Hans-CN&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;（没有差异）&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;!-- diff cache key my_wiki:diff:1.41:old-259:rev-358 --&gt;
&lt;/table&gt;</summary>
		<author><name>-&gt;Importer</name></author>
	</entry>
	<entry>
		<id>https://www.33dai.wiki/index.php?title=05-%E7%AE%97%E6%B3%95%E6%A8%A1%E6%9D%BF/08-%E5%B9%B6%E6%9F%A5%E9%9B%86&amp;diff=259&amp;oldid=prev</id>
		<title>33DAI：​导入1个版本</title>
		<link rel="alternate" type="text/html" href="https://www.33dai.wiki/index.php?title=05-%E7%AE%97%E6%B3%95%E6%A8%A1%E6%9D%BF/08-%E5%B9%B6%E6%9F%A5%E9%9B%86&amp;diff=259&amp;oldid=prev"/>
		<updated>2026-05-20T16:25:36Z</updated>

		<summary type="html">&lt;p&gt;导入1个版本&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;zh-Hans-CN&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;←上一版本&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;2026年5月20日 (三) 16:25的版本&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-notice&quot; lang=&quot;zh-Hans-CN&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;（没有差异）&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>33DAI</name></author>
	</entry>
	<entry>
		<id>https://www.33dai.wiki/index.php?title=05-%E7%AE%97%E6%B3%95%E6%A8%A1%E6%9D%BF/08-%E5%B9%B6%E6%9F%A5%E9%9B%86&amp;diff=258&amp;oldid=prev</id>
		<title>-&gt;Importer：​批量导入三三文档</title>
		<link rel="alternate" type="text/html" href="https://www.33dai.wiki/index.php?title=05-%E7%AE%97%E6%B3%95%E6%A8%A1%E6%9D%BF/08-%E5%B9%B6%E6%9F%A5%E9%9B%86&amp;diff=258&amp;oldid=prev"/>
		<updated>2026-05-20T16:22:29Z</updated>

		<summary type="html">&lt;p&gt;批量导入三三文档&lt;/p&gt;
&lt;p&gt;&lt;b&gt;新页面&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&lt;br /&gt;
并查集是一种用于维护&amp;#039;&amp;#039;&amp;#039;集合合并&amp;#039;&amp;#039;&amp;#039;和&amp;#039;&amp;#039;&amp;#039;查询元素所属集合&amp;#039;&amp;#039;&amp;#039;的数据结构。&lt;br /&gt;
&lt;br /&gt;
== 基础模板 ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
const int MAXN = 100005;&lt;br /&gt;
int fa[MAXN];&lt;br /&gt;
&lt;br /&gt;
void init(int n)&lt;br /&gt;
{&lt;br /&gt;
    for (int i = 1; i &amp;lt;= n; i++)&lt;br /&gt;
        fa[i] = i;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int find(int x)&lt;br /&gt;
{&lt;br /&gt;
    return fa[x] == x ? x : fa[x] = find(fa[x]);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void merge(int x, int y)&lt;br /&gt;
{&lt;br /&gt;
    x = find(x);&lt;br /&gt;
    y = find(y);&lt;br /&gt;
    if (x != y)&lt;br /&gt;
        fa[x] = y;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 按秩合并优化 ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int fa[MAXN], siz[MAXN];&lt;br /&gt;
&lt;br /&gt;
void init(int n)&lt;br /&gt;
{&lt;br /&gt;
    for (int i = 1; i &amp;lt;= n; i++)&lt;br /&gt;
        fa[i] = i, siz[i] = 1;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int find(int x)&lt;br /&gt;
{&lt;br /&gt;
    return fa[x] == x ? x : fa[x] = find(fa[x]);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void merge(int x, int y)&lt;br /&gt;
{&lt;br /&gt;
    x = find(x);&lt;br /&gt;
    y = find(y);&lt;br /&gt;
    if (x == y)&lt;br /&gt;
        return;&lt;br /&gt;
    if (siz[x] &amp;lt; siz[y])&lt;br /&gt;
        swap(x, y);&lt;br /&gt;
    fa[y] = x;&lt;br /&gt;
    siz[x] += siz[y];&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 带权并查集 ==&lt;br /&gt;
&lt;br /&gt;
维护每个节点到根节点的关系（如距离）：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int fa[MAXN], dis[MAXN];&lt;br /&gt;
&lt;br /&gt;
void init(int n)&lt;br /&gt;
{&lt;br /&gt;
    for (int i = 1; i &amp;lt;= n; i++)&lt;br /&gt;
        fa[i] = i, dis[i] = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int find(int x)&lt;br /&gt;
{&lt;br /&gt;
    if (fa[x] != x)&lt;br /&gt;
    {&lt;br /&gt;
        int root = find(fa[x]);&lt;br /&gt;
        dis[x] += dis[fa[x]];&lt;br /&gt;
        fa[x] = root;&lt;br /&gt;
    }&lt;br /&gt;
    return fa[x];&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void merge(int x, int y, int w) // x 到 y 的距离为 w&lt;br /&gt;
{&lt;br /&gt;
    int fx = find(x), fy = find(y);&lt;br /&gt;
    if (fx != fy)&lt;br /&gt;
    {&lt;br /&gt;
        fa[fx] = fy;&lt;br /&gt;
        dis[fx] = dis[y] + w - dis[x];&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 常见应用 ==&lt;br /&gt;
&lt;br /&gt;
- 判断图连通性&lt;br /&gt;
- Kruskal 最小生成树算法&lt;br /&gt;
- 维护朋友敌人关系（扩展域并查集）&lt;br /&gt;
- 区间染色问题&lt;br /&gt;
&lt;br /&gt;
== 时间复杂度 ==&lt;br /&gt;
&lt;br /&gt;
- 路径压缩 + 按秩合并：均摊 &amp;lt;math&amp;gt;O(\alpha(n))&amp;lt;/math&amp;gt;，其中 &amp;lt;math&amp;gt;\alpha&amp;lt;/math&amp;gt; 为阿克曼函数的反函数，近似 &amp;lt;math&amp;gt;O(1)&amp;lt;/math&amp;gt;&lt;br /&gt;
- 仅路径压缩或仅按秩合并：&amp;lt;math&amp;gt;O(\log n)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:算法模板]]&lt;br /&gt;
[[Category:三三文档]]&lt;/div&gt;</summary>
		<author><name>-&gt;Importer</name></author>
	</entry>
</feed>