{"id":2799,"date":"2022-03-08T15:25:00","date_gmt":"2022-03-08T07:25:00","guid":{"rendered":"http:\/\/139.9.1.231\/?p=2799"},"modified":"2022-03-08T15:25:01","modified_gmt":"2022-03-08T07:25:01","slug":"unnionset","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/03\/08\/unnionset\/","title":{"rendered":"\u5e76\u67e5\u96c6"},"content":{"rendered":"\n<h3>\u4e00\u3001\u6982\u5ff5\u53ca\u5176\u4ecb\u7ecd<\/h3>\n\n\n\n<p>\u5e76\u67e5\u96c6\u662f\u4e00\u79cd\u6811\u578b\u7684\u6570\u636e\u7ed3\u6784\uff0c\u7528\u4e8e\u5904\u7406\u4e00\u4e9b\u4e0d\u76f8\u4ea4\u96c6\u5408\u7684\u5408\u5e76\u53ca\u67e5\u8be2\u95ee\u9898\u3002<\/p>\n\n\n\n<p>\u5e76\u67e5\u96c6\u7684\u601d\u60f3\u662f\u7528\u4e00\u4e2a\u6570\u7ec4\u8868\u793a\u4e86\u6574\u7247\u68ee\u6797\uff08parent\uff09\uff0c\u6811\u7684\u6839\u8282\u70b9\u552f\u4e00\u6807\u8bc6\u4e86\u4e00\u4e2a\u96c6\u5408\uff0c\u6211\u4eec\u53ea\u8981\u627e\u5230\u4e86\u67d0\u4e2a\u5143\u7d20\u7684\u7684\u6811\u6839\uff0c\u5c31\u80fd\u786e\u5b9a\u5b83\u5728\u54ea\u4e2a\u96c6\u5408\u91cc\u3002<\/p>\n\n\n\n<h3>\u4e8c\u3001\u9002\u7528\u8bf4\u660e<\/h3>\n\n\n\n<p>\u5e76\u67e5\u96c6\u7528\u5728\u4e00\u4e9b\u6709&nbsp;<strong>N<\/strong>&nbsp;\u4e2a\u5143\u7d20\u7684\u96c6\u5408\u5e94\u7528\u95ee\u9898\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u662f\u5728\u5f00\u59cb\u65f6\u8ba9\u6bcf\u4e2a\u5143\u7d20\u6784\u6210\u4e00\u4e2a\u5355\u5143\u7d20\u7684\u96c6\u5408\uff0c\u7136\u540e\u6309\u4e00\u5b9a\u987a\u5e8f\u5c06\u5c5e\u4e8e\u540c\u4e00\u7ec4\u7684\u5143\u7d20\u6240\u5728\u7684\u96c6\u5408\u5408\u5e76\uff0c\u5176\u95f4\u8981\u53cd\u590d\u67e5\u627e\u4e00\u4e2a\u5143\u7d20\u5728\u54ea\u4e2a\u96c6\u5408\u4e2d\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u770b\u4f3c\u5e76\u4e0d\u590d\u6742\uff0c\u4f46\u6570\u636e\u91cf\u6781\u5927\uff0c\u82e5\u7528\u5176\u4ed6\u7684\u6570\u636e\u7ed3\u6784\u6765\u63cf\u8ff0\u7684\u8bdd\uff0c\u5f80\u5f80\u5728\u7a7a\u95f4\u4e0a\u8fc7\u5927\uff0c\u8ba1\u7b97\u673a\u65e0\u6cd5\u627f\u53d7\uff0c\u4e5f\u65e0\u6cd5\u5728\u77ed\u65f6\u95f4\u5185\u8ba1\u7b97\u51fa\u7ed3\u679c\uff0c\u6240\u4ee5\u53ea\u80fd\u7528\u5e76\u67e5\u96c6\u6765\u5904\u7406\u3002<\/p>\n\n\n\n<h3>\u4e09\u3001\u5e76\u67e5\u96c6\u7684\u57fa\u672c\u6570\u636e\u8868\u793a<\/h3>\n\n\n\n<figure class=\"wp-block-image is-style-default\"><img src=\"https:\/\/www.runoob.com\/wp-content\/uploads\/2020\/10\/union-01.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u5982\u4e0a\u56fe\u00a0<strong>0-4<\/strong>\u00a0\u4e0b\u9762\u90fd\u662f\u00a0<strong>0<\/strong>\uff0c<strong>5-9<\/strong>\u00a0\u4e0b\u9762\u90fd\u662f\u00a0<strong>1<\/strong>\uff0c\u8868\u793a\u00a0<strong>0\u30011\u30012\u30013\u30014<\/strong>\u00a0\u8fd9\u4e94\u4e2a\u5143\u7d20\u662f\u76f8\u8fde\u63a5\u7684\uff0c<strong>5\u30016\u30017\u30018\u30019<\/strong>\u00a0\u8fd9\u4e94\u4e2a\u5143\u7d20\u662f\u76f8\u8fde\u7684\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image is-style-default\"><img src=\"https:\/\/www.runoob.com\/wp-content\/uploads\/2020\/10\/union-02.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u518d\u5982\u4e0a\u56fe&nbsp;<strong>0\u30012\u30014\u30016\u30018<\/strong>&nbsp;\u4e0b\u9762\u90fd\u662f&nbsp;<strong>0<\/strong>&nbsp;\u8fd9\u4e2a\u96c6\u5408\uff0c\u8868\u793a&nbsp;<strong>0\u30012\u30014\u30016\u30018<\/strong>&nbsp;\u8fd9\u4e94\u4e2a\u5143\u7d20\u662f\u76f8\u8fde\u63a5\u7684\uff0c<strong>1\u30013\u30015\u30017\u30019<\/strong>&nbsp;\u4e0b\u9762\u90fd\u662f&nbsp;<strong>1<\/strong>&nbsp;\u8fd9\u4e2a\u96c6\u5408\uff0c\u8868\u793a&nbsp;<strong>0\uff0c1\u30013\u30015\u30017\u30019<\/strong>&nbsp;\u8fd9\u4e94\u4e2a\u5143\u7d20\u662f\u76f8\u8fde\u7684\u3002<\/p>\n\n\n\n<p>\u6784\u9020\u4e00\u4e2a\u7c7b UnionFind\uff0c\u521d\u59cb\u5316, \u6bcf\u4e00\u4e2aid[i]\u6307\u5411\u81ea\u5df1, \u6ca1\u6709\u5408\u5e76\u7684\u5143\u7d20\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public UnionFind1(int n) {\r\n        count = n;\r\n        id = new int&#91;n];\r\n        \/\/ \u521d\u59cb\u5316, \u6bcf\u4e00\u4e2aid&#91;i]\u6307\u5411\u81ea\u5df1, \u6ca1\u6709\u5408\u5e76\u7684\u5143\u7d20\r\n        for (int i = 0; i &lt; n; i++)\r\n            id&#91;i] = i;\r\n    }\n<\/code><\/pre>\n\n\n\n<h2 id=\"_2\">\u67e5\u627e<a href=\"https:\/\/oi-wiki.org\/ds\/dsu\/#_2\">\u00b6<\/a><\/h2>\n\n\n\n<p>\u901a\u4fd7\u5730\u8bb2\u4e00\u4e2a\u6545\u4e8b\uff1a\u51e0\u4e2a\u5bb6\u65cf\u8fdb\u884c\u5bb4\u4f1a\uff0c\u4f46\u662f\u5bb6\u65cf\u666e\u904d\u957f\u5bff\uff0c\u6240\u4ee5\u4eba\u6570\u4f17\u591a\u3002\u7531\u4e8e\u957f\u65f6\u95f4\u7684\u5206\u79bb\u4ee5\u53ca\u5e74\u9f84\u7684\u589e\u957f\uff0c\u8fd9\u4e9b\u4eba\u9010\u6e10\u5fd8\u6389\u4e86\u81ea\u5df1\u7684\u4eb2\u4eba\uff0c\u53ea\u8bb0\u5f97\u81ea\u5df1\u7684\u7238\u7238\u662f\u8c01\u4e86\uff0c\u800c\u6700\u957f\u8005\uff08\u79f0\u4e3a\u300c\u7956\u5148\u300d\uff09\u7684\u7236\u4eb2\u5df2\u7ecf\u53bb\u4e16\uff0c\u4ed6\u53ea\u77e5\u9053\u81ea\u5df1\u662f\u7956\u5148\u3002\u4e3a\u4e86\u786e\u5b9a\u81ea\u5df1\u662f\u54ea\u4e2a\u5bb6\u65cf\uff0c\u4ed6\u4eec\u60f3\u51fa\u4e86\u4e00\u4e2a\u529e\u6cd5\uff0c\u53ea\u8981\u95ee\u81ea\u5df1\u7684\u7238\u7238\u662f\u4e0d\u662f\u7956\u5148\uff0c\u4e00\u5c42\u4e00\u5c42\u7684\u5411\u4e0a\u95ee\uff0c\u76f4\u5230\u95ee\u5230\u7956\u5148\u3002\u5982\u679c\u8981\u5224\u65ad\u4e24\u4eba\u662f\u5426\u5728\u540c\u4e00\u5bb6\u65cf\uff0c\u53ea\u8981\u770b\u4e24\u4eba\u7684\u7956\u5148\u662f\u4e0d\u662f\u540c\u4e00\u4eba\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n\n\n\n<p>\u5728\u8fd9\u6837\u7684\u601d\u60f3\u4e0b\uff0c\u5e76\u67e5\u96c6\u7684\u67e5\u627e\u7b97\u6cd5\u8bde\u751f\u4e86\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Python Version\r\nfa = &#91;0] * MAXN # \u8bb0\u5f55\u67d0\u4e2a\u4eba\u7684\u7238\u7238\u662f\u8c01\uff0c\u7279\u522b\u89c4\u5b9a\uff0c\u7956\u5148\u7684\u7238\u7238\u662f\u4ed6\u81ea\u5df1\r\n\r\n# \u9012\u5f52\r\ndef find(x):\r\n    # \u5bfb\u627ex\u7684\u7956\u5148\r\n    if fa&#91;x] == x:\r\n        return x # \u5982\u679cx\u662f\u7956\u5148\u5219\u8fd4\u56de\r\n    else:\r\n        return find(fa&#91;x]) # \u5982\u679c\u4e0d\u662f\u5219 x \u7684\u7238\u7238\u95ee x \u7684\u7237\u7237\r\n\r\n# \u975e\u9012\u5f52\r\ndef find(x):\r\n    while x != fa&#91;x]: # \u5982\u679c x \u4e0d\u662f\u7956\u5148\uff0c\u5c31\u4e00\u76f4\u5f80\u4e0a\u4e00\u8f88\u627e\r\n        x = fa&#91;x]\r\n    return x # \u5982\u679c x \u662f\u7956\u5148\u5219\u8fd4\u56de<\/code><\/pre>\n\n\n\n<p>       \u8fd9\u6837\u7684\u786e\u53ef\u4ee5\u8fbe\u6210\u76ee\u7684\uff0c\u4f46\u662f\u663e\u7136\u6548\u7387\u5b9e\u5728\u592a\u4f4e\u3002\u4e3a\u4ec0\u4e48\u5462\uff1f\u56e0\u4e3a\u6211\u4eec\u4f7f\u7528\u4e86\u592a\u591a\u6ca1\u7528\u7684\u4fe1\u606f\uff0c\u6211\u7684\u7956\u5148\u662f\u8c01\u4e0e\u6211\u7236\u4eb2\u662f\u8c01\u6ca1\u4ec0\u4e48\u5173\u7cfb\uff0c\u8fd9\u6837\u4e00\u5c42\u4e00\u5c42\u627e\u592a\u6d6a\u8d39\u65f6\u95f4\uff0c\u4e0d\u5982\u6211\u76f4\u63a5\u5f53\u7956\u5148\u7684\u513f\u5b50\uff0c\u95ee\u4e00\u6b21\u5c31\u53ef\u4ee5\u51fa\u7ed3\u679c\u4e86\u3002\u751a\u81f3\u7956\u5148\u662f\u8c01\u90fd\u65e0\u6240\u8c13\uff0c\u53ea\u8981\u8fd9\u4e2a\u4eba\u53ef\u4ee5\u4ee3\u8868\u6211\u4eec\u5bb6\u65cf\u5c31\u80fd\u5f97\u5230\u60f3\u8981\u7684\u6548\u679c\u3002<strong>\u628a\u5728\u8def\u5f84\u4e0a\u7684\u6bcf\u4e2a\u8282\u70b9\u90fd\u76f4\u63a5\u8fde\u63a5\u5230\u6839\u4e0a<\/strong>\uff0c\u8fd9\u5c31\u662f\u8def\u5f84\u538b\u7f29\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># Python Version\ndef find(x):\n    if x != fa[x]: # x \u4e0d\u662f\u81ea\u8eab\u7684\u7236\u4eb2\uff0c\u5373 x \u4e0d\u662f\u8be5\u96c6\u5408\u7684\u4ee3\u8868\n        fa[x] = find(fa[x]) # \u67e5\u627e x \u7684\u7956\u5148\u76f4\u5230\u627e\u5230\u4ee3\u8868\uff0c\u4e8e\u662f\u987a\u624b\u8def\u5f84\u538b\u7f29\n    return fa[x]<\/pre>\n\n\n\n<h2 id=\"_4\">\u5408\u5e76<a href=\"https:\/\/oi-wiki.org\/ds\/dsu\/#_4\">\u00b6<\/a><\/h2>\n\n\n\n<p>\u5bb4\u4f1a\u4e0a\uff0c\u4e00\u4e2a\u5bb6\u65cf\u7684\u7956\u5148\u7a81\u7136\u5bf9\u53e6\u4e00\u4e2a\u5bb6\u65cf\u8bf4\uff1a\u6211\u4eec\u4e24\u4e2a\u5bb6\u65cf\u4ea4\u60c5\u8fd9\u4e48\u597d\uff0c\u4e0d\u5982\u5408\u6210\u4e00\u5bb6\u597d\u4e86\u3002\u53e6\u4e00\u4e2a\u5bb6\u65cf\u4e5f\u6b23\u7136\u63a5\u53d7\u4e86\u3002<br>\u6211\u4eec\u4e4b\u524d\u8bf4\u8fc7\uff0c\u5e76\u4e0d\u5728\u610f\u7956\u5148\u7a76\u7adf\u662f\u8c01\uff0c\u6240\u4ee5\u53ea\u8981\u5176\u4e2d\u4e00\u4e2a\u7956\u5148\u53d8\u6210\u53e6\u4e00\u4e2a\u7956\u5148\u7684\u513f\u5b50\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># Python Version\ndef unionSet(x, y):\n    # x \u4e0e y \u6240\u5728\u5bb6\u65cf\u5408\u5e76\n    x = find(x)\n    y = find(y)\n    fa[x] = y # \u628a x \u7684\u7956\u5148\u53d8\u6210 y \u7684\u7956\u5148\u7684\u513f\u5b50<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u6982\u5ff5\u53ca\u5176\u4ecb\u7ecd \u5e76\u67e5\u96c6\u662f\u4e00\u79cd\u6811\u578b\u7684\u6570\u636e\u7ed3\u6784\uff0c\u7528\u4e8e\u5904\u7406\u4e00\u4e9b\u4e0d\u76f8\u4ea4\u96c6\u5408\u7684\u5408\u5e76\u53ca\u67e5\u8be2\u95ee\u9898\u3002 \u5e76\u67e5\u96c6\u7684\u601d\u60f3\u662f\u7528\u4e00\u4e2a\u6570 &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/03\/08\/unnionset\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u5e76\u67e5\u96c6<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2799"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=2799"}],"version-history":[{"count":10,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2799\/revisions"}],"predecessor-version":[{"id":2852,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2799\/revisions\/2852"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=2799"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=2799"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=2799"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}