{"id":316,"date":"2026-03-19T13:07:22","date_gmt":"2026-03-19T05:07:22","guid":{"rendered":"https:\/\/jiangqvweihuan.cn\/?p=316"},"modified":"2026-03-19T13:07:22","modified_gmt":"2026-03-19T05:07:22","slug":"%e7%ae%97%e6%b3%95%e5%bf%83%e4%bc%a0%c2%b7%e5%b9%b6%e6%9f%a5%e9%9b%86","status":"publish","type":"post","link":"https:\/\/jiangqvweihuan.cn\/index.php\/2026\/03\/19\/%e7%ae%97%e6%b3%95%e5%bf%83%e4%bc%a0%c2%b7%e5%b9%b6%e6%9f%a5%e9%9b%86\/","title":{"rendered":"\u7b97\u6cd5\u5fc3\u4f20\u00b7\u5e76\u67e5\u96c6"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u5e76\u67e5\u96c6\uff08Disjoint Set Union\uff0cDSU\uff09<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1.\u5f15\u5165<\/h3>\n\n\n\n<p>\u5e76\u67e5\u96c6\uff08Disjoint Set Union\uff0cDSU\uff09\u662f\u4e00\u79cd\u7528\u4e8e\u5904\u7406<strong>\u4e0d\u76f8\u4ea4\u96c6\u5408<\/strong>\u7684\u5408\u5e76\u548c\u67e5\u8be2\u6811\u5f62\u95ee\u9898\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5176\u4e3b\u8981\u6709\u4e24\u79cd\u64cd\u4f5c\u65b9\u5f0f\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u67e5\u627e<\/strong>\uff08Find\uff09\uff1a\u786e\u5b9a\u67d0\u4e2a\u5143\u7d20\u5c5e\u4e8e\u54ea\u4e2a\u96c6\u5408\uff08\u901a\u5e38\u8fd4\u56de\u96c6\u5408\u7684\u201c\u4ee3\u8868\u5143\u201d\uff09\u3002<\/li>\n\n\n\n<li><strong>\u5408\u5e76<\/strong>\uff08Union\uff09\uff1a\u5c06\u4e24\u4e2a\u4e0d\u540c\u7684\u96c6\u5408\u5408\u5e76\u4e3a\u4e00\u4e2a\u96c6\u5408\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u5e76\u67e5\u96c6\u5e7f\u6cdb\u5e94\u7528\u4e8e\u8fde\u901a\u6027\u95ee\u9898\uff0c\u5982\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u56fe\u8bba\u4e2d\u7684\u8fde\u901a\u5206\u91cf\u5224\u65ad\uff08Kruskal \u7b97\u6cd5\u6c42\u6700\u5c0f\u751f\u6210\u6811\uff09<\/li>\n\n\n\n<li>\u52a8\u6001\u8fde\u901a\u6027\u95ee\u9898\uff08\u7f51\u7edc\u8fde\u63a5\u3001\u793e\u4ea4\u7f51\u7edc\u597d\u53cb\u5173\u7cfb\uff09<\/li>\n\n\n\n<li>\u96c6\u5408\u5212\u5206\u95ee\u9898<\/li>\n\n\n\n<li>\u6700\u8fd1\u516c\u5171\u7956\u5148\uff08LCA\uff09\u7684\u79bb\u7ebf\u7b97\u6cd5<\/li>\n<\/ol>\n\n\n\n<p>\u6211\u4eec\u6765\u601d\u8003\u8fd9\u6837\u4e00\u4e2a\u95ee\u9898\uff1a<\/p>\n\n\n\n<p>\u6709 n \u4f4d\u9b54\u6cd5\u5c11\u5973\uff0c\u7f16\u53f7\u4ece 1 \u5230 n\u3002\u521d\u59cb\u65f6\uff0c\u5927\u5bb6\u4e92\u76f8\u90fd\u4e0d\u8ba4\u8bc6\uff08\u5404\u81ea\u4e3a\u9635\uff09\u3002\u63a5\u4e0b\u6765\u4f1a\u4f9d\u6b21\u53d1\u751f m \u4e2a\u4e8b\u4ef6\uff0c\u6bcf\u4e2a\u4e8b\u4ef6\u53ef\u80fd\u662f\u4ee5\u4e0b\u4e24\u79cd\u4e4b\u4e00\uff1a<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li>\u6210\u4e3a\u670b\u53cb\uff08op = 1\uff09\uff1a\u9b54\u6cd5\u5c11\u5973 x \u548c y \u901a\u8fc7\u5951\u7ea6\u5efa\u7acb\u4e86\u76f4\u63a5\u7684\u670b\u53cb\u5173\u7cfb\u3002<\/li>\n\n\n\n<li>\u8be2\u95ee\u662f\u5426\u540c\u9635\uff08op = 2\uff09\uff1a\u8be2\u95ee\u9b54\u6cd5\u5c11\u5973 x \u548c y \u662f\u5426\u5728\u540c\u4e00\u4e2a\u9b54\u6cd5\u9635\u4e2d\uff08\u5373\u662f\u5426\u53ef\u4ee5\u901a\u8fc7\u4e00\u7cfb\u5217\u670b\u53cb\u5173\u7cfb\u76f8\u8fde\uff09\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u5bf9\u4e8e\u6bcf\u4e2a\u8be2\u95ee\uff0c\u4f60\u9700\u8981\u8f93\u51fa\u00a0&#8220;Yes&#8221;\u00a0\u6216\u00a0&#8220;No&#8221;\u3002<\/p>\n\n\n\n<p>\u7b2c\u4e00\u884c\u4e24\u4e2a\u6574\u6570 n, m\uff0c\u8868\u793a\u9b54\u6cd5\u5c11\u5973\u6570\u91cf\uff081 \u2264 n \u2264 10^5\uff09\u548c\u4e8b\u4ef6\u6570\u91cf\uff081 \u2264 m \u2264 2\u00d710^5\uff09\u3002<br>\u63a5\u4e0b\u6765 m \u884c\uff0c\u6bcf\u884c\u4e09\u4e2a\u6574\u6570 op, x, y\uff0c\u5176\u4e2d op \u4e3a 1 \u6216 2\uff0cx \u548c y \u4e3a\u9b54\u6cd5\u5c11\u5973\u7684\u7f16\u53f7\uff081 \u2264 x, y \u2264 n\uff09\u3002<\/p>\n\n\n\n<p>\u5bf9\u4e8e\u4e0a\u9762\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u628a\u6574\u4e2a\u8fc7\u7a0b\u5206\u4e3a\u4e09\u6b65\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u521d\u59cb\u65f6\uff0c\u6bcf\u4e2a\u9b54\u6cd5\u5c11\u5973\u72ec\u81ea\u4e3a\u4e00\u4e2a\u96c6\u5408\uff08\u9b54\u6cd5\u9635\uff09\u3002<\/li>\n\n\n\n<li>\u5f53\u53d1\u751f\u201c\u6210\u4e3a\u670b\u53cb\u201d\u4e8b\u4ef6\u65f6\uff0c\u5c06\u4e24\u4e2a\u9b54\u6cd5\u5c11\u5973\u6240\u5728\u7684\u96c6\u5408\u5408\u5e76\u3002<\/li>\n\n\n\n<li>\u5f53\u53d1\u751f\u201c\u8be2\u95ee\u201d\u4e8b\u4ef6\u65f6\uff0c\u5224\u65ad\u4e24\u4e2a\u9b54\u6cd5\u5c11\u5973\u662f\u5426\u5728\u540c\u4e00\u96c6\u5408\uff08\u5373\u6839\u8282\u70b9\u662f\u5426\u76f8\u540c\uff09\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">2.\u57fa\u7840\u5e76\u67e5\u96c6<\/h3>\n\n\n\n<p>\u4e0b\u9762\u7ed9\u51fa\u7528\u5e76\u67e5\u96c6\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;bits\/stdc++.h>\nusing namespace std;\n\nconst int N = 1e5 + 10;\nint parent&#91;N];\n\nvoid init(int n)\n{\n    \/\/\u521d\u59cb\u5316parent\u6570\u7ec4\uff0c\u6ca1\u4e00\u4e2a\u9b54\u6cd5\u5c11\u5973\u9ed8\u8ba4\u53ea\u548c\u81ea\u5df1\u662f\u670b\u53cb\n    for (int i = 1; i &lt;= n; ++i)\n        parent&#91;i] = i;\n}\n\nint find(int x)\n{\n    \/\/\u67e5\u8be2\u7b2cx\u4f4d\u9b54\u6cd5\u5c11\u5973\u662f\u4e0d\u662f\u8fd8\u662f\u53ea\u548c\u81ea\u5df1\u662f\u670b\u53cb\n    while (x != parent&#91;x])\n    {\n        x = parent&#91;x];\n    }\n    return x;\n}\n\nvoid unite(int x, int y)\n{\n    \/\/\u5c06x\u548cy\u53d8\u6210\u670b\u53cb\n    int rx = find(x);\n    int ry = find(y);\n    if (rx != ry)\n    {\n        parent&#91;ry] = rx;\n    }\n}\n\nsigned main()\n{\n    ios::sync_with_stdio(false);\n    cin.tie(0);\n    cout.tie(0);\n\n    int n, m;\n    cin >> n >> m;\n    init(n);\n    while (m--)\n    {\n        int op, x, y;\n        cin >> op >> x >> y;\n        if (op == 1)\n        {\n            unite(x, y);\n        }\n        else\n        {\n            if (find(x) == find(y))\n                cout &lt;&lt; \"Yes\" &lt;&lt; endl;\n            else\n                cout &lt;&lt; \"No\" &lt;&lt; endl;\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u6211\u4eec\u5bf9\u4e8e\u8fd9\u4e00\u4e2a\u6837\u4f8b\u8fdb\u884c\u5206\u6790\uff1a<\/p>\n\n\n<p>\u8f93\u5165\uff1a<br \/>\n5 6<br \/>\n1 1 2<br \/>\n1 2 3<br \/>\n2 1 3<br \/>\n2 3 4<br \/>\n1 4 5<br \/>\n2 4 5<br \/>\n\u8f93\u51fa\uff1a<br \/>\nYes<br \/>\nNo<br \/>\nYes<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/jiangqvweihuan.cn\/wp-content\/uploads\/2026\/03\/image-3.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"864\" height=\"759\" data-original=\"https:\/\/jiangqvweihuan.cn\/wp-content\/uploads\/2026\/03\/image-3.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-323\" style=\"aspect-ratio:1.138355990344302;width:395px;height:auto\"  sizes=\"auto, (max-width: 864px) 100vw, 864px\" \/><\/div><\/figure>\n\n\n\n<p>\u4e0d\u96be\u53d1\u73b0\u521d\u59cb\u5316\u4e4b\u540e1 1 2\u4f1a\u662fparent\u6570\u7ec4\u91cc\u7684parent[2]=find(1)=1\uff0c\u540c\u7406\uff0c1 2 3\u4e5f\u4f1a\u628aparent[3] = find(2)=1\uff0c\u6b64\u65f6\u67e5\u8be21\u548c3\u7684\u5173\u7cfb\uff0c\u8c03\u7528if\u5224\u65adfind(1)\u548cfind(3)\u4f1a\u53d1\u73b0\u90fd\u6307\u5411\u4e861\uff0c\u6b64\u65f6\u5c31\u4ee3\u8868\u662f\u670b\u53cb\uff0c\u4e0d\u540c\u5219\u4ee3\u8868\u67e5\u8be2\u8fd9\u4e00\u523b\u8fd8\u4e0d\u662f\u670b\u53cb\uff0c\u6ca1\u6709\u4f8b\u5916\u3002<\/p>\n\n\n\n<p>\u6700\u540e\u5bf9\u4e8e\u8fd9\u4e00\u7ec4\u6837\u4f8bparent\u6570\u7ec4\u4f1a\u53d8\u4e3a\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/jiangqvweihuan.cn\/wp-content\/uploads\/2026\/03\/image-4.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"928\" height=\"456\" data-original=\"https:\/\/jiangqvweihuan.cn\/wp-content\/uploads\/2026\/03\/image-4.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-324\" style=\"aspect-ratio:2.0351302241066023;width:377px;height:auto\"  sizes=\"auto, (max-width: 928px) 100vw, 928px\" \/><\/div><\/figure>\n\n\n\n<p>\u5176\u4e2dparent\u6570\u7ec4\u7684\u503c\u5168\u6307\u5411parent[1]=1\u3002<\/p>\n\n\n\n<p>\u663e\u7136\u4ee3\u7801\u662f\u6b63\u786e\u4e14\u5408\u7406\u7684\uff0c\u4e0d\u8fc7\uff0c\u80fd\u4e0d\u80fd\u518d\u4f18\u5316\u4ee5\u4e0b\u5462\uff1f\u5982\u679c\u6211\u4eec\u7684\u6837\u4f8b\u53d8\u6210\u91cc\u9762\u5408\u5e76\u64cd\u4f5c\u53d8\u4e3a1\u548c2\uff0c3\u548c1\uff0c4\u548c3\uff0c5\u548c4\u65f6\u6211\u4eec\u7684parent\u6570\u7ec4\u4f1a\u53d8\u6210\u4ec0\u4e48\u5462\uff1f\u4e0b\u9762\u7ed9\u51faparent\u6570\u7ec4\u6700\u540e\u7684\u6570\u503c\uff1a<\/p>\n\n\n\n<p>parent[1]=3\uff0cparent[2]=1,parent[3]=4\uff0cparent[4]=5\uff0cparent[5]=5\u3002\u8fd9\u65f6\u5019\u4ee3\u8868\u7740\u6bcf\u4e00\u4f4d\u9b54\u6cd5\u5c11\u5973\u548c\u5f7c\u6b64\u90fd\u662f\u670b\u53cb\uff0c\u8fd9\u65f6\u6211\u4eec\u5982\u679c\u67e5\u8be21\u548c2\u662f\u5426\u4e3a\u670b\u53cbfind\u51fd\u6570\u4f1a\u4e00\u76f4\u8c03\u7528\u5230x=5\u65f6\u624d\u4f1a\u8fd4\u56de\u503c\uff0c\u8fd9\u79cd\u6781\u7aef\u6761\u4ef6\u4e0b\u6811\u5f62\u7ed3\u6784\u9000\u5316\u4e3a\u7ebf\u6027\uff0c\u6b64\u65f6\u82e5n\u548cm\u90fd\u5f88\u5927\uff0c\u67e5\u8be2\u90fd\u5f88\u6781\u7aef\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u5c06\u4f1a\u53d8\u4e3aO(nm)\uff0c\u663e\u7136\u8d85\u65f6\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3.\u4f18\u5316\u5e76\u67e5\u96c6<\/h3>\n\n\n\n<p>\u6211\u4eec\u7684\u601d\u8def\u662f\u5bf9\u7684\uff0c\u4f46\u662f\u5fc5\u987b\u8981\u4f18\u5316\u3002\u4f18\u5316\u4ece\u54ea\u91cc\u5165\u624b\u5462\uff1f\u901a\u5e38\uff0c\u6211\u4eec\u53ef\u4ee5\u8fdb\u884c<strong>\u8def\u5f84\u538b\u7f29<\/strong>\uff0c\u4e0b\u9762\u5148\u7ed9\u51fa\u8def\u5f84\u538b\u7f29\u540e\u5bf9\u5e94\u7684find\u51fd\u6570\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int find(int x)\n{\n    \/\/\u67e5\u8be2\u7b2cx\u4f4d\u9b54\u6cd5\u5c11\u5973\u662f\u4e0d\u662f\u8fd8\u662f\u53ea\u548c\u81ea\u5df1\u662f\u670b\u53cb\n    if (parent&#91;x] != x)\n    {\n        parent&#91;x] = find(parent&#91;x]);\n    }\n    return parent&#91;x];\n}<\/code><\/pre>\n\n\n\n<p>\u8bfb\u8005\u4e0d\u59a8\u81ea\u5df1\u5bf9\u4ee5\u4e0b\u6837\u4f8b\u8fdb\u884c\u6a21\u62df\uff1a<\/p>\n\n\n<p>\u8f93\u5165\uff1a<br \/>\n5 6<br \/>\n1 1 2<br \/>\n1 3 1<br \/>\n1 4 3<br \/>\n1 5 4<br \/>\n2 2 5<br \/>\n2 1 5<br \/>\n\u8f93\u51fa\uff1a<br \/>\nYes<br \/>\nYes<\/p>\n\n\n\n<p>\u6211\u4eec\u5728find(x)\u51fd\u6570\u4e2d\u76f4\u63a5\u5c06parent[x]\u8fdb\u884c\u4fee\u6539\uff0c\u4f7f\u5f97\u7b2c\u4e00\u6b21\u540e\u6bcf\u4e00\u6b21\u67e5\u8fc7\u548cparent[x]\u6709\u670b\u53cb\u5173\u7cfb\u7684\u6240\u6709\u8282\u70b9\u90fd\u4f1a\u6307\u5411\u6700\u7ec8\u5171\u540c\u7684\u6839\u8282\u70b9\uff0c\u4ee5\u540e\u4ec5\u9700\u4e00\u6b65\u5c31\u80fd\u76f4\u63a5\u67e5\u8be2\u6839\u8282\u70b9\u3002\u8fd9\u6837\u7684\u4f18\u5316\u5df2\u7ecf\u8db3\u591f\u4e86\uff0c\u5bf9\u4e8e\u6837\u4f8b\u7b2c\u4e00\u6b21\u7684\u67e5\u8be2\u65f6\u4f9d\u65e7\u662fparent[1]=3\uff0cparent[2]=1,parent[3]=4\uff0cparent[4]=5\uff0cparent[5]=5\uff0c\u4f46\u662f\u5f53\u7b2c\u4e8c\u6b21\u67e5\u8be2\u65f6parent\u6570\u7ec4\u53d8\u4e3aparent[1]=5\uff0cparent[2]=5,parent[3]=5\uff0cparent[4]=5\uff0cparent[5]=5\uff0c\u4e00\u6b65\u5373\u5230\u4f4d\uff0c\u8bfb\u8005\u53ef\u4ee5\u624b\u5199\u6a21\u62df\u4e00\u904d\u53d8\u6362\u6d41\u7a0b\u3002\u8fd9\u6837\u5c31\u7b97\u662f\u6700\u6781\u7aef\u7684\u60c5\u51b5\uff0c\u9664\u4e86\u7b2c\u4e00\u6b21\u67e5\u8be2\u6211\u4eec\u9700\u8981\u4ece\u5c3e\u5230\u5934\uff0c\u5176\u4ed6\u6b21\u67e5\u8be2\u590d\u6742\u5ea6\u5747\u4e3aO(1)\u3002<\/p>\n\n\n\n<p>\u90a3\u4e48\u8fd8\u53ef\u4ee5\u4e0d\u53ef\u4ee5\u5728\u6b64\u63d0\u5347\u5462\uff1f\u5f53\u7136\u53ef\u4ee5\uff0c\u4ee5\u4e0b\u5185\u5bb9\u8bfb\u8005\u53ef\u4ee5\u4e86\u89e3\uff0c\u4f18\u5316\u7a0b\u5ea6\u8fdc\u4e0d\u53ca\u8def\u5f84\u538b\u7f29\u6548\u7387\uff0c\u7ed3\u5408\u4f7f\u7528\u5bf9\u4e8e\u65f6\u95f4\u590d\u6742\u5ea6\u7684\u4f18\u5316\u4e5f\u5341\u5206\u6709\u9650\uff0c\u56e0\u4e3a\u8def\u5f84\u538b\u7f29\u6548\u7387\u5df2\u7ecf\u8db3\u591f\u9ad8\u4e86\u3002\u6211\u4eec\u4ecefind\u51fd\u6570\u8fdb\u884c\u4e86\u538b\u7f29\uff0c\u90a3\u4e48\u6211\u4eec\u662f\u4e0d\u662f\u4e5f\u53ef\u4ee5\u5728unite\u51fd\u6570\u8fd0\u884c\u65f6\u5c31\u8fdb\u884c\u538b\u7f29\u64cd\u4f5c\u5462\uff1f\u5f53\u7136\u53ef\u4ee5\uff0c\u4ee5\u4e0b\u8fd9\u79cd\u538b\u7f29\u65b9\u5f0f\u53eb\u505a<strong>\u6309\u79e9\u5408\u5e76<\/strong>\uff0c\u4e0b\u9762\u7ed9\u51fa\u4fee\u6539\u4f18\u5316\u540e\u7684unite\u51fd\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int rank_&#91;N];  \/\/ \u8bb0\u5f55\u6811\u7684\u9ad8\u5ea6\uff08\u79e9\uff09\uff0c\u6ce8\u610f\u907f\u5f00\u5173\u952e\u5b57 rank\nvoid init(int n) {\n    for (int i = 1; i &lt;= n; i++) {\n        parent&#91;i] = i;\n        rank_&#91;i] = 0;   \/\/ \u521d\u59cb\u79e9\u4e3a0\n    }\n}\nvoid unite(int x, int y) {\n    int rx = find(x);\n    int ry = find(y);\n    if (rx == ry) return;\n\n    \/\/ \u6309\u79e9\u5408\u5e76\uff1a\u5c06\u79e9\u5c0f\u7684\u6811\u5408\u5e76\u5230\u79e9\u5927\u7684\u6811\n    if (rank_&#91;rx] &lt; rank_&#91;ry]) {\n        parent&#91;rx] = ry;\n    } else if (rank_&#91;rootX] > rank_&#91;rootY]) {\n        parent&#91;ry] = rx;\n    } else {\n        \/\/ \u79e9\u76f8\u7b49\uff0c\u5c06 rootY \u5408\u5e76\u5230 rootX\uff0c\u5e76\u589e\u52a0 rootX \u7684\u79e9\n        parent&#91;ry] = rx;\n        rank_&#91;rx]++;\n    }\n}<\/code><\/pre>\n\n\n\n<p>unite\u51fd\u6570\u8fd9\u6837\u4fee\u6539\u540e\u5230\u67e5\u8be2\u4e4b\u524dparent\u6570\u7ec4\u5df2\u7ecf\u5168\u90e8\u4e3a1\u4e86\uff0c\u8def\u5f84\u538b\u7f29\u6709\u65e0\u5728\u8fd9\u4e2a\u6837\u4f8b\u4e2d\u65e0\u63d0\u5347\u3002\u5bf9\u4e8e\u63d0\u4ea4\u9898\u76ee\u65f6\u7684\u590d\u6742\u6837\u4f8b\u7ec4\u6210\u6765\u8bf4\uff0c\u5c06\u8def\u5f84\u538b\u7f29\u548c\u6309\u79e9\u5408\u5e76\u7ed3\u5408\u5c06\u4f1a\u8fbe\u5230\u5e76\u67e5\u96c6\u7684<strong>\u7406\u8bba\u6700\u4f18<\/strong>\u65f6\u95f4\u590d\u6742\u5ea6<strong>O(<em>\u03b1<\/em>(<em>n<\/em>))<\/strong>\uff0c\u5176\u4e2d<em>\u03b1<\/em>(<em>n<\/em>)\u662f<strong>\u53cd\u963f\u514b\u66fc\u51fd\u6570<\/strong>\uff08Inverse Ackermann Function\uff09\uff0c\u963f\u514b\u66fc\u51fd\u6570\u672c\u8eab\u662f\u4e00\u4e2a\u201c\u602a\u517d\u7ea7\u201d\u7684\u51fd\u6570\uff0c\u589e\u957f\u901f\u5ea6\u6781\u5feb\uff0c\u5feb\u5230\u00a0<math xmlns=\"http:\/\/www.w3.org\/1998\/Math\/MathML\"><semantics><mrow><mi>A<\/mi><mo stretchy=\"false\">(<\/mo><mn>4<\/mn><mo separator=\"true\">,<\/mo><mn>3<\/mn><mo stretchy=\"false\">)<\/mo><\/mrow><\/semantics><\/math>\u00a0\u5c31\u5df2\u7ecf\u662f\u4e00\u4e2a\u5929\u6587\u6570\u5b57\u00a0<a href=\"https:\/\/leetcode.cn\/discuss\/post\/3149596\/jian-yi-shou-cang-leetcoderbing-cha-ji-m-udig\/\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a>\u3002\u56e0\u6b64\uff0c\u5b83\u7684\u53cd\u51fd\u6570\u00a0<math xmlns=\"http:\/\/www.w3.org\/1998\/Math\/MathML\"><semantics><mrow><mi>\u03b1<\/mi><mo stretchy=\"false\">(<\/mo><mi>n<\/mi><mo stretchy=\"false\">)<\/mo><\/mrow><\/semantics><\/math>\u00a0\u7684\u589e\u957f\u901f\u5ea6\u5c31<strong>\u6781\u6162<\/strong>\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u7ecf\u5178\u4f8b\u9898<\/h2>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u5e76\u67e5\u96c6\uff08Disjoint Set Union\uff0cDSU\uff09 1.\u5f15\u5165 \u5e76\u67e5\u96c6\uff08Disjoint Set Uni [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":156,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,15,12],"tags":[],"class_list":["post-316","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-article","category-algorithm-template","category-programming-algorithm-road"],"_links":{"self":[{"href":"https:\/\/jiangqvweihuan.cn\/index.php\/wp-json\/wp\/v2\/posts\/316","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jiangqvweihuan.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jiangqvweihuan.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jiangqvweihuan.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jiangqvweihuan.cn\/index.php\/wp-json\/wp\/v2\/comments?post=316"}],"version-history":[{"count":2,"href":"https:\/\/jiangqvweihuan.cn\/index.php\/wp-json\/wp\/v2\/posts\/316\/revisions"}],"predecessor-version":[{"id":325,"href":"https:\/\/jiangqvweihuan.cn\/index.php\/wp-json\/wp\/v2\/posts\/316\/revisions\/325"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/jiangqvweihuan.cn\/index.php\/wp-json\/wp\/v2\/media\/156"}],"wp:attachment":[{"href":"https:\/\/jiangqvweihuan.cn\/index.php\/wp-json\/wp\/v2\/media?parent=316"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jiangqvweihuan.cn\/index.php\/wp-json\/wp\/v2\/categories?post=316"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jiangqvweihuan.cn\/index.php\/wp-json\/wp\/v2\/tags?post=316"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}