{"id":634,"date":"2013-09-16T10:40:29","date_gmt":"2013-09-16T02:40:29","guid":{"rendered":"http:\/\/blog.dayandcarrot.net\/?p=634"},"modified":"2013-09-16T10:40:29","modified_gmt":"2013-09-16T02:40:29","slug":"%e4%b9%9d%e5%ba%a6oj-%e9%a2%98%e7%9b%ae1012%ef%bc%9a%e7%95%85%e9%80%9a%e5%b7%a5%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=634","title":{"rendered":"\u4e5d\u5ea6OJ \u9898\u76ee1012\uff1a\u7545\u901a\u5de5\u7a0b"},"content":{"rendered":"<dl>\n<dt><b>\u9898\u76ee\u63cf\u8ff0\uff1a<\/b><\/dt>\n<dd>\u00a0\u00a0\u00a0 \u67d0\u7701\u8c03\u67e5\u57ce\u9547\u4ea4\u901a\u72b6\u51b5\uff0c\u5f97\u5230\u73b0\u6709\u57ce\u9547\u9053\u8def\u7edf\u8ba1\u8868\uff0c\u8868\u4e2d\u5217\u51fa\u4e86\u6bcf\u6761\u9053\u8def\u76f4\u63a5\u8fde\u901a\u7684\u57ce\u9547\u3002\u7701\u653f\u5e9c\u201c\u7545\u901a\u5de5\u7a0b\u201d\u7684\u76ee\u6807\u662f\u4f7f\u5168\u7701\u4efb\u4f55\u4e24\u4e2a\u57ce\u9547\u95f4\u90fd\u53ef\u4ee5\u5b9e\u73b0\u4ea4\u901a\uff08\u4f46\u4e0d\u4e00\u5b9a\u6709\u76f4\u63a5\u7684\u9053\u8def\u76f8\u8fde\uff0c\u53ea\u8981\u4e92\u76f8\u95f4\u63a5\u901a\u8fc7\u9053\u8def\u53ef\u8fbe\u5373\u53ef\uff09\u3002\u95ee\u6700\u5c11\u8fd8\u9700\u8981\u5efa\u8bbe\u591a\u5c11\u6761\u9053\u8def\uff1f\n<\/dd>\n<\/dl>\n<dl>\n<dt><b>\u8f93\u5165\uff1a<\/b><\/dt>\n<dd>\u00a0\u00a0\u00a0 \u6d4b\u8bd5\u8f93\u5165\u5305\u542b\u82e5\u5e72\u6d4b\u8bd5\u7528\u4f8b\u3002\u6bcf\u4e2a\u6d4b\u8bd5\u7528\u4f8b\u7684\u7b2c1\u884c\u7ed9\u51fa\u4e24\u4e2a\u6b63\u6574\u6570\uff0c\u5206\u522b\u662f\u57ce\u9547\u6570\u76eeN ( &lt; 1000 )\u548c\u9053\u8def\u6570\u76eeM\uff1b\u968f\u540e\u7684M\u884c\u5bf9\u5e94M\u6761\u9053\u8def\uff0c\u6bcf\u884c\u7ed9\u51fa\u4e00\u5bf9\u6b63\u6574\u6570\uff0c\u5206\u522b\u662f\u8be5\u6761\u9053\u8def\u76f4\u63a5\u8fde\u901a\u7684\u4e24\u4e2a\u57ce\u9547\u7684\u7f16\u53f7\u3002\u4e3a\u7b80\u5355\u8d77\u89c1\uff0c\u57ce\u9547\u4ece1\u5230N\u7f16\u53f7\u3002<br \/>\n\u6ce8\u610f:\u4e24\u4e2a\u57ce\u5e02\u4e4b\u95f4\u53ef\u4ee5\u6709\u591a\u6761\u9053\u8def\u76f8\u901a,\u4e5f\u5c31\u662f\u8bf4<br \/>\n3 3<br \/>\n1 2<br \/>\n1 2<br \/>\n2 1<br \/>\n\u8fd9\u79cd\u8f93\u5165\u4e5f\u662f\u5408\u6cd5\u7684<br \/>\n\u5f53N\u4e3a0\u65f6\uff0c\u8f93\u5165\u7ed3\u675f\uff0c\u8be5\u7528\u4f8b\u4e0d\u88ab\u5904\u7406\u3002\n<\/dd>\n<\/dl>\n<dl>\n<dt><b>\u8f93\u51fa\uff1a<\/b><\/dt>\n<dd>\u00a0\u00a0\u00a0 \u5bf9\u6bcf\u4e2a\u6d4b\u8bd5\u7528\u4f8b\uff0c\u57281\u884c\u91cc\u8f93\u51fa\u6700\u5c11\u8fd8\u9700\u8981\u5efa\u8bbe\u7684\u9053\u8def\u6570\u76ee\u3002\n<\/dd>\n<\/dl>\n<dl>\n<dt><b>\u6837\u4f8b\u8f93\u5165\uff1a<\/b><\/dt>\n<dd>\n<pre>4 2\n1 3\n4 3\n3 3\n1 2\n1 3\n2 3\n5 2\n1 2\n3 5\n999 0\n0<\/pre>\n<\/dd>\n<\/dl>\n<dl>\n<dt><b>\u6837\u4f8b\u8f93\u51fa\uff1a<\/b><\/dt>\n<dd>\n<pre>1\n2\n998<\/pre>\n<\/dd>\n<\/dl>\n<p>==============================<br \/>\n\u8fd9\u4e2a\u9898\u76ee\uff0c\u6982\u5ff5\u4e0a\u5c31\u662f\u6c42\u975e\u8fde\u901a\u56fe\u91cc\u9762\u8054\u901a\u5b50\u56fe\u7684\u6570\u91cf&#8230;\u9053\u8def\u561b\u5c31\u662f\u518d\u52a0\u51e0\u6761\u8fb9\u80fd\u628a\u5404\u4e2a\u8054\u901a\u5b50\u56fe\u8fde\u8d77\u6765~<br \/>\n\u505a\u6cd5\u7684\u8bdd\uff0c\u7528\u5e76\u67e5\u96c6\u5f04\u5427~<br \/>\n\u5e76\u67e5\u96c6\u6700\u7b80\u5355\u7684\u73a9\u6cd5\u5c31\u662f\uff0c\u5f04\u4e00\u4e2a\u4eb2\u7239\u6570\u7ec4\uff0c\u6570\u7ec4\u91cc\u9762\u5b58\u6307\u5411\u81ea\u5df1\u4eb2\u7239\u7684\u6307\u9488\uff0c\u6ca1\u7239\u7684\u5c31\u662f\u6839\u4e86&#8230;<br \/>\n\u7edf\u8ba1\u6709\u51e0\u4e2a\u6ca1\u7239\u7684\u5c31\u662f\u7edf\u8ba1\u6709\u591a\u5c11\u8054\u901a\u5b50\u56fe<br \/>\n==============================<br \/>\n<code lang=\"c++\"><br \/>\n#include <iostream><br \/>\nusing namespace std;<br \/>\nint findRoot(int *parentAry, int currIndex)<br \/>\n{<br \/>\n\tif(parentAry[currIndex] == -1)<br \/>\n\t\treturn currIndex;  \/\/\u6ca1\u7239\u4e86\uff0ccurrIndex\u5c31\u662f\u7239<br \/>\n\telse<br \/>\n\t{<br \/>\n\t\tint rootIndex = findRoot(parentAry, parentAry[currIndex]);<br \/>\n\t\t\/\/\u538b\u7f29\u8def\u5f84<br \/>\n\t\tparentAry[currIndex] = rootIndex;<br \/>\n\t\treturn rootIndex;<br \/>\n\t}<br \/>\n}<br \/>\nint main()<br \/>\n{<br \/>\n\tint N;<br \/>\n\twhile(cin >> N && N!=0)<br \/>\n\t{<br \/>\n\t\tint M;<br \/>\n\t\tcin >> M;<br \/>\n\t\tint *parentAry = new int[N];<br \/>\n\t\tint i;<br \/>\n\t\tfor(i=0; i<n; i++)\n\t\t\tparentAry[i] = -1;\n\t\tfor(i=0; i<m; i++)\n\t\t{\n\t\t\tint x, y;\n\t\t\tcin >> x >> y;<br \/>\n\t\t\tx--; y--;<br \/>\n\t\t\tint parentX = findRoot(parentAry, x);<br \/>\n\t\t\tint parentY = findRoot(parentAry, y);<br \/>\n\t\t\tif(parentX != parentY)<br \/>\n\t\t\t{<br \/>\n\t\t\t\t\/\/\u4e0d\u5728\u4e00\u4e2a\u96c6\u5408\u4e2d\uff0c\u5408\u5e76\uff01<br \/>\n\t\t\t\tparentAry[parentX] = parentY; \/\/\u5c06X\u5f53\u524d\u7684\u6839\u6302\u5728Y\u7684\u6839\u4e0a<br \/>\n\t\t\t}<br \/>\n\t\t}<br \/>\n\t\tint count = 0;<br \/>\n\t\tfor(i=0; i<n; i++)\n\t\t{\n\t\t\tif(parentAry[i] == -1)\n\t\t\t\tcount++;\n\t\t}\n\t\tcout << count-1 << endl;\n\t\tdelete []parentAry;\n\t}\n\treturn 0;\n}\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u63cf\u8ff0\uff1a \u00a0\u00a0\u00a0 \u67d0\u7701\u8c03\u67e5\u57ce\u9547\u4ea4\u901a\u72b6\u51b5\uff0c\u5f97\u5230\u73b0\u6709\u57ce\u9547\u9053\u8def\u7edf\u8ba1\u8868\uff0c\u8868\u4e2d\u5217\u51fa\u4e86\u6bcf\u6761\u9053\u8def\u76f4\u63a5\u8fde\u901a\u7684\u57ce\u9547\u3002\u7701\u653f\u5e9c\u201c\u7545\u901a\u5de5\u7a0b\u201d\u7684\u76ee\u6807\u662f\u4f7f\u5168\u7701\u4efb\u4f55\u4e24\u4e2a\u57ce\u9547\u95f4\u90fd\u53ef\u4ee5\u5b9e\u73b0\u4ea4\u901a\uff08\u4f46\u4e0d\u4e00\u5b9a\u6709\u76f4\u63a5\u7684\u9053\u8def\u76f8\u8fde\uff0c\u53ea\u8981\u4e92\u76f8\u95f4\u63a5\u901a\u8fc7\u9053\u8def\u53ef\u8fbe\u5373\u53ef\uff09\u3002\u95ee\u6700\u5c11\u8fd8\u9700\u8981\u5efa\u8bbe\u591a\u5c11\u6761\u9053\u8def\uff1f \u8f93\u5165\uff1a \u00a0\u00a0\u00a0 \u6d4b\u8bd5\u8f93\u5165\u5305\u542b\u82e5\u5e72\u6d4b\u8bd5\u7528\u4f8b\u3002\u6bcf\u4e2a\u6d4b\u8bd5\u7528\u4f8b\u7684\u7b2c1\u884c\u7ed9\u51fa\u4e24\u4e2a\u6b63\u6574\u6570\uff0c\u5206\u522b\u662f\u57ce\u9547\u6570\u76eeN ( &lt; 1000 )\u548c\u9053\u8def\u6570\u76eeM\uff1b\u968f\u540e\u7684M\u884c\u5bf9\u5e94M\u6761\u9053\u8def\uff0c\u6bcf\u884c\u7ed9\u51fa\u4e00\u5bf9\u6b63\u6574\u6570\uff0c\u5206\u522b\u662f\u8be5\u6761\u9053\u8def\u76f4\u63a5\u8fde\u901a\u7684\u4e24\u4e2a\u57ce\u9547\u7684\u7f16\u53f7\u3002\u4e3a\u7b80\u5355\u8d77\u89c1\uff0c\u57ce\u9547\u4ece1\u5230N\u7f16\u53f7\u3002 \u6ce8\u610f:\u4e24\u4e2a\u57ce\u5e02\u4e4b\u95f4\u53ef\u4ee5\u6709\u591a\u6761\u9053\u8def\u76f8\u901a,\u4e5f\u5c31\u662f\u8bf4 3 3 1 2 1 2 2 1 \u8fd9\u79cd\u8f93\u5165\u4e5f\u662f\u5408\u6cd5\u7684 \u5f53N\u4e3a0\u65f6\uff0c\u8f93\u5165\u7ed3\u675f\uff0c\u8be5\u7528\u4f8b\u4e0d\u88ab\u5904\u7406\u3002 \u8f93\u51fa\uff1a \u00a0\u00a0\u00a0 \u5bf9\u6bcf\u4e2a\u6d4b\u8bd5\u7528\u4f8b\uff0c\u57281\u884c\u91cc\u8f93\u51fa\u6700\u5c11\u8fd8\u9700\u8981\u5efa\u8bbe\u7684\u9053\u8def\u6570\u76ee\u3002 \u6837\u4f8b\u8f93\u5165\uff1a 4 2 1 3 4 3 3 3 1 2 1 3 2 3 5 2 1 2 3 5 999 0 0 \u6837\u4f8b\u8f93\u51fa\uff1a 1 2 998 ============================== \u8fd9\u4e2a\u9898\u76ee\uff0c\u6982\u5ff5\u4e0a\u5c31\u662f\u6c42\u975e\u8fde\u901a\u56fe\u91cc\u9762\u8054\u901a\u5b50\u56fe\u7684\u6570\u91cf&#8230;\u9053\u8def\u561b\u5c31\u662f\u518d\u52a0\u51e0\u6761\u8fb9\u80fd\u628a\u5404\u4e2a\u8054\u901a\u5b50\u56fe\u8fde\u8d77\u6765~ \u505a\u6cd5\u7684\u8bdd\uff0c\u7528\u5e76\u67e5\u96c6\u5f04\u5427~ [&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],"tags":[132],"class_list":["post-634","post","type-post","status-publish","format-standard","hentry","category-study","tag-oj"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/634","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=634"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/634\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=634"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=634"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=634"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}