{"id":320,"date":"2020-04-18T22:27:52","date_gmt":"2020-04-18T14:27:52","guid":{"rendered":"http:\/\/www.zyhcoding.club\/?p=320"},"modified":"2020-04-18T22:54:33","modified_gmt":"2020-04-18T14:54:33","slug":"%e5%b9%b6%e6%9f%a5%e9%9b%86","status":"publish","type":"post","link":"http:\/\/www.zyhcoding.club\/index.php\/2020\/04\/18\/%e5%b9%b6%e6%9f%a5%e9%9b%86\/","title":{"rendered":"\u5e76\u67e5\u96c6\u539f\u7406"},"content":{"rendered":"<h2>\u5e76\u67e5\u96c6<\/h2>\n<p>\u5c06\u4e00\u4e9b\u5143\u7d20\u5206\u7ec4\uff0c\u5f62\u6210\u4e0d\u540c\u7684\u96c6\u5408\uff0c\u7136\u540e\u53ef\u4ee5\u5bf9\u8fd9\u4e9b\u96c6\u5408\u8fdb\u884c\u5f88\u5feb\u7684\u5408\u5e76\u548c\u67e5\u8be2\u7684\u6570\u636e\u7ed3\u6784\u5c31\u662f\u5e76\u67e5\u96c6\u3002<br \/>\n\u5f53\u64cd\u4f5c\u6b21\u6570\u7279\u522b\u591a\u65f6\uff0c\u6734\u7d20\u7684\u5408\u5e76\u96c6\u5408\u7684\u65b9\u6cd5\u4f1a\u8017\u8d39\u5927\u91cf\u7684\u65f6\u95f4\uff0c\u8fd9\u65f6\u5019\u4f7f\u7528\u5e76\u67e5\u96c6\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002<br \/>\n\u5e76\u67e5\u96c6\u4e2d\uff0c\u4f7f\u7528\u4e00\u4e2a\u96c6\u5408\u4e2d\u7684\u67d0\u4e2a\u5143\u7d20\u6765\u6807\u8bb0\u8fd9\u4e2a\u96c6\u5408\uff0c\u8fd9\u548c\u5143\u7d20\u4efb\u610f\u3002\u6211\u4eec\u628a\u8fd9\u4e2a\u7528\u6765\u6807\u8bb0\u6574\u4e2a\u96c6\u5408\u7684\u5143\u7d20\u79f0\u4e3a\u6700\u5927\u8001\u5927\u3002\u5b9a\u4e49\u6570\u7ec4fa(father..)\uff0cfa[x]\u6240\u5728\u7684\u96c6\u5408\u5c31\u662fx\u6240\u5728\u7684\u96c6\u5408\uff0cfa[x]\u662fx\u7684\u76f4\u7cfb\u8001\u5927\u3002\u5f53fa[x]==x\u65f6\uff0c\u8fd9\u4e2ax\u5c31\u662f\u6700\u5927\u8001\u5927\u4e86\u3002<br \/>\n\u6709\u4e86\u4e0a\u9762\u7684\u5173\u7cfb\uff0c\u53ef\u4ee5\u77e5\u9053\u4e00\u4e2a\u96c6\u5408\u91cc\u7684\u5143\u7d20\u6700\u7ec8\u6307\u5411\u7684\u8001\u5927\u80af\u5b9a\u662f\u8fd9\u4e2a\u96c6\u5408\u7684\u6700\u5927\u8001\u5927\u3002\u800c\u5408\u5e76\u4e24\u4e2a\u96c6\u5408\u7684\u65f6\u5019\uff0c\u5728\u8fd9\u4e24\u4e2a\u96c6\u5408\u7684\u4e24\u4e2a\u6700\u5927\u8001\u5927\u91cc\u4efb\u9009\u4e00\u4e2a\u4f5c\u4e3a\u5408\u5e76\u540e\u7684\u6700\u5927\u8001\u5927\u5373\u53ef\u3002<br \/>\n\u8fd9\u5c31\u662f\u5e76\u67e5\u96c6\u7684\u57fa\u672c\u539f\u7406\u3002<br \/>\n\u90a3\u4e48\u521d\u59cb\u65f6\uff0c\u6bcf\u4e2a\u5143\u7d20\u81ea\u6210\u4e00\u4e2a\u96c6\u5408\u3002<\/p>\n<pre><code class=\"language-c++\">for (int i=1;i&lt;=n;i++)\n    fa[i] = i;<\/code><\/pre>\n<p>\u64cd\u4f5c1\uff1a\u67e5\u8be2\u4e24\u4e2a\u5143\u7d20\u662f\u5426\u5728\u4e00\u4e2a\u96c6\u5408\u4e2d<br \/>\n\u53ea\u9700\u68c0\u67e5\u8fd9\u4e24\u4e2a\u5143\u7d20\u7684\u6700\u5927\u8001\u5927\u662f\u5426\u76f8\u7b49\u5373\u53ef\uff01<\/p>\n<h4>\u67e5\u627e\u4e00\u4e2a\u5143\u7d20\u7684\u6700\u5927\u8001\u5927<\/h4>\n<pre><code class=\"language-c++\">int find(int x){\n    if (x==f[x])    return x;\n    return fa[x] = find(fa[x]);\n}<\/code><\/pre>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u82e5x==fa[x]\uff0cx\u5c31\u662f\u6700\u5927\u8001\u5927\uff0c\u8fd9\u662f\u9012\u5f52\u7ec8\u6b62\u6761\u4ef6\u3002<br \/>\n\u5426\u5219\uff0c\u8fd4\u56defind(fa[x])\u3002\u540c\u65f6\uff0c\u6211\u4eec\u77e5\u9053\uff0c\u4e00\u4e2a\u96c6\u5408\u4e2d\u7684\u76f4\u7cfb\u8001\u5927\u8d8a\u5c11\uff0c\u5373\u8d8a\u591a\u7684\u5143\u7d20\u76f4\u63a5\u6307\u5411\u6700\u5927\u8001\u5927\uff0c\u6bcf\u6b21\u67e5\u627e\u7684\u901f\u5ea6\u5c31\u8d8a\u5feb\uff0c\u6240\u4ee5\u6709fa[x] = find(fa[x]);<br \/>\n\u8fd8\u53ef\u4ee5\u5728\u8fdb\u4e00\u6b65\u7684\u4f18\u5316\uff0c\u4e0d\u4f7f\u7528\u9012\u5f52\u6765\u5b8c\u6210\u8fd9\u4e2a\u5de5\u4f5c\u3002<\/p>\n<pre><code class=\"language-c++\">int find(int x){\n    while (x!=fa[x])    x = fa[x] = fa[fa[x]];\n    return x;\n}<\/code><\/pre>\n<p>\u76f8\u6bd4\u4f7f\u7528\u9012\u5f52\uff0c\u5176\u5b9e\u5feb\u4e0d\u4e86\u591a\u5c11\uff0c\uff0c\uff0c\uff0c\u53ea\u662f\u628a\u9012\u5f52\u53d8\u6210\u4e86\u8fed\u4ee3\u800c\u5df2\u3002<br \/>\n\u548c\u9012\u5f52\u4e0d\u540c\u7684\u662f\uff0c\u82e5fa[x]\u4e0d\u662fx\u7684\u6700\u5927\u8001\u5927\uff0cfa[x]\u6700\u7ec8\u7684\u8d4b\u503c\u4e5f\u4e0d\u4e00\u5b9a\u662f\u6700\u5927\u8001\u5927(\u5728\u9012\u5f52\u4e2d\u662f\u7684\uff01)\u3002<\/p>\n<h4>\u5408\u5e76\u4e24\u4e2a\u96c6\u5408<\/h4>\n<p>\u628a\u4e24\u4e2a\u5143\u7d20\u7684\u6700\u5927\u8001\u5927\u5408\u5e76\u5373\u53ef\u3002<\/p>\n<pre><code class=\"language-c++\">fa[find(x)] = find(y);<\/code><\/pre>\n<p>x\uff0cy\u7684\u6b21\u5e8f\u5e76\u4e0d\u91cd\u8981\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5e76\u67e5\u96c6 \u5c06\u4e00\u4e9b\u5143\u7d20\u5206\u7ec4\uff0c\u5f62\u6210\u4e0d\u540c\u7684\u96c6\u5408\uff0c\u7136\u540e\u53ef\u4ee5\u5bf9\u8fd9\u4e9b\u96c6\u5408\u8fdb\u884c\u5f88\u5feb\u7684\u5408\u5e76\u548c\u67e5\u8be2\u7684\u6570\u636e\u7ed3\u6784\u5c31\u662f\u5e76\u67e5\u96c6\u3002 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[29],"tags":[],"_links":{"self":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/320"}],"collection":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/comments?post=320"}],"version-history":[{"count":2,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/320\/revisions"}],"predecessor-version":[{"id":322,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/320\/revisions\/322"}],"wp:attachment":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/media?parent=320"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/categories?post=320"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/tags?post=320"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}