{"id":617,"date":"2017-05-24T14:26:00","date_gmt":"2017-05-24T06:26:00","guid":{"rendered":"http:\/\/kylelv.com\/?p=617"},"modified":"2020-03-04T14:29:20","modified_gmt":"2020-03-04T06:29:20","slug":"bzoj-3390-usaco2004-decbad-cowtractors%e7%89%9b%e7%9a%84%e6%8a%a5%e5%a4%8d-%e6%9c%80%e5%a4%a7%e7%94%9f%e6%88%90%e6%a0%91","status":"publish","type":"post","link":"https:\/\/blog.kylelv.com\/?p=617","title":{"rendered":"bzoj 3390: [Usaco2004 Dec]Bad Cowtractors\u725b\u7684\u62a5\u590d &#8212; \u6700\u5927\u751f\u6210\u6811"},"content":{"rendered":"\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_73 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-69fdef5f9f264\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-69fdef5f9f264\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/blog.kylelv.com\/?p=617\/#3390_Usaco2004_DecBad_Cowtractors%E7%89%9B%E7%9A%84%E6%8A%A5%E5%A4%8D\" title=\"3390: [Usaco2004 Dec]Bad Cowtractors\u725b\u7684\u62a5\u590d\">3390: [Usaco2004 Dec]Bad Cowtractors\u725b\u7684\u62a5\u590d<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/blog.kylelv.com\/?p=617\/#Description\" title=\"Description\">Description<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/blog.kylelv.com\/?p=617\/#Input\" title=\"Input\">Input<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/blog.kylelv.com\/?p=617\/#Output\" title=\"Output\">Output<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/blog.kylelv.com\/?p=617\/#Sample_Input\" title=\"Sample Input\">Sample Input<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/blog.kylelv.com\/?p=617\/#Sample_Output\" title=\"Sample Output\">Sample Output<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/blog.kylelv.com\/?p=617\/#HINT\" title=\"HINT\">HINT<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"has-text-align-center wp-block-heading\"><span class=\"ez-toc-section\" id=\"3390_Usaco2004_DecBad_Cowtractors%E7%89%9B%E7%9A%84%E6%8A%A5%E5%A4%8D\"><\/span>3390: [Usaco2004 Dec]Bad Cowtractors\u725b\u7684\u62a5\u590d<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"has-text-align-center\">Time Limit:&nbsp;1 Sec&nbsp;&nbsp;Memory Limit:&nbsp;128 MB<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Description\"><\/span>Description<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;\u5976\u725b\u8d1d\u831c\u88ab\u96c7\u53bb\u5efa\u8bbeN(2\u2264N\u22641000)\u4e2a\u725b\u68da\u95f4\u7684\u4e92\u8054\u7f51\uff0e\u5979\u5df2\u7ecf\u52d8\u63a2\u51faM(1\u2264M\u226420000)\u6761\u53ef\u5efa\u7684\u7ebf\u8def\uff0c\u6bcf\u6761\u7ebf\u8def\u8fde\u63a5\u4e24\u4e2a\u725b\u68da\uff0c\u800c\u4e14\u4f1a\u82de\u8d39C(1\u2264C\u2264100000)\uff0e\u519c\u592b\u7ea6\u7ff0\u541d\u556c\u5f97\u5f88\uff0c\u4ed6\u5e0c\u671b\u5efa\u8bbe\u8d39\u7528\u6700\u5c11\u751a\u81f3\u4ed6\u90fd\u4e0d\u60f3\u7ed9\u8d1d\u831c\u5de5\u94b1\uff0e&nbsp;\u8d1d\u831c\u5f97\u77e5\u5de5\u94b1\u8981\u544a\u5439\uff0c\u51b3\u5b9a\u62a5\u590d\uff0e\u5979\u6253\u7b97\u9009\u62e9\u5efa\u4e00\u4e9b\u7ebf\u8def\uff0c\u628a\u6240\u6709\u725b\u68da\u8fde\u63a5\u5728\u4e00\u8d77\uff0c\u8ba9\u7ea6\u7ff0\u82b1\u8d39\u6700\u5927\uff0e\u4f46\u662f\u5979\u4e0d\u80fd\u9020\u51fa\u73af\u6765\uff0c\u8fd9\u6837\u7ea6\u7ff0\u5c31\u4f1a\u53d1\u73b0\uff0e<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Input\"><\/span>Input<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>&nbsp;&nbsp;\u7b2c1\u884c\uff1aN\uff0cM.&nbsp;&nbsp;\u7b2c2\u5230M+1\u884c\uff1a\u4e09\u4e2a\u6574\u6570\uff0c\u8868\u793a\u4e00\u6761\u53ef\u80fd\u7ebf\u8def\u7684\u4e24\u4e2a\u7aef\u70b9\u548c\u8d39\u7528\uff0e&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Output\"><\/span>Output<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\u6700\u5927\u7684\u82b1\u8d39\uff0e\u5982\u679c\u4e0d\u80fd\u5efa\u6210\u5408\u7406\u7684\u7ebf\u8def\uff0c\u5c31\u8f93\u51fa-1<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Sample_Input\"><\/span>Sample Input<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>5 8<br>1 2 3<br>1 3 7<br>2 3 10<br>2 4 4<br>2 5 8<br>3 4 6<br>3 5 2<br>4 5 17<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Sample_Output\"><\/span>Sample Output<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>42<\/p>\n\n\n\n<p>\u8fde\u63a54\u548c5,2\u548c5\uff0c2\u548c3\uff0c1\u548c3\uff0c\u82b1\u8d3917+8+10+7=42<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"HINT\"><\/span>HINT<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include&lt;map>\n#include&lt;cmath>\n#include&lt;queue>\n#include&lt;cstdio>\n#include&lt;cstring>\n#include&lt;iostream>\n#include&lt;algorithm>\nusing namespace std;\n#define ll long long\n#define N 1010\n#define M 20010\ninline int rd()\n{\n    int x=0,f=1;char ch=getchar();\n    while(ch&lt;'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}\n    while(ch>='0'&amp;&amp;ch&lt;='9'){x=x*10+ch-'0';ch=getchar();}\n    return x*f;\n}\nstruct qaz{int a,b;ll v;}e&#91;M];\nbool cmp(qaz a,qaz b){return a.v>b.v;}\nint cnt,fa&#91;N];\nint findf(int x){return x==fa&#91;x]?x:fa&#91;x]=findf(fa&#91;x]);}\nint n,m,x,y;\nll ans;\nint main()\n{\n    n=rd();m=rd();\n    int i,j,f1,f2;\n    for(i=1;i&lt;=n;i++) fa&#91;i]=i;\n    for(i=1;i&lt;=m;i++){e&#91;i].a=rd();e&#91;i].b=rd();e&#91;i].v=rd();}\n    sort(e+1,e+m+1,cmp);\n    for(int i=1;i&lt;=m;i++)\n    {\n        x=findf(e&#91;i].a);y=findf(e&#91;i].b);\n        if(x!=y)\n        {\n            ans+=e&#91;i].v;\n            fa&#91;y]=x;\n        }\n    }\n    bool f=0;x=findf(1);\n    for(int i=1;i&lt;=n;i++) if(findf(i)!=x){f=1;break;}\n    f?puts(\"-1\"):printf(\"%lld\\n\",ans);\n    return 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>3390: [Usaco2004 Dec]Bad Cowtractors\u725b\u7684\u62a5\u590d Time Limit:&#038;nb [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,66],"tags":[56],"class_list":["post-617","post","type-post","status-publish","format-standard","hentry","category-bzoj","category-usaco","tag-56"],"_links":{"self":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/617","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=617"}],"version-history":[{"count":1,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/617\/revisions"}],"predecessor-version":[{"id":618,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/617\/revisions\/618"}],"wp:attachment":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=617"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=617"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=617"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}