{"id":47,"date":"2016-05-27T14:11:19","date_gmt":"2016-05-27T06:11:19","guid":{"rendered":"http:\/\/tyswly.com\/?p=47"},"modified":"2016-05-27T14:11:19","modified_gmt":"2016-05-27T06:11:19","slug":"haoi2006%e5%8f%97%e6%ac%a2%e8%bf%8e%e7%9a%84%e7%89%9b","status":"publish","type":"post","link":"https:\/\/tys.fun\/?p=47","title":{"rendered":"[HAOI2006]\u53d7\u6b22\u8fce\u7684\u725b"},"content":{"rendered":"<p>\u539f\u9898\u89c1<a href=\"http:\/\/cogs.pro\/cogs\/problem\/problem.php?pid=1309\">COGS<\/a><br \/>\ntarjan\u7684\u5178\u578b\u9898\u3001\u5165\u95e8\u9898\uff0c\u771f\u7684\u662f\u88f8\u7684tarjan\u3002<br \/>\n\u8fd9\u9053\u9898\u5c31\u662f\u5148\u641c\u7d22\u5f3a\u8fde\u901a\uff0c\u518d\u7edf\u8ba1\u5f3a\u8fde\u901a\u7684\u51fa\u5ea6\uff0c\u5982\u679c\u6709\u4e24\u4e2a\u6216\u4ee5\u4e0a\u51fa\u5ea6\u4e3a\u96f6\u7684\u5f3a\u8fde\u901a\uff0c\u5219\u6b64\u9898\u65e0\u89e3\u3002<br \/>\n\u6700\u540e\u8f93\u51fa\u552f\u4e00\u7684\u5f3a\u8fde\u901a\u7684\u5927\u5c0f\uff0c\u4e5f\u5c31\u662f\u5b83\u5185\u90e8\u7684\u70b9\u7684\u4e2a\u6570\u3002<br \/>\n\u4ee3\u7801\u63a8\u8350\u4ee5\u65b0\u9875\u9762\u6253\u5f00\uff0c\u56e0\u4e3a\u6709\u4e9b\u6ce8\u91ca\u6253\u7684\u6709\u70b9\u957f\u3002\u3002\/\/\u8bdd\u8bf4\u8fd9\u662f\u6211\u7b2c\u4e00\u6b21<del>\u5199\u6ce8\u91ca<\/del>\u628a\u6ce8\u91ca\u6253\u8fd9\u4e48\u8be6\u7ec6\u554a<\/p>\n<pre class=\"lang:c++ decode:true \">#include &lt;iostream&gt;\n#include &lt;cstdio&gt;\n#include &lt;algorithm&gt;\nconst int MX = 10005;\nusing namespace std;\nint low[MX], dfn[MX], cnt[MX], n, m, be[MX], sccn;\nbool out[MX];\nclass stack {\/\/\u624b\u5199\u6808\nprivate:\n\tint s[MX];\n\tint top_;\n\tbool instack[MX];\npublic:\n\tint top() {\n\t\treturn s[top_];\n\t}\n\tvoid pop() {\n\t\tinstack[s[top_]] = false;\n\t\ttop_--;\n\t}\n\tvoid push(int num) {\n\t\tinstack[num] = true;\n\t\ts[++top_] = num;\n\t}\n\tbool in(int num) {\n\t\treturn instack[num];\n\t}\n}s;\nstruct edge {\n\tint to, ne;\n}e[MX * 5];\/\/\u90bb\u63a5\u8868\u5b58\u8fb9\nint head[MX], tot;\nvoid tarjan(int now) {\n\tint to;\n\tlow[now] = dfn[now] = ++tot;\/\/dfn\u4e3a\u5f53\u524d\u6587\u4ef6\u5e8f\u53f7, low\u662f\u4e00\u4e2atarjan\u72ec\u6709\u7684\u7279\u6b8a\u6570\u7ec4\u3002\u3002\u3002\n\ts.push(now);\n\tfor (int i = head[now]; i; i = e[i].ne) {\n\t\tto = e[i].to;\n\t\tif (!dfn[to]) {\/\/\u5982\u679c\u4e4b\u524d\u6ca1\u6709\u8bbf\u95ee\u8fc7\u8be5\u8282\u70b9\n\t\t\ttarjan(to);\/\/\u5faa\u73af\u8fdb\u884c\u6df1\u641c\n\t\t\tlow[now] = min(low[to], low[now]);\/\/\u66f4\u65b0low\u503c\n\t\t}\n\t\telse if (s.in(to)) {\/\/\u5df2\u7ecf\u8bbf\u95ee\u8fc7\u8be5\u8282\u70b9\uff0c\u5e76\u4e14\u662f\u5728\u8fd9\u6b21\u6df1\u641c\u4e2d\u8bbf\u95ee\u7684\uff0c\u8868\u660e\u6709\u73af\n\t\t\tlow[now] = min(low[to], low[now]);\/\/\u66f4\u65b0low\u503c\n\t\t}\n\t}\n\tif (dfn[now] == low[now]) {\/\/\u8fdb\u884c\u7f29\u70b9\n\t\t\tint tmp;\n\t\t\tsccn++;\/\/\u5f3a\u8fde\u901a\u7684\u7f16\u53f7\n\t\t\tdo {\n\t\t\t\ttmp = s.top();\n\t\t\t\ts.pop();\n\t\t\t\tcnt[sccn]++;\n\t\t\t\tbe[tmp] = sccn;\/\/tmp\u5c5e\u4e8e\u7684\u5f3a\u8fde\u901a\n\t\t\t}while (tmp != now);\n\t}\n}\nvoid addedge(int x0, int y0) {\/\/\u90bb\u63a5\u8868\u52a0\u8fb9\n\ttot++;\n\te[tot].to = y0;\n\te[tot].ne = head[x0];\n\thead[x0] = tot;\n}\nint get_num() {\/\/\u65e5\u5e38\u8bfb\u5165\u4f18\u5316\n\tint ans = 0;\n\tchar tmp;\n\ttmp = getchar();\n\twhile (tmp &gt; '9' || tmp &lt; '0')\ttmp = getchar();\n\twhile (tmp &lt;= '9' &amp;&amp; tmp &gt;= '0') {\n\t\tans = ans * 10 + tmp - 48;\n\t\ttmp = getchar();\n\t}\n\treturn ans;\n}\nint Main() {\n\t\/\/freopen(\"cow.in\", \"r\", stdin);\n\t\/\/freopen(\"cow.out\", \"w\", stdout);\n\tn = get_num();\n\tm = get_num();\n\tint x, y;\n\tfor (int i = 1; i &lt;= m; i++) {\n\t\tx = get_num();\n\t\ty = get_num();\n\t\taddedge(x, y);\n\t}\n\tfor (int i = 1; i &lt;= n; i++) {\n\t\tif(!dfn[i]) tarjan(i);\/\/\u5982\u679c\u8be5\u8282\u70b9\u5e76\u6ca1\u6709\u88ab\u8bbf\u95ee\u8fc7\uff0c\u8fdb\u884c\u6df1\u641c\u904d\u5386\n\t}\n\tfor (int i = 1; i &lt;= n; i++) {\n\t\tfor (int j = head[i]; j; j = e[j].ne) {\n\t\t\tif (be[i] != be[e[j].to]) {\/\/\u5982\u679c\u8be5\u8282\u70b9\u548c\u5b83\u6307\u5411\u7684\u8282\u70b9\u4e0d\u5c5e\u4e8e\u540c\u4e00\u4e2a\u5f3a\u8fde\u901a\n\t\t\t\tout[be[i]] = true;\/\/\u5c31\u8bf4\u660e\u8fd9\u4e2a\u5f3a\u8fde\u901a\u6709\u51fa\u5ea6\n\t\t\t\tbreak;\/\/\u7136\u540e\u8bc1\u660e\u8fd9\u4e2a\u8282\u70b9\u4e0d\u662f\u6211\u4eec\u8981\u627e\u7684\u7b54\u6848\uff0c\u53ef\u4ee5\u76f4\u63a5\u904d\u5386\u4e0b\u4e00\u4e2a\u8282\u70b9\n\t\t\t}\n\t\t}\n\t}\n\tint tmp = 0;\n\tint ans = 0;\n\tfor (int i = 1; i &lt;= sccn; i++) {\n\t\tif (!out[i]) {\/\/\u5982\u679c\u8fd9\u4e2a\u8282\u70b9\u6ca1\u6709\u51fa\u5ea6\n\t\t\tans = cnt[i];\/\/\u66f4\u65b0\u7b54\u6848\n\t\t\ttmp++;\/\/\u6ca1\u6709\u51fa\u5ea6\u7684\u5f3a\u8fde\u901a\u7684\u4e2a\u6570\n\t\t}\n\t\tif (tmp == 2) {\/\/\u5982\u679c\u6709\u4e00\u4e2a\u4ee5\u4e0a\u7684\u6ca1\u6709\u51fa\u5ea6\u7684\u5f3a\u8fde\u901a\uff0c\u5219\u6b64\u9898\u65e0\u89e3\n\t\t\tcout &lt;&lt; \"0\" &lt;&lt; endl;\n\t\t\treturn 0;\n\t\t}\n\t}\n\tcout &lt;&lt; ans &lt;&lt; endl;\n\treturn 0;\n}\nint AA = Main();\/\/\u5bf9\u4e8eCOGS\u7684\u4e00\u70b9\u795e\u5947\u7684\u4f18\u5316\uff0c\u81f3\u4eca\u4e0d\u660e\u539f\u7406\nint main() {;}\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u9898\u89c1COGS tarjan\u7684\u5178\u578b\u9898\u3001\u5165\u95e8\u9898\uff0c\u771f\u7684\u662f\u88f8\u7684tarjan\u3002 \u8fd9\u9053\u9898\u5c31\u662f\u5148\u641c\u7d22\u5f3a\u8fde\u901a\uff0c\u518d\u7edf\u8ba1\u5f3a\u8fde\u901a\u7684 &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-47","post","type-post","status-publish","format-standard","hentry","category-report"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/tys.fun\/index.php?rest_route=\/wp\/v2\/posts\/47","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tys.fun\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tys.fun\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tys.fun\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tys.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=47"}],"version-history":[{"count":0,"href":"https:\/\/tys.fun\/index.php?rest_route=\/wp\/v2\/posts\/47\/revisions"}],"wp:attachment":[{"href":"https:\/\/tys.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=47"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tys.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=47"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tys.fun\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=47"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}