{"id":152,"date":"2023-04-24T21:40:26","date_gmt":"2023-04-24T13:40:26","guid":{"rendered":"https:\/\/solost.cn\/?p=152"},"modified":"2023-04-24T21:40:26","modified_gmt":"2023-04-24T13:40:26","slug":"260","status":"publish","type":"post","link":"https:\/\/solost.cn\/?p=152","title":{"rendered":"P1195 &#8211; \u53e3\u888b\u7684\u5929\u7a7a"},"content":{"rendered":"<h2>\u9898\u76ee<\/h2>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2026\/11\/image-24.png\" alt=\"image.png\" title=\"image.png\"><\/p>\n<h2>\u9898\u76ee\u94fe\u63a5<\/h2>\n<p><a href=\"https:\/\/www.luogu.com.cn\/problem\/P1195\">\u94fe\u63a5<\/a><\/p>\n<h2>\u9898\u76ee\u5927\u610f<\/h2>\n<p>\u7ed9\u4f60n\u4e2a\u70b9\uff0cm\u6761\u8fb9\uff0c\u8981\u751f\u6210k\u4e2a\u6700\u5c0f\u751f\u6210\u6811<\/p>\n<h2>\u9898\u89e3<\/h2>\n<p>\u521a\u5f00\u59cb\u60f3\u7684\u5c31\u662fkruskal\uff0c\u4f46\u662f\u6ca1\u7406\u89e3\u4e32\u6210k\u4e32\u68c9\u82b1\u7cd6\u3002\u4e5f\u5c31\u662f\u81f3\u5c11\u751f\u6210k\u4e2a\u6700\u5c0f\u751f\u6210\u6811\u3002<\/p>\n<p>\u751f\u6210\u4e00\u4e2an\u4e2a\u70b9\u7684\u6811\u9700\u8981n-1\u6761\u8fb9\uff0c\u7136\u540e\u751f\u6210k\u4e2a\u6700\u5c0f\u751f\u6210\u6811\u90a3\u5c31\u81f3\u5c11\u9700\u8981n-k\u6761\u8fb9\u3002<\/p>\n<blockquote>\n<p>\u6240\u4ee5\u6211\u4eec\u5982\u679c\u60f3\u8981\u8fde\u51fak\u68f5\u6811\uff0c\u5c31\u9700\u8981\u8fden-k\u6761\u8fb9\u3002<\/p>\n<p>\u9898\u76ee\u8981\u6c42\u7528n\u6735\u4e91\u8fde\u51fak\u4e2a\u68c9\u82b1\u7cd6\u3002<\/p>\n<p>\u56e0\u4e3a\u6bcf\u4e2a\u68c9\u82b1\u7cd6\u90fd\u662f\u8fde\u901a\u7684\uff0c<\/p>\n<p>\u90a3\u4e48\u6bcf\u4e2a\u68c9\u82b1\u7cd6\u5c31\u76f8\u5f53\u4e8e\u662f\u4e00\u68f5\u6811\u3002<\/p>\n<p>\u5c31\u662f\u8bf4\u8981\u7528n\u4e2a\u8282\u70b9\u8fde\u51fak\u68f5\u6811\u3002<\/p>\n<p>\u4e5f\u5c31\u662f\u8bf4\u8981\u7528n-k\u6761\u8fb9\u8fde\u51fak\u68f5\u6811\u3002<\/p>\n<p>\u4e5f\u5c31\u662f\u8bf4\u8981\u82b1\u8d39\u8fde\u51fan-k\u6761\u8fb9\u7684\u4ee3\u4ef7\u3002<\/p>\n<p>\u65e2\u7136\u4e00\u5b9a\u8981\u82b1\u8d39\u8fde\u51fan-k\u6761\u8fb9\u7684\u4ee3\u4ef7\uff0c<\/p>\n<p>\u90a3\u4e48\u5f53\u7136\u8981\u9009\u62e9\u4ee3\u4ef7\u6700\u5c0f\u7684\u8fb9\u8fde\u8d77\u6765\u3002<\/p>\n<p>\u6240\u4ee5\u7ed9\u6bcf\u6761\u53ef\u4ee5\u8fde\u7684\u8fb9\u6309\u4ee3\u4ef7\u4ece\u5c0f\u5230\u5927\u6392\u4e2a\u5e8f\uff0c<\/p>\n<p>\u7136\u540e\u8fden-k\u6761\u8fb9\u9020k\u4e2a\u6700\u5c0f\u751f\u6210\u6811\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n<p>\u5982\u679c\u7ed9\u7684\u5173\u7cfb\u6570m\u5c0f\u4e8e\u9700\u8981\u8fde\u7684\u8fb9\u6570\uff08n-k\uff09\uff0c\u662f\u4e00\u5b9a\u8fde\u4e0d\u51fak\u4e2a\u6811\u6765\u7684\uff0c\u56e0\u4e3am\u4e2a\u5173\u7cfb\u53ea\u80fd\u8fdem\u6761\u8fb9\u3002<\/p>\n<\/blockquote>\n<pre><code class=\"lang-cpp\">#include &lt;map&gt;\n#include &lt;set&gt;\n#include &lt;cmath&gt;\n#include &lt;queue&gt;\n#include &lt;stack&gt;\n#include &lt;vector&gt;\n#include &lt;bitset&gt;\n#include &lt;cstdio&gt;\n#include &lt;cstdlib&gt;\n#include &lt;cstring&gt;\n#include &lt;fstream&gt;\n#include &lt;iostream&gt;\n#include &lt;algorithm&gt;\n#include &lt;unordered_map&gt;\n#include &lt;unordered_set&gt;\n\n#define x first\n#define y second\n#define endl &#039;\\n&#039;\n#define IOS                       \\\n    ios_base::sync_with_stdio(0); \\\n    cin.tie(0);                   \\\n    cout.tie(0);\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair&lt;ll, ll&gt; pll;\ntypedef pair&lt;int, int&gt; pii;\ntypedef unsigned long long ull;\n\nconst int INF = 0x3f3f3f3f, mod = 1000000007;\nconst int N = 2e5 + 10;\nint n, m, k;\nstruct node{\n    int x, y, w;\n    bool operator&lt; (const node &amp;a) const{\n        return w &lt; a.w;\n    }\n}a[N];\n\nint p[N];\n\nint find(int x){\n    if(p[x] != x) p[x] = find(p[x]);\n    return p[x];\n}\n\nvoid solve()\n{\n    cin &gt;&gt; n &gt;&gt; m &gt;&gt; k;\n    for(int i = 1; i &lt;= n; i++) p[i] = i;\n    ll res = 0, cnt = 0;\n    for(int i = 1; i &lt;= m; i++){\n        int x, y, w;\n        cin &gt;&gt; x &gt;&gt; y &gt;&gt; w;\n        a[i] = {x, y, w};\n    }\n    sort(a + 1, a + m + 1);\n    for(int i = 1; i &lt;= m; i++){\n        int x = a[i].x, y = a[i].y, w = a[i].w;\n        int fx = find(x), fy = find(y);\n        if(fx != fy){\n            p[fy] = fx;\n            res += w;\n            cnt++;\n        }\n        if(cnt &gt;= n - k){\n            cout &lt;&lt; res &lt;&lt; endl;\n            return;\n        }\n    }\n    cout &lt;&lt; &quot;No Answer\\n&quot;;\n\n}\nint main()\n{\n    \/\/ IOS;\n    int t = 1;\n    \/\/ cin &gt;&gt; t;\n    while (t--) {\n        solve();\n    }\n    return 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee \u9898\u76ee\u94fe\u63a5 \u94fe\u63a5 \u9898\u76ee\u5927\u610f \u7ed9\u4f60n\u4e2a\u70b9\uff0cm\u6761\u8fb9\uff0c\u8981\u751f\u6210k\u4e2a\u6700\u5c0f\u751f\u6210\u6811 \u9898\u89e3 \u521a\u5f00\u59cb\u60f3\u7684\u5c31\u662fkruskal\uff0c\u4f46\u662f\u6ca1\u7406\u89e3\u4e32\u6210k\u4e32\u68c9\u82b1\u7cd6\u3002\u4e5f\u5c31\u662f\u81f3\u5c11\u751f\u6210k\u4e2a\u6700\u5c0f\u751f\u6210\u6811\u3002 \u751f\u6210\u4e00\u4e2an\u4e2a\u70b9\u7684\u6811\u9700\u8981n-1\u6761\u8fb9\uff0c\u7136\u540e\u751f\u6210k\u4e2a\u6700\u5c0f\u751f\u6210\u6811\u90a3\u5c31\u81f3\u5c11\u9700\u8981n-k\u6761\u8fb9\u3002 \u6240\u4ee5\u6211\u4eec\u5982\u679c\u60f3\u8981\u8fde\u51fak\u68f5\u6811\uff0c\u5c31\u9700\u8981\u8fden-k\u6761\u8fb9\u3002 \u9898\u76ee\u8981\u6c42\u7528n\u6735\u4e91\u8fde\u51fak\u4e2a\u68c9\u82b1\u7cd6\u3002 \u56e0\u4e3a\u6bcf\u4e2a\u68c9\u82b1\u7cd6\u90fd\u662f\u8fde\u901a\u7684\uff0c \u90a3\u4e48\u6bcf\u4e2a\u68c9\u82b1\u7cd6\u5c31\u76f8\u5f53\u4e8e\u662f\u4e00\u68f5\u6811\u3002 \u5c31\u662f\u8bf4\u8981\u7528n\u4e2a\u8282\u70b9\u8fde\u51fak\u68f5\u6811\u3002 \u4e5f\u5c31\u662f\u8bf4\u8981\u7528n-k\u6761\u8fb9\u8fde\u51fak\u68f5\u6811\u3002 \u4e5f\u5c31\u662f\u8bf4\u8981\u82b1\u8d39\u8fde\u51fan-k\u6761\u8fb9\u7684\u4ee3\u4ef7\u3002 \u65e2\u7136\u4e00\u5b9a\u8981\u82b1\u8d39\u8fde\u51fan-k\u6761\u8fb9\u7684\u4ee3\u4ef7\uff0c \u90a3\u4e48\u5f53\u7136\u8981\u9009\u62e9\u4ee3\u4ef7\u6700\u5c0f\u7684\u8fb9\u8fde\u8d77\u6765\u3002 \u6240\u4ee5\u7ed9\u6bcf\u6761\u53ef\u4ee5\u8fde\u7684\u8fb9\u6309\u4ee3\u4ef7\u4ece\u5c0f\u5230\u5927\u6392\u4e2a\u5e8f\uff0c \u7136\u540e\u8fden-k\u6761\u8fb9\u9020k\u4e2a\u6700\u5c0f\u751f\u6210\u6811\u5c31\u53ef\u4ee5\u4e86\u3002 \u5982\u679c\u7ed9\u7684\u5173\u7cfb\u6570m\u5c0f\u4e8e\u9700\u8981\u8fde\u7684\u8fb9\u6570\uff08n-k\uff09\uff0c\u662f\u4e00\u5b9a\u8fde\u4e0d\u51fak\u4e2a\u6811\u6765\u7684\uff0c\u56e0\u4e3am\u4e2a\u5173\u7cfb\u53ea\u80fd\u8fdem\u6761\u8fb9\u3002 #include &lt;map&gt; #include &lt;set&gt; #include &lt;cmath&gt; #include &lt;queue&gt; #include &lt;stack&gt; #include &lt;vector&gt; #include &lt;bitset&gt; #include &lt;cstdio&gt; #include &lt;cstdlib&gt; #include &lt;cstring&gt; #include &lt;fstream&gt; #include &lt;iostream&gt; #include &lt;algorithm&gt; #include &lt;unordered_map&gt; #include &lt;unordered_set&gt; #define x first #define y [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[53,52],"class_list":["post-152","post","type-post","status-publish","format-standard","hentry","category-solution","tag-kruskal","tag-52"],"_links":{"self":[{"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts\/152","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=152"}],"version-history":[{"count":0,"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts\/152\/revisions"}],"wp:attachment":[{"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=152"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}