{"id":603,"date":"2020-05-26T20:57:26","date_gmt":"2020-05-26T12:57:26","guid":{"rendered":"http:\/\/www.zyhcoding.club\/?p=603"},"modified":"2020-05-26T21:09:47","modified_gmt":"2020-05-26T13:09:47","slug":"%e6%9f%93%e8%89%b2%e6%b3%95%ef%bc%8c%e5%a5%87%e6%95%b0%e7%8e%af%e5%88%a4%e5%ae%9a","status":"publish","type":"post","link":"http:\/\/www.zyhcoding.club\/index.php\/2020\/05\/26\/%e6%9f%93%e8%89%b2%e6%b3%95%ef%bc%8c%e5%a5%87%e6%95%b0%e7%8e%af%e5%88%a4%e5%ae%9a\/","title":{"rendered":"\u67d3\u8272\u6cd5\uff0c\u5947\u6570\u73af\u5224\u5b9a"},"content":{"rendered":"<p>\u4e3a\u5565\u4e00\u76f4\u90fd\u6ca1\u60f3\u5230\u67d3\u8272\u6cd5\u6709\u8fd9\u4e48\u591a\u7528\u5904\u5462\uff0c\uff0c\uff0c\u867d\u7136\u80fd\u5f88\u5bb9\u6613\u5199\u51fadfs\uff0c\u4f46\u8fd8\u662f\u9898\u505a\u7684\u5c11\u3002<\/p>\n<h4>\u73af\u7684\u5224\u5b9a<\/h4>\n<p>dfs\u8fc7\u7a0b\u4e2d\uff0c\u82e5\u9047\u5230\u5f53\u524d\u6b63\u5728\u641c\u7d22\u7684\u70b9\u7684\u90bb\u63a5\u70b9\u5df2\u7ecf\u88ab\u641c\u7d22\u8fc7\u65f6\uff0c\u5219\u8bf4\u660e\u5b58\u5728\u73af\uff01\u8fd9\u662f\u9002\u7528\u4e8e\u65e0\u5411\u56fe\u7684dfs\u65b9\u6848\uff0c\u6709\u5411\u56feemmm,,\u8c8c\u4f3c\u4e5f\u662f\u3002<\/p>\n<p>\u6709\u5411\u56fe\u5224\u65ad\u73af\u7684\u5b58\u5728\uff0c\u6c42\u73af\u7684\u957f\u5ea6\u3002<br \/>\n<strong><a href=\"https:\/\/ac.nowcoder.com\/acm\/contest\/3007\/B\">\u56fe<\/a><\/strong><\/p>\n<h4>\u5947\u6570\u73af<\/h4>\n<p>\u5176\u5b9e\u5947\u6570\u73af\u4f1a\u5224\u65ad\u4e86\uff0c\u5076\u6570\u73af\u4e0d\u4e5f\u4e00\u6837\u4e48\uff0c\uff0c\uff0c<br \/>\n\u67d3\u8272\u6cd5\uff0c\u82e5\u4e00\u4e2a\u70b9\u7684\u989c\u8272\u548c\u5b83\u7684\u4e00\u4e2a\u90bb\u63a5\u70b9\u7684\u989c\u8272\u76f8\u540c\uff0c\u90a3\u4e48\u5c31\u8bf4\u660e\u6709\u5947\u6570\u73af\u5b58\u5728\uff01<br \/>\n\u5224\u65ad\u5076\u6570\u73af\u7684\u8bdd\uff0c\u5148\u5224\u65ad\u4e00\u4e0b\u662f\u5426\u6709\u73af\uff0c\u7136\u540e\u518d\u5224\u65ad\u6bcf\u4e00\u4e2a\u70b9\u7684\u989c\u8272\u548c\u5b83\u7684\u90bb\u63a5\u70b9\u7684\u989c\u8272\uff0c\u82e5\u6709\u4e0d\u51b2\u7a81\u7684\u60c5\u51b5\uff0c\u5219\u8bf4\u660e\u6709\u5076\u6570\u73af\u3002<\/p>\n<h6><a href=\"https:\/\/ac.nowcoder.com\/acm\/problem\/52275\">\u56fe\u7684\u904d\u5386<\/a><\/h6>\n<p>\u9996\u5148\u6c42\u4e00\u4e0b\u8fde\u901a\u5757\u7684\u6570\u91cfcur\uff0c\u7136\u540e\u7b54\u6848\u5148\u52a0\u4e0acur-1\uff0c\u56e0\u4e3a\u8981\u628a\u56fe\u8fde\u901a\u3002\u7136\u540e\u5224\u65ad\u6bcf\u4e2a\u8fde\u901a\u5757\u4e2d\uff0c\u662f\u5426\u5b58\u5728\u5947\u6570\u73af\u5373\u53ef\uff0c\u82e5\u5b58\u5728\uff0c\u7b54\u6848\u76f4\u63a5\u8f93\u51fa\uff1b\u5426\u5219\u7b54\u6848\u8981\u518d\u52a01\u3002<\/p>\n<pre><code class=\"language-c++\">#include &lt;bits\/stdc++.h&gt;\nusing namespace std;\nconst int maxn = 1e5+5;\nstruct edge{\n    int from, to, next;\n}e[maxn*2];\nint n, m, tot, cur, flag;\nint head[maxn], vis[maxn];\ninline void AddEdge(int from, int to){\n    tot++;\n    e[tot].from = from;\n    e[tot].to = to;\n    e[tot].next = head[from];\n    head[from] = tot;\n    return;\n}\nvoid dfs(int x, int color){\n    vis[x] = color;\n    for (int i=head[x];i;i=e[i].next){\n        if (!vis[e[i].to])\n            dfs(e[i].to, -1*color);\n        if (vis[e[i].to]==color)\n            flag = 0;\n    }\n    return;\n}\nint main(){\n    int u, v;\n    scanf (&quot;%d %d&quot;, &amp;n, &amp;m);\n    tot = cur = 0;\n    flag = 1;\n    for (int i=1;i&lt;=m;i++){\n        scanf (&quot;%d %d&quot;, &amp;u, &amp;v);\n        AddEdge(u, v);\n        AddEdge(v, u);\n    }\n    for (int i=1;i&lt;=n;i++)  if (vis[i]==0){\n        cur++;\n        dfs(i, 1);\n    }\n    printf (&quot;%d&quot;, cur-1+flag);\n    return 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e3a\u5565\u4e00\u76f4\u90fd\u6ca1\u60f3\u5230\u67d3\u8272\u6cd5\u6709\u8fd9\u4e48\u591a\u7528\u5904\u5462\uff0c\uff0c\uff0c\u867d\u7136\u80fd\u5f88\u5bb9\u6613\u5199\u51fadfs\uff0c\u4f46\u8fd8\u662f\u9898\u505a\u7684\u5c11\u3002 \u73af\u7684\u5224\u5b9a dfs [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[45],"tags":[],"_links":{"self":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/603"}],"collection":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/comments?post=603"}],"version-history":[{"count":5,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/603\/revisions"}],"predecessor-version":[{"id":608,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/603\/revisions\/608"}],"wp:attachment":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/media?parent=603"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/categories?post=603"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/tags?post=603"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}