{"id":135,"date":"2023-04-17T21:38:40","date_gmt":"2023-04-17T13:38:40","guid":{"rendered":"https:\/\/solost.cn\/?p=135"},"modified":"2023-04-17T21:38:40","modified_gmt":"2023-04-17T13:38:40","slug":"242","status":"publish","type":"post","link":"https:\/\/solost.cn\/?p=135","title":{"rendered":"Kruskal"},"content":{"rendered":"<h2>kruskal<\/h2>\n<pre><code class=\"lang-cpp\">int n, m;       \/\/ n\u662f\u70b9\u6570\uff0cm\u662f\u8fb9\u6570\nint p[N];       \/\/ \u5e76\u67e5\u96c6\u7684\u7236\u8282\u70b9\u6570\u7ec4\n\nstruct Edge     \/\/ \u5b58\u50a8\u8fb9\n{\n    int a, b, w;\n\n    bool operator&lt; (const Edge &amp;W)const\n    {\n        return w &lt; W.w;\n    }\n}edges[M];\n\nint find(int x)     \/\/ \u5e76\u67e5\u96c6\u6838\u5fc3\u64cd\u4f5c\n{\n    if (p[x] != x) p[x] = find(p[x]);\n    return p[x];\n}\n\nint kruskal()\n{\n    sort(edges, edges + m);\n\n    for (int i = 1; i &lt;= n; i ++ ) p[i] = i;    \/\/ \u521d\u59cb\u5316\u5e76\u67e5\u96c6\n\n    int res = 0, cnt = 0;\n    for (int i = 0; i &lt; m; i ++ )\n    {\n        int a = edges[i].a, b = edges[i].b, w = edges[i].w;\n\n        a = find(a), b = find(b);\n        if (a != b)     \/\/ \u5982\u679c\u4e24\u4e2a\u8fde\u901a\u5757\u4e0d\u8fde\u901a\uff0c\u5219\u5c06\u8fd9\u4e24\u4e2a\u8fde\u901a\u5757\u5408\u5e76\n        {\n            p[a] = b;\n            res += w;\n            cnt ++ ;\n        }\n    }\n\n    if (cnt &lt; n - 1) return INF;\n    return res;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>kruskal int n, m; \/\/ n\u662f\u70b9\u6570\uff0cm\u662f\u8fb9\u6570 int p[N]; \/\/ \u5e76\u67e5\u96c6\u7684\u7236\u8282\u70b9\u6570\u7ec4 struct Edge \/\/ \u5b58\u50a8\u8fb9 { int a, b, w; bool operator&lt; (const Edge &amp;W)const { return w &lt; W.w; } }edges[M]; int find(int x) \/\/ \u5e76\u67e5\u96c6\u6838\u5fc3\u64cd\u4f5c { if (p[x] != x) p[x] = find(p[x]); return p[x]; } int kruskal() { sort(edges, edges + m); for [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[72],"tags":[53,52],"class_list":["post-135","post","type-post","status-publish","format-standard","hentry","category-acm","tag-kruskal","tag-52"],"_links":{"self":[{"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts\/135","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=135"}],"version-history":[{"count":0,"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts\/135\/revisions"}],"wp:attachment":[{"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=135"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}