{"id":1089,"date":"2024-08-14T09:00:00","date_gmt":"2024-08-14T01:00:00","guid":{"rendered":"https:\/\/seanxd.com\/?p=1089"},"modified":"2024-07-14T23:43:44","modified_gmt":"2024-07-14T15:43:44","slug":"zerojudge-a445","status":"publish","type":"post","link":"https:\/\/seanxd.com\/en\/zerojudge-a445\/","title":{"rendered":"ZeroJudge A445: I don't have friends"},"content":{"rendered":"\n\n\n<p>\u6211\u7684\u670b\u53cb\u5f88\u5c11\uff5e\u4f46\u662f\u670b\u53cb\u7684\u670b\u53cb\u5c31\u662f\u6211\u7684\u670b\u53cb\uff01<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u7bc4\u4f8b\u6e2c\u8cc7<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7bc4\u4f8b\u8f38\u5165<\/th><th>\u7bc4\u4f8b\u8f38\u51fa<\/th><\/tr><\/thead><tbody><tr><td>\u7b2c\u4e00\u884c\u6709\u4e09\u500b\u6b63\u6574\u6578 N\u3001M\u3001Q (N &lt;= 10000\uff0cM &lt;= 10000\uff0cQ &lt;= 100000)\uff0c\u4ee3\u8868\u6709 N \u500b\u4eba\u3001M \u7b46\u670b\u53cb\u95dc\u4fc2\u3001Q \u7b46\u8a62\u554f\u3002<br>\u63a5\u4e0b\u4f86\u6709 M \u884c\uff0c \u6bcf\u884c\u6709\u5169\u500b\u6b63\u6574\u6578 A \u548c B (A\u3001B &lt;= N)\uff0c\u4ee3\u8868 A \u548c B \u6709\u670b\u53cb\u95dc\u4fc2\u3002<br>\u518d\u4f86\u6709 Q \u884c\uff0c \u6bcf\u884c\u5169\u500b\u6b63\u6574\u6578 P \u548c Q (P\u3001Q &lt;= N )\u3002<\/td><td>\u8acb\u8f38\u51fa P \u548c Q \u662f\u4e0d\u662f\u670b\u53cb\uff0c\u5982\u679c\u662f\u8acb\u8f38\u51fa \u300c:)\u300d\uff0c\u5426\u5247\u8acb\u8f38\u51fa \u300c:(\u300d\u3002<\/td><\/tr><tr><td>5 3 2<br>1 2<br>2 5<br>3 4<br>1 5<br>1 3<\/td><td>\ud83d\ude42<br>\ud83d\ude41<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\u984c\u601d\u8def<\/h2>\n\n\n\n<p>\u5ba3\u544a\u4e00\u500b\u9663\u5217\u4ee3\u8868\u6bcf\u4e00\u500b\u4eba\u7684\u6700\u5e95\u5c64\u670b\u53cb\u662f\u8ab0\uff0c\u6bcf\u4e00\u500b\u4eba\u9810\u8a2d\u503c\u70ba 0\u3002\u5982\u679c\u6536\u5230\u4eba\u7684\u6642\u5019\u9019\u500b\u4eba\u7684\u9663\u5217\u503c\u70ba 0\uff0c\u4ee3\u8868\u9084\u6c92\u6709\u7d00\u9304\uff0c\u6240\u4ee5\u5c07\u9663\u5217\u503c\u8a2d\u70ba\u81ea\u5df1\u3002<\/p>\n\n\n\n<p>\u5ba3\u544a\u4e00\u500b\u51fd\u5f0f findEnd \u7528\u4f86\u5c0b\u627e\u6700\u5e95\u5c64\u7684\u670b\u53cb\uff0c\u6709\u4e00\u500b\u53c3\u6578 N\uff0c\u5982\u679c N \u7684\u9663\u5217\u503c\u70ba 0 \u6216\u662f\u81ea\u5df1\u5c31 return N\uff0c\u5426\u5247\u5c31 return findEnd(\u9663\u5217\u503c[N])\u3002<\/p>\n\n\n\n<p>\u6536\u5230\u8cc7\u6599\u7684\u6642\u5019\u5c07 \u9663\u5217\u503c[findEnd(B)] \u8a2d\u70ba findEnd(A)\u3002\u6700\u5f8c\u53ea\u9700\u8981\u78ba\u8a8d \u9663\u5217\u503c[P] \u662f\u5426\u7b49\u65bc \u9663\u5217\u503c[Q]\uff0c\u5982\u679c\u76f8\u540c\u5c31\u4ee3\u8868\u9019\u5169\u4eba\u662f\u670b\u53cb\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u7bc4\u4f8b\u7a0b\u5f0f\u78bc\uff0d<a href=\"https:\/\/zerojudge.tw\/ShowProblem?problemid=a445\" target=\"_blank\" rel=\"noreferrer noopener\">ZeroJudge A445: \u65b0\u624b\u8a13\u7df4\u7cfb\u5217 &#8211; \u6211\u7684\u670b\u53cb\u5f88\u5c11<\/a><\/h3>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-cpp\" data-lang=\"C++\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nint MAP[870000] = {};\n\nint findEnd(const int N) {\n    const int MM = MAP[N];\n    if (MM == 0 || MM == N) return N;\n    return findEnd(MM);\n}\n\nvoid line(const int a, const int b) {\n    if (MAP[a] == 0) MAP[a] = a;\n    if (MAP[b] == 0) MAP[b] = b;\n    MAP[findEnd(b)] = findEnd(a);\n}\n\nint main() {\n    cin.sync_with_stdio(0);\n    cin.tie(0);\n    cout.sync_with_stdio(0);\n    cout.tie(0);\n    int N, M, Q;\n    cin &gt;&gt; N &gt;&gt; M &gt;&gt; Q;\n    for (int i = 1; i&lt;=N; i++) MAP[i] = 0;\n    for (int i = 0; i&lt;M; i++) {\n        int a, b;\n        cin &gt;&gt; a &gt;&gt; b;\n        line(a, b);\n    }\n    for (int i = 0; i&lt;Q; i++) {\n        int a, b;\n        cin &gt;&gt; a &gt;&gt; b;\n        if (findEnd(a) == findEnd(b)) cout &lt;&lt; &quot;:)\\n&quot;;\n        else cout &lt;&lt; &quot;:(\\n&quot;;\n    }\n}\n\n\/\/ZeroJudge A445\n\/\/Dr. SeanXD<\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u7684\u670b\u53cb\u5f88\u5c11\uff5e\u4f46\u662f\u670b\u53cb\u7684\u670b\u53cb\u5c31\u662f\u6211\u7684\u670b\u53cb\uff01 \u7bc4\u4f8b\u6e2c\u8cc7 \u7bc4\u4f8b\u8f38\u5165 \u7bc4\u4f8b\u8f38\u51fa \u7b2c\u4e00\u884c\u6709\u4e09\u500b\u6b63\u6574\u6578 N\u3001M\u3001Q (N [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","footnotes":""},"categories":[29],"tags":[20,8,9],"class_list":["post-1089","post","type-post","status-publish","format-standard","hentry","category-zerojudge-","tag-20","tag-8","tag-9"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts\/1089","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/comments?post=1089"}],"version-history":[{"count":1,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts\/1089\/revisions"}],"predecessor-version":[{"id":1090,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts\/1089\/revisions\/1090"}],"wp:attachment":[{"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/media?parent=1089"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/categories?post=1089"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/tags?post=1089"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}