{"id":1353,"date":"2024-02-05T22:55:45","date_gmt":"2024-02-05T14:55:45","guid":{"rendered":"http:\/\/www.forillusion.com\/?p=1353"},"modified":"2025-02-14T11:39:10","modified_gmt":"2025-02-14T03:39:10","slug":"union-and-find","status":"publish","type":"post","link":"https:\/\/www.forillusion.com\/index.php\/union-and-find\/","title":{"rendered":"\u5e76\u67e5\u96c6"},"content":{"rendered":"\n<p><div class=\"has-toc have-toc\"><\/div><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u6a21\u7248<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include&lt;bits\/stdc++.h&gt;\nusing namespace std;\nint n,m,k,x,y;\nint pre&#91;200005];\n\nint find(int x)\n   \/\/\u67e5\u627e\u6839\n{\n    if (x!=pre&#91;x]) pre&#91;x]=find(pre&#91;x]);\n    return pre&#91;x];\n}\n\nvoid merge(int a,int b)\n   \/\/\u5408\u5e76\n{\n    int x=find(a);\n    int y=find(b);\n    pre&#91;x]=y;\n}\n\nint main()\n{\n    cin&gt;&gt;n&gt;&gt;m;\n    for (int i=1;i&lt;=n;i++)\n        pre&#91;i]=i;\n    for (int i=1;i&lt;=m;i++)\n    {\n        cin&gt;&gt;k&gt;&gt;x&gt;&gt;y;\n        if (k==1) merge(x,y);\n        if (k==2) \n        {\n            int x0=find(x);\n            int y0=find(y);\n            if (x0==y0) cout&lt;&lt;\"Y\"&lt;&lt;endl;\n            else cout&lt;&lt;\"N\"&lt;&lt;endl;\n        }\n    }\n    return 0;\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\u91ca<\/h2>\n\n\n\n<p>\u5e76\u67e5\u96c6\u662f\u53ef\u4ee5\u5feb\u901f\u5b8c\u6210\u5224\u65ad\u4e24\u4e2a\u5143\u7d20\u662f\u5426\u5728\u540c\u4e00\u96c6\u5408\u6216\u8005\u5408\u5e76\u4e24\u4e2a\u96c6\u5408\u7684\u4e00\u79cd\u6570\u636e\u7ed3\u6784\u3002<\/p>\n\n\n\n<p>\u5c06\u6bcf\u4e00\u4e2a\u5143\u7d20\u90fd\u89c6\u4e3a\u6811\u4e0a\u7684\u4e00\u4e2a\u8282\u70b9\uff0c\u6bcf\u4e00\u4e2a\u96c6\u5408\u5c31\u662f\u4e00\u68f5\u72ec\u7acb\u7684\u6811\uff0c\u6307\u5b9a\u4e00\u4e2a\u96c6\u5408\u4e2d\u7684\u4efb\u610f\u4e00\u4e2a\u5143\u7d20\u4e3a\u6839\uff0c\u5c06\u5176\u4ed6\u5143\u7d20\u4ee5\u6811\u7684\u5f62\u5f0f\u8fde\u63a5\u5230\u8fd9\u4e2a\u6839\u4e0a\u3002\u540c\u65f6\u6811\u4e2d\u7684\u6bcf\u4e00\u4e2a\u8282\u70b9\u90fd\u5b58\u50a8\u7740\u4e00\u4e2a\u6307\u9488\u6307\u5411\u4ed6\u7684\u7236\u8282\u70b9\u3002\u800c\u6839\u8282\u70b9\u7684\u7236\u8282\u70b9\u6307\u5411\u81ea\u5df1\u3002<\/p>\n\n\n\n<p>\u5e76\u67e5\u96c6\u6709\u4e24\u79cd\u57fa\u7840\u64cd\u4f5c\uff0c\u67e5\u627e\u6839\u548c\u5408\u5e76\u96c6\u5408\u3002<\/p>\n\n\n\n<p>\u67e5\u627e\u4e00\u4e2a\u5143\u7d20\u7684\u6839\u65f6\uff0c\u56e0\u4e3a\u6bcf\u4e00\u4e2a\u5143\u7d20\u5c31\u662f\u6811\u4e0a\u7684\u4e00\u4e2a\u8282\u70b9\uff0c\u800c\u8282\u70b9\u8bb0\u5f55\u7740\u81ea\u5df1\u7684\u7236\u8282\u70b9\uff0c\u6240\u4ee5\u53ef\u4ee5\u5229\u7528\u9012\u5f52\u987a\u7740\u8bb0\u5f55\u7684\u7236\u8282\u70b9\u4e0d\u65ad\u5411\u4e0a\u67e5\u627e\uff0c\u76f4\u5230\u627e\u5230\u4e00\u4e2a\u8282\u70b9\u5176\u7236\u8282\u70b9\u5c31\u662f\u4ed6\u81ea\u5df1\uff0c\u8fd9\u4e2a\u8282\u70b9\u5c31\u662f\u6839\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int find(int x)   \/\/\u67e5\u627e\u6839\n{\n    if (x!=pre&#91;x]) return find(pre&#91;x]);\n    else return x;\n}<\/code><\/pre>\n\n\n\n<p>\u800c\u5408\u5e76\u4e24\u4e2a\u96c6\u5408\u65f6\uff0c\u4e5f\u5c31\u662f\u9700\u8981\u5c06\u5176\u4e2d\u4e00\u4e2a\u96c6\u5408\u7684\u6811\u8fde\u63a5\u5230\u5230\u53e6\u4e00\u4e2a\u96c6\u5408\u7684\u6811\u4e0a\uff0c\u4e8e\u662f\u67e5\u627e\u5230\u5176\u4e2d\u4e00\u4e2a\u6811\u7684\u6839\uff0c\u5e76\u5c06\u8fd9\u4e2a\u6839\u7684\u7236\u8282\u70b9\u6307\u5411\u53e6\u4e00\u4e2a\u6811\u4e0a\u7684\u4efb\u610f\u5143\u7d20\u3002<\/p>\n\n\n\n<p>\u4e5f\u5c31\u662f\u8bf4\uff0c\u5408\u5e76\u96c6\u5408\u662f\u5728\u67e5\u627e\u6839\u7684\u57fa\u7840\u4e0a\u5b8c\u6210\u7684\u3002\u800c\u67e5\u627e\u6839\u65f6\uff0c\u9012\u5f52\u7684\u6df1\u5ea6\u662f\u7531\u5f53\u524d\u8282\u70b9\u5230\u6811\u6839\u7684\u8ddd\u79bb\u51b3\u5b9a\u7684\uff0c\u800c\u5e76\u4e0d\u5173\u7cfb\u9014\u4e2d\u7ecf\u8fc7\u4e86\u54ea\u4e9b\u8282\u70b9\uff0c\u5373\u8282\u70b9\u8ddd\u79bb\u6811\u6839\u8d8a\u8fd1\uff0c\u9012\u5f52\u6df1\u5ea6\u8d8a\u4f4e\u3002\u90a3\u4e48\u5c06\u6bcf\u4e00\u4e2a\u8282\u70b9\u90fd\u76f4\u63a5\u8fde\u63a5\u5728\u6811\u6839\u4e0a\uff0c\u8fd9\u662f\u67e5\u627e\u6811\u6839\u7684\u901f\u5ea6\u5c06\u662f\u6700\u5feb\u7684\u3002\u4e8e\u662f\u5728\u8fdb\u884c\u5408\u5e76\u64cd\u4f5c\u65f6\uff0c\u4f1a\u540c\u65f6\u627e\u5230\u8981\u5408\u5e76\u7684\u4e24\u68f5\u6811\u7684\u6839\uff0c\u5e76\u5c06\u5176\u4e2d\u7684\u4e00\u4e2a\u6839\u7684\u7236\u8282\u70b9\u76f4\u63a5\u6307\u5411\u53e6\u4e00\u4e2a\u6839\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"cpp\">void merge(int a,int b)\n   \/\/\u5408\u5e76\n{\n    int x=find(a);\n    int y=find(b);\n    pre&#91;x]=y;\n}<\/code><\/pre>\n\n\n\n<p>\u4f46\u8fd9\u6837\u505a\u5e76\u4e0d\u662f\u6700\u5feb\u7684\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7<strong>\u8def\u5f84\u538b\u7f29<\/strong>\u4f7f\u5f97\u64cd\u4f5c\u66f4\u5feb\u4e00\u70b9\u3002\u5728\u5229\u7528\u9012\u5f52\u67e5\u627e\u6839\u7684\u540c\u65f6\uff0c\u5f53\u627e\u5230\u6839\u540e\uff0c\u6839\u7684\u4e0b\u6807\u4f1a\u88ab\u4e00\u8def\u4ee5\u8fd4\u56de\u7ed9\u4e0a\u4e00\u5c42\u51fd\u6570\uff0c\u5373\u67e5\u627e\u8fc7\u7a0b\u4e2d\u7ecf\u8fc7\u7684\u5b50\u8282\u70b9\uff0c\u8fd9\u662f\u5c31\u53ef\u4ee5\u5c06\u9014\u7ecf\u7684\u6240\u6709\u8282\u70b9\u7684\u7236\u8282\u70b9\u90fd\u6307\u5411\u6839\uff0c\u4e5f\u5c31\u53ef\u4ee5\u4f7f\u5f97\u66f4\u591a\u7684\u5143\u7d20\u76f4\u63a5\u8fde\u63a5\u5728\u6839\u4e0a\uff0c\u8fd9\u6837\u5c31\u5b8c\u6210\u4e86\u5bf9\u6811\u7684\u538b\u7f29\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"cpp\">int find(int x)\n   \/\/\u67e5\u627e\u6839\n{\n    if (x!=pre&#91;x]) pre&#91;x]=find(pre&#91;x]);\n    return pre&#91;x];\n}<\/code><\/pre>\n\n\n\n<p>\u6240\u4ee5\u5982\u679c\u60f3\u8981\u5224\u65ad\u4e24\u4e2a\u5143\u7d20\u662f\u5426\u5c5e\u4e8e\u540c\u4e00\u96c6\u5408\u65f6\uff0c\u53ea\u9700\u8981\u5206\u522b\u67e5\u627e\u8fd9\u4e24\u4e2a\u5143\u7d20\u6240\u5728\u6811\u7684\u6839\uff0c\u5224\u65ad\u6839\u662f\u5426\u4e00\u81f4\u5373\u53ef\u3002\u5982\u679c\u5904\u4e8e\u4e0d\u540c\u96c6\u5408\uff0c\u5219\u6839\u4e0d\u540c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"cpp\">int x0=find(x);\nint y0=find(y);\nif (x0==y0) cout&lt;&lt;\"Y\"&lt;&lt;endl;\nelse cout&lt;&lt;\"N\"&lt;&lt;endl;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6a21\u7248 \u89e3\u91ca \u5e76\u67e5\u96c6\u662f\u53ef\u4ee5\u5feb\u901f\u5b8c\u6210\u5224\u65ad\u4e24\u4e2a\u5143\u7d20\u662f\u5426\u5728\u540c\u4e00\u96c6\u5408\u6216\u8005\u5408\u5e76\u4e24\u4e2a\u96c6\u5408\u7684\u4e00\u79cd\u6570\u636e\u7ed3\u6784\u3002 \u5c06\u6bcf\u4e00\u4e2a\u5143\u7d20\u90fd\u89c6\u4e3a\u6811\u4e0a\u7684\u4e00\u4e2a\u8282\u70b9\uff0c\u6bcf\u4e00 &#8230;<\/p>","protected":false},"author":1,"featured_media":1355,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,35],"tags":[43,12,34,22],"class_list":["post-1353","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-3","category-35","tag-43","tag-12","tag-34","tag-22"],"_links":{"self":[{"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/posts\/1353","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/comments?post=1353"}],"version-history":[{"count":1,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/posts\/1353\/revisions"}],"predecessor-version":[{"id":1732,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/posts\/1353\/revisions\/1732"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/media\/1355"}],"wp:attachment":[{"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/media?parent=1353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/categories?post=1353"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/tags?post=1353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}