{"id":149,"date":"2026-05-16T11:17:33","date_gmt":"2026-05-16T03:17:33","guid":{"rendered":"https:\/\/huxiaole.cloud\/?p=149"},"modified":"2026-05-16T11:17:34","modified_gmt":"2026-05-16T03:17:34","slug":"d-alternating-path","status":"publish","type":"post","link":"https:\/\/huxiaole.cloud\/index.php\/2026\/05\/16\/d-alternating-path\/","title":{"rendered":"D. Alternating Path"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u7ed9\u5b9a\u4e00\u4e2a\u5305\u542b n \u4e2a\u9876\u70b9\u548c m \u6761\u8fb9\u7684<strong>\u65e0\u5411\u56fe<\/strong>\u3002\u9876\u70b9\u7f16\u53f7\u4e3a 1 \u5230 n \u3002\u56fe\u4e2d\u6ca1\u6709\u81ea\u73af\u6216\u91cd\u8fb9\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u7684\u4efb\u52a1\u662f\u901a\u8fc7\u4e3a\u6bcf\u6761\u8fb9\u9009\u62e9\u4e00\u4e2a\u65b9\u5411\uff0c\u5c06\u56fe\u53d8\u4e3a<strong>\u6709\u5411\u56fe<\/strong>\u3002\u5b9a\u5411\u4e4b\u540e\uff0c\u5b9a\u4e49\u4e00\u4e2a\u9876\u70b9\u5e8f\u5217 v1\u200b,v2\u200b,\u2026,vk\u200b \uff08\u5176\u4e2d k \u53ef\u4ee5\u4efb\u610f\u5927\uff0c\u4efb\u4f55\u9876\u70b9\u53ef\u4ee5\u91cd\u590d\u4efb\u610f\u6b21\u6570\uff09\u4e3a<strong>\u4ea4\u66ff\u8def\u5f84<\/strong>\uff0c\u5982\u679c\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8fb9 (v1\u200b,v2\u200b) \u4ece v1\u200b \u6307\u5411 v2\u200b<br><\/li>\n\n\n\n<li>\u8fb9 (v2\u200b,v3\u200b) \u4ece v3\u200b \u6307\u5411 v2\u200b<br><\/li>\n\n\n\n<li>\u8fb9 (v3\u200b,v4\u200b) \u4ece v3\u200b \u6307\u5411 v4\u200b<br><\/li>\n\n\n\n<li>\u8fb9 (v4\u200b,v5\u200b) \u4ece v5\u200b \u6307\u5411 v4\u200b<br><\/li>\n\n\n\n<li>\u4ee5\u6b64\u7c7b\u63a8\u2026\u2026<br><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u5373\u8fb9\u7684\u65b9\u5411\u4ea4\u66ff\u4e3a\uff1a\u2192, \u2190, \u2192, \u2190, \u2026<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4e00\u4e2a\u9876\u70b9 v \u6ee1\u8db3\uff1a<strong>\u539f\u56fe\u4e2d\u4ece v \u51fa\u53d1\u7684\u4efb\u610f\u8def\u5f84<\/strong>\uff08\u4e0d\u4e00\u5b9a\u662f\u7b80\u5355\u8def\u5f84\uff09\u5728\u5b9a\u5411\u540e\u7684\u6709\u5411\u56fe\u4e2d\u90fd\u662f<strong>\u4ea4\u66ff\u8def\u5f84<\/strong>\uff0c\u5219\u79f0 v \u4e3a<strong>\u7f8e\u4e3d\u9876\u70b9<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6c42\uff1a\u5728\u5b9a\u5411\u8fb9\u4e4b\u540e\uff0c\u6700\u591a\u80fd\u4f7f\u591a\u5c11\u4e2a\u9876\u70b9\u6210\u4e3a\u7f8e\u4e3d\u9876\u70b9\uff1f<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4ee3\u7801<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Sunshine, sunshine, ladybugs awake!\n\/\/ Clap your hooves and do a little shake!\n#include &lt;bits\/stdc++.h&gt;\n\/\/#define int long long\n#define endl '\\n'\nusing namespace std;\n\nusing PII=pair&lt;int,int&gt; ;\n\/\/inline static constexpr int MAXN=100010;\n\nconst int MAXN=200010;\nvector&lt;int&gt; adj&#91;MAXN];\n\nvoid solve() {\n\tint n,m;\n\tcin&gt;&gt;n&gt;&gt;m;\n\t\n\tfor(int i=1;i&lt;=n;i++)adj&#91;i].clear();\n\t\n\tfor(int i=0;i&lt;m;i++){\n\t\tint u,v;\n\t\tcin&gt;&gt;u&gt;&gt;v;\n\t\tadj&#91;u].push_back(v);\n\t\tadj&#91;v].push_back(u);\n\t}\n\t\n\tvector&lt;int&gt; f(n+1,-1);\n\tint ans=0;\n\t\n\tfor(int i=1;i&lt;=n;i++){\n\t\tif(f&#91;i]!=-1) continue;\n\t\t\n\t\tqueue&lt;int&gt; q;\n\t\tq.push(i);\n\t\tf&#91;i]=0;\n\t\t\n\t\tint cnt&#91;2]={1,0};\n\t\tbool flag=true;\n\t\t\n\t\twhile(!q.empty()){\n\t\t\tint u=q.front();\n\t\t\tq.pop();\n\t\t\t\n\t\t\tfor(auto v:adj&#91;u]){\n\t\t\t\tif(f&#91;v]==-1){\n\t\t\t\t\tf&#91;v]=1^f&#91;u];\n\t\t\t\t\tcnt&#91;f&#91;v]]++;\n\t\t\t\t\tq.push(v);\n\t\t\t\t}else if(f&#91;v]==f&#91;u]){\n\t\t\t\t\tflag=false;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif(flag){\n\t\t\tans+=max(cnt&#91;0],cnt&#91;1]);\n\t\t}\n\t}\n\tcout&lt;&lt;ans&lt;&lt;endl;\n}\n\nsigned main() {\n\tios_base::sync_with_stdio(false);\n\tcin.tie(NULL);\n\tint t;cin &gt;&gt; t;while (t--) \n\t\tsolve();\n\treturn 0;\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u5305\u542b n \u4e2a\u9876\u70b9\u548c m \u6761\u8fb9\u7684\u65e0\u5411\u56fe\u3002\u9876\u70b9\u7f16\u53f7\u4e3a 1 \u5230 n \u3002\u56fe\u4e2d\u6ca1\u6709\u81ea\u73af\u6216\u91cd\u8fb9\u3002 \u4f60\u7684\u4efb\u52a1\u662f\u901a\u8fc7\u4e3a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":93,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22,12],"tags":[],"class_list":["post-149","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-pta","category-12"],"_links":{"self":[{"href":"https:\/\/huxiaole.cloud\/index.php\/wp-json\/wp\/v2\/posts\/149","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/huxiaole.cloud\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/huxiaole.cloud\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/huxiaole.cloud\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/huxiaole.cloud\/index.php\/wp-json\/wp\/v2\/comments?post=149"}],"version-history":[{"count":1,"href":"https:\/\/huxiaole.cloud\/index.php\/wp-json\/wp\/v2\/posts\/149\/revisions"}],"predecessor-version":[{"id":150,"href":"https:\/\/huxiaole.cloud\/index.php\/wp-json\/wp\/v2\/posts\/149\/revisions\/150"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/huxiaole.cloud\/index.php\/wp-json\/wp\/v2\/media\/93"}],"wp:attachment":[{"href":"https:\/\/huxiaole.cloud\/index.php\/wp-json\/wp\/v2\/media?parent=149"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/huxiaole.cloud\/index.php\/wp-json\/wp\/v2\/categories?post=149"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/huxiaole.cloud\/index.php\/wp-json\/wp\/v2\/tags?post=149"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}