{"id":1057,"date":"2020-01-12T00:02:41","date_gmt":"2020-01-11T16:02:41","guid":{"rendered":"http:\/\/47.101.202.111\/?p=1057"},"modified":"2023-04-07T15:18:02","modified_gmt":"2023-04-07T07:18:02","slug":"%e7%ae%97%e6%b3%95-%e5%b9%b6%e6%9f%a5%e9%9b%86-union-find","status":"publish","type":"post","link":"http:\/\/139.196.114.170\/?p=1057","title":{"rendered":"\u9ad8\u6548\u89e3\u51b3\u56fe\u8fde\u901a\u6027 &#8211; \u5e76\u67e5\u96c6 Union Find"},"content":{"rendered":"<h3>\u5e76\u67e5\u96c6\u7684\u7528\u5904 &#8211; \u5206\u7ec4\u548c\u67e5\u8be2\u5206\u7ec4<\/h3>\n<p>\u5e76\u67e5\u96c6\u53ef\u4ee5\u5feb\u901f\u5730\u5206\u7ec4\u548c\u67e5\u8be2\u5206\u7ec4\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u8fd1\u4f3c\u5e38\u6570\uff0c\u901a\u5e38\u7528\u4e8e\u89e3\u51b3\u56fe\u7684 <strong>\u8fde\u901a\u6027<\/strong> \u95ee\u9898\u3002\u5206\u7ec4\u65f6\u4f7f\u7528 <strong>\u5e76\u64cd\u4f5c<\/strong> \u5c06\u5143\u7d20\u5212\u4e3a\u4e00\u7ec4\u6216\u52a0\u5165\u540c\u4e00\u4e2a\u8fde\u901a\u5206\u91cf\uff0c\u67e5\u8be2\u7ec4\u522b\u6216\u6240\u5c5e\u8fde\u901a\u5b50\u56fe\u65f6 \u4f7f\u7528 <strong>\u67e5\u64cd\u4f5c<\/strong> \u8fd4\u56de\u7ec4\u4e2d<\/p>\n<h4>\u5355\u8282\u70b9 &#8211; \u5355\u8282\u70b9\u8fde\u901a\u6027<\/h4>\n<p>\u901a\u8fc7\u5206\u7ec4\u5c06\u8054\u901a\u5206\u91cf\u5206\u4e3a\u540c\u4e00\u7ec4<br \/>\n\u68c0\u6d4b\u5927\u578b\u7535\u8def\u4e2d\u4e24\u4e2a\u70b9\u662f\u5426\u8fde\u901a<br \/>\n\u68c0\u6d4b\u7f51\u7edc\u4e2d\u4e24\u4e2a\u8282\u70b9\u662f\u5426\u53ef\u8fbe\u5230<br \/>\n\u68c0\u6d4b\u793e\u4ea4\u7f51\u7edc\u4e2d\u4e24\u4eba\u662f\u5426\u6709\u5173\u7cfb<\/p>\n<h4>\u591a\u8282\u70b9 &#8211; \u591a\u8282\u70b9\u8fde\u901a\u6027<\/h4>\n<p>\u5728\u591a\u8282\u70b9\u7aef\u5916\u989d\u5916\u6dfb\u52a0\u4e00\u4e2a\u8282\u70b9\u4f5c\u4e3a\u6839\u8282\u70b9<br \/>\n\u5c06\u591a\u8282\u70b9\u4e0e\u6839\u8282\u70b9\u5206\u4e3a\u4e00\u7ec4<br \/>\n\u904d\u5386\u56fe\u5c06\u8054\u901a\u5206\u91cf\u5206\u4e3a\u540c\u4e00\u7ec4<br \/>\n\u4ec5\u9700\u68c0\u6d4b\u4e24\u7aef\u989d\u5916\u6dfb\u52a0\u7684<strong>\u6839\u8282\u70b9\u662f\u5426\u540c\u4e00\u7ec4\u522b<\/strong><\/p>\n<h3>\u5e76\u67e5\u96c6\u7684\u8868\u793a<\/h3>\n<pre lang=\"java\">\nabstract class UF {\n    protected int id[];                         \/\/ \u7ec4\u6807\u8bc6\n    protected int sz[];                         \/\/ \u5143\u7d20\u6570\n    protected int count;                        \/\/ \u603b\u7ec4\u6570\n    public abstract int find(int i);            \/\/ \u67e5\u64cd\u4f5c\n    public abstract void union(int p, int p);   \/\/ \u5e76\u64cd\u4f5c\n    public boolean connected(int p, int q) {    \/\/ \u5224\u8fde\u63a5\n        return find(p) == find(q);\n    }\n    public UF(int N){                   \/\/ \u521d\u59cb\u5316\u4e3aN\u4e2a\u7ec4\u522b\n        count = N;\n        id = new int[N];\n        sz = new int[N];\n        for(int i = 0; i < N; i++) {\n            id[i] = i;\n            sz[i] = 1;\n        }\n    }\n}\n<\/pre>\n<h3>\u5e76\u67e5\u96c6\u7684\u5b9e\u73b0<\/h3>\n<h4>\u5feb\u901f\u67e5\u627e Quick Find - \u65b0\u52a0\u8282\u70b9id\u4f5c\u4e3a\u7ec4\u6807\u8bc6<\/h4>\n<p><strong>\u8868\u793a\u6cd5<\/strong> \u6bcf\u4e2a\u5143\u7d20\u5404\u81ea\u5b58\u50a8\u6240\u5c5e\u7684\u7ec4id\uff0c\u540c\u4e00\u5206\u7ec4\u7684\u7ec4id\u76f8\u540c<br \/>\n<strong>\u5e76\u64cd\u4f5c<\/strong> \u628a\u5bb9\u7eb3\u7ec4\u8282\u70b9\u5b58\u50a8\u7684\u7ec4\u6807\u8bc6\u4e00\u4e00\u4fee\u6539\u4e3a\u65b0\u52a0\u8282\u70b9\u7684id<br \/>\n<strong>\u67e5\u64cd\u4f5c<\/strong> \u8bfb\u53d6\u5143\u7d20\u5b58\u50a8\u7684\u7ec4\u6807\u8bc6id<br \/>\n<strong>\u590d\u6742\u5ea6<\/strong> \u5e76\u64cd\u4f5c<code class=\"katex-inline\">\\scriptsize O(n)<\/code>\uff0c\u67e5\u64cd\u4f5c<code class=\"katex-inline\">\\scriptsize O(1)<\/code><\/p>\n<pre><code class=\"language-java\">class QuickFindUF extends UF {\n    @Override\n    public int find(int i) {\n        return id[i];\n    }\n    @Override\n    public void union(int p, int q){\n        int pId = find(p);\n        int qId = find(q);\n        if(pId == qId) return;\n        for(int i = 0; i &lt; id.length; i++)\n            if(id[i] == pId) id[i] = qId;\n        count--;\n    }\n}<\/code><\/pre>\n<h4>\u5feb\u901f\u5408\u5e76 Quick Union - \u6839\u8282\u70b9id\u4f5c\u4e3a\u7ec4\u6807\u8bc6<\/h4>\n<p><strong>\u8868\u793a\u6cd5<\/strong> \u4ec5\u7528\u6839\u8282\u70b9id\u552f\u4e00\u6807\u8bc6\u7ec4\u522b\uff0c\u5b50\u8282\u70b9\u94fe\u63a5\u6839\u8282\u70b9\u6765\u8bfb\u53d6\u6807\u8bc6<br \/>\n<strong>\u5e76\u64cd\u4f5c<\/strong> \u5408\u5e76\u4e24\u8282\u70b9\u5b9e\u9645\u662f\u5408\u5e76\u4e24\u4e2a\u7ec4\uff0c\u5373\u5c06\u5e76\u5165\u7ec4\u6839\u8282\u70b9\u6210\u4e3a\u5bb9\u7eb3\u7ec4\u6839\u8282\u70b9\u7684\u5b50\u6811<br \/>\n<strong>\u67e5\u64cd\u4f5c<\/strong> \u6839\u636e\u94fe\u63a5\u67e5\u8be2\u81f3\u6839\u8282\u70b9\uff0c\u8bfb\u53d6\u7ec4\u6807\u8bc6id<br \/>\n<strong>\u590d\u6742\u5ea6<\/strong> \u5e76\u67e5\u64cd\u4f5c\u5747\u4e3a <code class=\"katex-inline\">\\scriptsize O(height(tree))<\/code><\/p>\n<pre><code class=\"language-java\">class QuickUnionUF extends UF {\n    @Override\n    public int find(int i) {\n        if(i == id[i]) return i;\n        return find(id[i]);\n    }\n    @Override\n    public void union(int p, int q){\n        int pRoot = find(p);\n        int qRoot = find(q);\n        if(pRoot == qRoot) return;\n        id[qRoot] = pRoot;\n        count--;\n    }\n}<\/code><\/pre>\n<h4>\u5e26\u6743\u5feb\u901f\u5408\u5e76 Weighted Quick Union - \u5c0f\u6811\u6210\u5b50\u6811<\/h4>\n<p><strong>\u8868\u793a\u6cd5<\/strong> \u7ef4\u62a4\u6811\u7684\u5bb9\u91cf\u5b57\u6bb5\uff0c\u8ba9\u5c0f\u89c4\u6a21\u6811\u6210\u4e3a\u5927\u89c4\u6a21\u6811\u7684\u5b50\u8282\u70b9\uff0c\u9632\u6b62\u6811\u8fc7\u9ad8\u800c\u964d\u4f4e\u67e5\u8be2\u94fe\u63a5\u901f\u5ea6<br \/>\n<strong>\u5e76\u64cd\u4f5c<\/strong> \u6539\u8fdb\u5feb\u901f\u5408\u5e76\uff0c\u5c06\u5c0f\u6811\u6839\u8282\u70b9\u6210\u4e3a\u5927\u6811\u6839\u8282\u70b9\u7684\u5b50\u8282\u70b9\uff0c\u540c\u65f6\u4fee\u6539\u6811\u7684\u5927\u5c0f<br \/>\n<strong>\u67e5\u64cd\u4f5c<\/strong> \u540c\u5feb\u901f\u5408\u5e76\uff0c\u94fe\u63a5\u67e5\u8be2\u81f3\u6839\u8282\u70b9<br \/>\n<strong>\u590d\u6742\u5ea6<\/strong> \u5e76\u67e5\u64cd\u4f5c\u5747 <code class=\"katex-inline\">\\scriptsize O(\\log n)<\/code><\/p>\n<pre><code class=\"language-java\">class WeightedQuickUnionUF extends UF {\n    @Override\n    public int find(int i) {\n        if(i == id[i]) return i;\n        return find(id[i]);\n    }\n    @Override\n    public void union(int p, int q){\n        int pRoot = find(p);\n        int qRoot = find(q);\n        if(pRoot == qRoot) return;\n        if(sz[pRoot] &lt; sz[qRoot]) {\n            id[pRoot] =  qRoot;\n            sz[qRoot] += sz[pRoot];\n        }\n        else {\n            id[qRoot] =  pRoot;\n            sz[pRoot] += sz[qRoot];\n        }\n        count--;\n    }\n}<\/code><\/pre>\n<h4>\u8def\u5f84\u538b\u7f29\u5feb\u901f\u5408\u5e76 Path Compression Quick Union - \u76f4\u63a5\u6302\u6811\u6839<\/h4>\n<p><strong>\u8868\u793a\u6cd5<\/strong> \u5feb\u901f\u5408\u5e76\u7684\u6539\u8fdb\u3002\u67e5\u64cd\u4f5c\u68c0\u7d22\u6839\u8282\u70b9\u540c\u65f6\uff0c\u5c06\u6240\u6709\u88ab\u68c0\u7d22\u8282\u70b9\u6307\u5411\u6839\u8282\u70b9\uff0c\u51cf\u5c0f\u6811\u9ad8\u5ea6<br \/>\n<strong>\u5e76\u64cd\u4f5c<\/strong> \u540c\u5feb\u901f\u5408\u5e76<br \/>\n<strong>\u67e5\u64cd\u4f5c<\/strong> \u540c\u5feb\u901f\u5408\u5e76\uff0c\u533a\u522b\u662f\u5411\u4e0a\u68c0\u7d22\u8282\u70b9\u65f6\uff0c\u5c06\u6bcf\u4e2a\u8282\u70b9\u90fd\u6307\u5411\u6839\u8282\u70b9<br \/>\n<strong>\u590d\u6742\u5ea6<\/strong> \u5e76\u67e5\u64cd\u4f5c\u5747\u63a5\u8fd1 <code class=\"katex-inline\">\\scriptsize O(1)<\/code><\/p>\n<pre><code class=\"language-java\">class WeightedQuickUnionUF extends UF {\n    @Override\n    public int find(int i) {\n        if(i == id[i]) return i;\n        return id[i] = find(id[i]);\n    }\n    @Override\n    public void union(int p, int q){\n        int pRoot = find(p);\n        int qRoot = find(q);\n        if(pRoot == qRoot) return;\n        id[qRoot] = pRoot;\n        count--;\n    }\n}<\/code><\/pre>\n<h4>\u8def\u5f84\u538b\u7f29\u7684\u5e26\u6743\u5feb\u901f\u5408\u5e76 Weighted Path Compression Quick Union<\/h4>\n<p>\u7ed3\u5408\u5e26\u6743\u548c\u8def\u5f84\u538b\u7f29\u4e24\u79cd\u4f18\u70b9\uff0c\u6709\u6548\u63a7\u5236\u6811\u7684\u9ad8\u5ea6\uff0c\u4ece\u800c\u5e76\u67e5\u64cd\u4f5c\u6027\u80fd\u903c\u8fd1 <code class=\"katex-inline\">\\scriptsize O(1)<\/code><\/p>\n<pre><code class=\"language-java\">class WPCQuickUnion extends UF{\n    @Override\n    public int find(int i) {\n        if(i == id[i]) return i;\n        return id[i] = find(id[i]);\n    }\n    @Override\n    public void union(int p, int q) {\n        int pRoot = find(p);\n        int qRoot = find(q);\n        if (pRoot == qRoot) return;\n        if (sz[pRoot] &lt; sz[qRoot]) {\n            id[pRoot] = qRoot;\n            sz[qRoot] += sz[pRoot];\n        } else {\n            id[qRoot] = pRoot;\n            sz[pRoot] += sz[qRoot];\n        }\n    }\n}<\/code><\/pre>\n<h3>\u5e94\u7528\u6848\u4f8b<\/h3>\n<p>\u2708<a href=\"http:\/\/poj.org\/problem?id=1703\">POJ1703<\/a><\/p>\n<pre><code class=\"language-java\">\/**\n * ---------------------------------------------------------------\n * - \u5b9e\u73b0\u601d\u8def\n *      \u5df1\u65b9\u548c\u5bf9\u65b9\u5206\u4e3a\u4e24\u7ec4 -&gt; \u5df1\u65b9\u548c\u5bf9\u65b9\u7684\u5bf9\u65b9\u5206\u4e3a\u4e00\u7ec4\n *      \u4f7f\u7528\u8def\u5f84\u538b\u7f29\u7684\u5e26\u6743\u5feb\u901f\u5408\u5e76\n * - \u5b9e\u73b0\u7ec6\u8282\n *      \u521d\u59cb\u6bcf\u4e2a\u8282\u70b9\u4ee5id\u4e3a\u7ec4\u6807\u8bc6\u7b26\u81ea\u6210\u4e00\u7ec4\uff0c\u989d\u5916\u5185\u5b58op\u4fdd\u5b58\u8282\u70b9\u7684\u5bf9\u65b9\n *      \u4f9d\u636e\u5bf9\u65b9\u8282\u70b9\u7684op\u67e5\u8be2\u5df1\u65b9\u8282\u70b9\u5206\u5165\u5df1\u65b9\u7ec4\uff0cop\u4e3a0\u7684\u8282\u70b9\u8fd8\u672a\u5206\u7ec4\n *      \u6bcf\u6b21\u5408\u5e76\u5206\u7ec4\u65f6\u5c06\u672a\u5206\u7ec4\u8282\u70b9\u5212\u5165\u5df2\u5206\u7ec4\u8282\u70b9\n * - \u5176\u4ed6\u6ce8\u610f\n *      \u8f93\u5165\u6570\u636e\u4ece1\u5f00\u59cb\uff0c\u6570\u7ec4\u5f00\u4e3aN+1\u5c3a\u5bf8\u65b9\u4fbf\u64cd\u4f5c\n *      \u8f93\u5165\u7684\u4e24\u4e2a\u6570\u636e\u5747\u4e3a\u5df1\u65b9\u65f6\uff0c\u9020\u6210\u6570\u636e\u77db\u76fe\n * ---------------------------------------------------------------\n *\/<\/code><\/pre>\n<pre><code class=\"language-java\">class UnionFind {\n    private int[] id;\n    private int[] op;\n    private int[] sz;\n\n    public UnionFind(int N) {\n        id = new int[N + 1];\n        op = new int[N + 1];\n        sz = new int[N + 1];\n        for (int i = 0; i &lt;= N; i++) {\n            id[i] = i;\n            sz[i] = 1;\n        }\n    }\n\n    public int[] op(int i) {\n        return op;\n    }\n\n    \/\/ union\u65b9\u6cd5\u3001find\u65b9\u6cd5\u89c1\u8def\u5f84\u538b\u7f29\u7684\u5e26\u6743\u5feb\u901f\u5408\u5e76\n}\n\npublic static void main(String[] args) throws Exception {\n    Scanner sc = new Scanner(System.in);\n    UnionFind uf = new UnionFind(N);\n    while (sc.hasNext()) {\n        String flag = sc.next();\n        int p = sc.nextInt();\n        int q = sc.nextInt();\n        switch (flag) {\n            case &quot;A&quot;:\n                if (uf.find(p) == uf.find(q))\n                    System.out.println(&quot;In the same gang.&quot;);\n                else if (uf.find(p) == uf.find(uf.op[q]))\n                    System.out.println(&quot;In different gangs.&quot;);\n                else\n                    System.out.println(&quot;Not sure yet.&quot;);\n                break;\n\n            case &quot;D&quot;:\n                if (uf.find(p) == uf.find(q))\n                    throw new Exception(&quot;Ambiguous Data&quot;);\n                if (uf.op[p] == 0) {\n                    if (uf.op[q] == 0) {\n                        uf.op[p] = q;\n                        uf.op[q] = p;\n                    }\n                    else {\n                        uf.op[p] = q;\n                        uf.union(p, uf.op[q]);\n                    }\n                }\n                else {\n                    if (uf.op[q] == 0) {\n                        uf.op[q] = p;\n                        uf.union(q, uf.op[p]);\n                    }\n                    else {\n                        uf.union(p, uf.op[q]);\n                        uf.union(q, uf.op[p]);\n                    }\n                }\n                break;\n\n            default: throw new Exception(&quot;Non-Compliant Input&quot;);\n        }\n    }\n}<\/code><\/pre>\n<p>\u521d\u59cb\u72b6\u6001\u5982\u4e0b\u6240\u793a<\/p>\n<pre lang=\"java\">\nid [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\nsz [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]\nop [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n<\/pre>\n<p>\u65b0\u5165\u8282\u70b91\u548c\u8282\u70b92\u76f8\u4e92\u4fdd\u5b58\u5bf9\u65b9\u7ec4\u522b\u6807\u8bc6<\/p>\n<pre>\n> D 1 2\n<\/pre>\n<pre lang=\"java\">\nid [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\nsz [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]\nop [0, 2, 1, 0, 0, 0, 0, 0, 0, 0]\n       \u2191  \u2191\n<\/pre>\n<p>\u65b0\u5165\u8282\u70b94\u4f9d\u636e\u8282\u70b91\u67e5\u8be2\u5230\u5df1\u65b9\u8282\u70b92\u5e76\u5206\u4e3a\u4e00\u7ec4<\/p>\n<pre>\n> D 1 4\n<\/pre>\n<pre lang=\"java\">\nid [0, 1, 4, 3, 4, 5, 6, 7, 8, 9]\nsz [1, 1, 1, 1, 2, 1, 1, 1, 1, 1]\nop [0, 2, 1, 0, 1, 0, 0, 0, 0, 0]\n       \u2191  \u2191     \u2191\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5e76\u67e5\u96c6\u7684\u7528\u5904 &#8211; \u5206\u7ec4\u548c\u67e5\u8be2\u5206\u7ec4 \u5e76\u67e5\u96c6\u53ef\u4ee5\u5feb\u901f\u5730\u5206\u7ec4\u548c\u67e5\u8be2\u5206\u7ec4\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u8fd1\u4f3c\u5e38\u6570\uff0c\u901a\u5e38\u7528\u4e8e\u89e3\u51b3 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/139.196.114.170\/?p=1057\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201c\u9ad8\u6548\u89e3\u51b3\u56fe\u8fde\u901a\u6027 &#8211; \u5e76\u67e5\u96c6 Union Find\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[12],"tags":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/posts\/1057"}],"collection":[{"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.196.114.170\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1057"}],"version-history":[{"count":28,"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/posts\/1057\/revisions"}],"predecessor-version":[{"id":2739,"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/posts\/1057\/revisions\/2739"}],"wp:attachment":[{"href":"http:\/\/139.196.114.170\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1057"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.196.114.170\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1057"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.196.114.170\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1057"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}