{"id":534,"date":"2010-11-12T23:22:59","date_gmt":"2010-11-12T15:22:59","guid":{"rendered":"http:\/\/comzyh.tk\/blog\/?p=534"},"modified":"2014-07-28T18:10:45","modified_gmt":"2014-07-28T10:10:45","slug":"poj-2186-1236-%e5%bc%ba%e8%bf%9e%e9%80%9a%e5%88%86%e9%87%8f-tarjan%e7%ae%97%e6%b3%95","status":"publish","type":"post","link":"https:\/\/comzyh.com\/blog\/archives\/534\/","title":{"rendered":"POJ 2186 &#038; 1236 \u5f3a\u8fde\u901a\u5206\u91cf Tarjan\u7b97\u6cd5"},"content":{"rendered":"<p>\u5148\u7ed9\u94fe\u63a5<a title=\"Popular Cows\" href=\"http:\/\/poj.org\/problem?id=2186\" target=\"_blank\">POJ 2186 Popular Cows<\/a> ,<a title=\"Network of Schools\" href=\"http:\/\/poj.org\/problem?id=1236\" target=\"_blank\">POJ 1236 Network of Schools<\/a><br \/>\n\u8fd9\u4e24\u9053\u9898\u90fd\u662f\u7ecf\u5178\u7684\u5f3a\u8fde\u901a\u5206\u91cf\u95ee\u9898,\u672c\u4eba\u90fd\u91c7\u7528\u4e86\u4f1f\u5927\u7684SCC Tarjan\u7b97\u6cd5.<br \/>\n\u5173\u4e8eSCC Tarjan\u7b97\u6cd5 \u53ef\u4ee5\u53c2\u89c1\u672c\u535a\u7684<a title=\"\u5904\u7406SCC(\u5f3a\u8fde\u901a\u5206\u91cf\u95ee\u9898)\u7684Tarjan\u7b97\u6cd5\" href=\"http:\/\/comzyh.com\/blog\/archives\/517\/\" target=\"_blank\">\u5904\u7406SCC(\u5f3a\u8fde\u901a\u5206\u91cf\u95ee\u9898)\u7684Tarjan\u7b97\u6cd5<\/a><\/p>\n<h5>\u5148\u8bf4POJ 2186 Popular Cows<\/h5>\n<p>(<a title=\"POJ 2186 &amp; 1236 \u5f3a\u8fde\u901a\u5206\u91cf Tarjan\u7b97\u6cd5\" href=\"http:\/\/comzyh.com\/blog\/archives\/534\/\">\u4e0d\u770b\u8fd9\u9898,\u76f4\u63a5\u8df3\u52301236\u7684\u9898\u89e3<\/a>)<br \/>\n\u9898\u610f:\u6bcf\u4e2aCow\u90fd\u68a6\u60f3\u6210\u4e3a\u725b\u7fa4\u91cc\u6700\u77e5\u540d\u7684\u5976\u725b,\u5728\u4e00\u4e2a\u6709N(1 &lt;= N &lt;= 10,000) \u5934\u725b\u7684\u725b\u7fa4\u91cc,\u7ed9\u51fa\u6700\u591aM(1 &lt;= M &lt;= 50,000) \u4e2a\u6570\u5bf9(A,B)\u544a\u8bc9\u4f60A\u8ba4\u4e3aB\u662f\u6709\u540d\u7684,\u5e76\u4e14\u540d\u6c14\u53ef\u4ee5\u4f20\u9012,\u82e5\u679cA\u8ba4\u4e3aB\u6709\u540d,B\u8ba4\u4e3aC\u6709\u540d,\u5219A\u4e5f\u4f1a\u8ba4\u4e3aC\u6709\u540d,\u5373\u4f7f\u5728\u8f93\u5165\u7684\u6570\u5bf9\u4e2d\u6ca1\u6709\u7ed9\u51fa\u8fd9\u6bb5\u5173\u7cfb,\u4f60\u7684\u4efb\u52a1\u662f\u8ba1\u7b97\u51fa\u88ab\u6240\u6709\u725b\u8ba4\u4e3a\u662f\u6709\u540d\u7684\u725b\u7684\u4e2a\u6570.<\/p>\n<p>\u5f15\u7528\u53e6\u4e00\u4f4d\u4ec1\u5144<a href=\"http:\/\/www.cppblog.com\/RyanWang\/\">Headacher<\/a>\u7684\u9898\u89e3\u4e2d\u7684\u4e00\u90e8\u5206(\u53c2\u89c1<a href=\"http:\/\/www.cppblog.com\/RyanWang\/archive\/2009\/02\/26\/74984.html\" target=\"_blank\">PKU POJ 2186 Popular Cows \u5f3a\u8fde\u901a\u5206\u91cf<\/a> ),\u8bb2\u7684\u5f88\u597d,\u5f88\u7cbe\u8f9f<\/p>\n<blockquote><p>\u7b97\u6cd5\u8bc1\u660e\uff1a<\/p>\n<p>1\uff1a\u5047\u8bbea\u548cb\u90fd\u662f\u6700\u53d7\u6b22\u8fce\u7684cow\uff0c\u90a3\u4e48\uff0ca\u6b22\u8fceb\uff0c\u800c\u4e14b\u6b22\u8fcea\uff0c\u4e8e\u662f\uff0ca\u548cb\u662f\u5c5e\u4e8e\u540c\u4e00\u4e2a\u8fde\u901a\u5206\u91cf\u5185\u7684\u70b9\uff0c\u6240\u6709\uff0c\u95ee\u9898\u7684\u89e3\u96c6\u6784\u6210\u4e00\u4e2a\u5f3a\u8fde\u901a\u5206\u91cf\u3002<\/p>\n<p>2\uff1a\u5982\u679c\u67d0\u4e2a\u5f3a\u8fde\u901a\u5206\u91cf\u5185\u7684\u70b9a\u5230\u5f3a\u8fde\u901a\u5206\u91cf\u5916\u7684\u70b9b\u6709\u901a\u8def\uff0c\u56e0\u4e3ab\u548ca\u4e0d\u662f\u540c\u4e00\u4e2a\u5f3a\u8fde\u901a\u5206\u91cf\u5185\u7684\u70b9\uff0c\u6240\u4ee5b\u5230a\u4e00\u5b9a\u6ca1\u6709\u901a\u8def\uff0c\u90a3\u4e48a\u4e0d\u88abb\u6b22\u8fce\uff0c\u4e8e\u662fa\u6240\u5728\u7684\u8fde\u901a\u5206\u91cf\u4e00\u5b9a\u4e0d\u662f\u89e3\u96c6\u7684\u90a3\u4e2a\u8fde\u901a\u5206\u91cf\u3002<\/p>\n<p>3\uff1a\u5982\u679c\u5b58\u5728\u4e24\u4e2a\u72ec\u7acb\u7684\u5f3a\u8fde\u901a\u5206\u91cfa\u548cb\uff0c\u90a3\u4e48a\u5185\u7684\u70b9\u548cb\u5185\u7684\u70b9\u4e00\u5b9a\u4e0d\u80fd\u4e92\u76f8\u5230\u8fbe\uff0c\u90a3\u4e48\uff0c\u65e0\u8bba\u662fa\u8fd8\u662fb\u90fd\u4e0d\u662f\u89e3\u96c6\u7684\u90a3\u4e2a\u8fde\u901a\u5206\u91cf\uff0c\u95ee\u9898\u4fdd\u8bc1\u65e0\u89e3\u3002<\/p>\n<p>4\uff1a\u5982\u679c\u56fe\u975e\u8fde\u901a\uff0c\u90a3\u4e48\uff0c\u81f3\u5c11\u5b58\u5728\u4e24\u4e2a\u72ec\u7acb\u7684\u8fde\u901a\u5206\u91cf\uff0c\u95ee\u9898\u4e00\u5b9a\u65e0\u89e3\u3002<\/p><\/blockquote>\n<p>\u8fd9\u6837\u6211\u4eec\u4fbf\u7684\u5f97\u5230\u4e86\u521d\u6b65\u7684\u89e3\u51b3\u65b9\u6848<\/p>\n<ol>\n<li>\u7528Tarjan\u7b97\u6cd5\u6c42\u51fa\u5f3a\u8fde\u901a\u5206\u91cf,\u8bbe\u7acbBelong\u6570\u7ec4,\u7528\u5e76\u67e5\u96c6\u5c06\u5728\u540c\u4e00\u5f3a\u8fde\u901a\u5206\u91cf\u7684\u8282\u70b9\u5e76\u5728\u4e00\u8d77.<\/li>\n<li>\u5efa\u6811,\u904d\u5386\u6240\u6709\u8f93\u5165\u4e2d\u7684\u70b9\u5bf9(A,B)\u5982\u679cAB\u5206\u5c5e\u4e24\u4e2a\u4e0d\u540c\u7684\u5f3a\u8fde\u901a\u5206\u91cf,\u5219belong[B]\u662fbelong[A]\u7684\u7236\u8282\u70b9<\/li>\n<li>\u663e\u7136,\u5982\u679c\u70b9\u5bf9(A,B),<strong>A\u6240\u5728\u7684\u5f3a\u8fde\u901a\u5206\u91cf<\/strong>(\u7528\u5e76\u67e5\u96c6\u6307\u5b9a,getf(A))\u6307\u5411\u7684\u5143\u7d20\u548c&#8221;<strong>A\u6307\u5411\u7684\u5f3a\u8fde\u901a\u5206\u91cf<\/strong>&#8220;\u4e0d\u5c5e\u4e8e\u540c\u4e00\u5f3a\u8fde\u901a\u5206\u91cf,\u5219\u4e0d\u6784\u6210\u6811\u7ed3\u6784,\u65e0\u89e3.<\/li>\n<li>\u904d\u5386\u6240\u6709\u5f3a\u8fde\u901a\u5206\u91cf,\u5982\u679c\u5b58\u5728\u4e00\u4e2a\u5f3a\u8fde\u901a\u5206\u91cf\u6307\u5411\u81ea\u5df1,\u5219\u5b83\u662f\u6700\u8457\u540d\u7684\u5f3a\u8fde\u901a\u5206\u91cf<\/li>\n<li>\u904d\u5386\u6240\u6709\u8282\u70b9,\u5982\u679c\u5c5e\u4e8e\u7b54\u6848\u5f3a\u8fde\u901a\u5206\u91cf,\u8ba1\u6570\u5668\u7d2f\u52a0,\u6700\u540e\u8f93\u51fa\u7b54\u6848<\/li>\n<\/ol>\n<p>\u672c\u4eba\u7684\u4ee3\u7801\u5f88\u4e11,\u7528\u7684\u524d\u5411\u661f,\u7b2c\u4e00\u6b21\u5199\u524d\u5411\u661f,\u88abyxc\u540c\u5fd7\u6307\u4e3a&#8221;\u5149\u5efa\u56fe\u6392\u5e8f\u5c31\u8981\\(N\\log (N)\\)&#8220;\u6240\u4ee5\u7528\u7684\u8ba1\u6570\u6392\u5e8f,\u5efa\u56fe\u90e8\u5206\u662f\u4e3a\u4e86\u524d\u5411\u661f\u800c\u524d\u5411\u661f,\u76f4\u63a5\u65e0\u89c6\u5373\u53ef<br \/>\n<!--more--><br \/>\nMY CODE:<\/p>\n<pre class=\"lang:default decode:true\">\/*\r\nProblem: 2186\t\tUser: comzyh\r\nMemory: 1204K\t\tTime: 63MS\r\nLanguage: C++\t\tResult: Accepted\r\n*\/\r\n#include &lt;cstdio&gt;\r\n#include &lt;cstring&gt;\r\n#include &lt;cstdlib&gt;\r\nstruct think{\r\n       int f,t;\r\n       };\r\nint N,M,R;\r\nthink link0[50009],link[50009];\r\nint times[10009],timesr[10009],first[10009];\/\/\u8ba1\u6570\u6392\u5e8f \r\n\/\/SCC Tarjan\r\nint low[10009],dfn[10009],instack[10009],belong[10009];\r\nint father[10009];\/\/\u4e0b\u6807\u548c\u5143\u7d20\u5747\u662f\u5f3a\u8fde\u901a\u5206\u91cf\u7f16\u53f7 \r\n\/\/Count\r\nint NOANS,ANS,NANS;\r\nint count[10009];\r\nint f[10009];\r\nint stap[50009];\r\nint top,nSCC,rtime\/*\u65f6\u95f4\u6233*\/;\r\nvoid tarjan(int x);\r\nint getf(int x);\r\nvoid add(int x,int y);\/\/add x to y\r\nint main(){\r\n    int i,j;\r\n    scanf(\"%d%d\",&amp;N,&amp;M);\r\n    memset(times,0,sizeof(times));\r\n    \/\/\u4ee5\u4e0b\u5927\u6bb5\u8ba1\u6570\u6392\u5e8f,\u6784\u56fe(\u524d\u5411\u661f) \r\n    for (i=1;i&lt;=M;i++){\r\n        scanf(\"%d%d\",&amp;link0[i].f,&amp;link0[i].t);     \r\n        times[link0[i].f]++;\r\n        }\r\n    \r\n    for (i=1;i&lt;=N;i++){\r\n        times[i]+=times[i-1];\r\n        timesr[i]=times[i-1];\r\n        first[i]=times[i-1]+1;               \r\n        }\/\/\u53d6\u503c\u65f6\u4f7f\u7528 \r\n    for (i=1;i&lt;=M;i++)\r\n        link[++timesr[link0[i].f]]=link0[i];\r\n    \/\/\u8ba1\u6570\u6392\u5e8f\u5b8c\u6210 \r\n    \/\/Tarjan\r\n    top=nSCC=rtime=0;\r\n    memset(dfn,0,sizeof(dfn));\r\n    for (i=1;i&lt;=N;i++)\/\/\u521d\u59cb\u5316\u5e76\u67e5\u96c6 \r\n        f[i]=i;\r\n    for (i=1;i&lt;=N;i++)\r\n        if (!dfn[i])\r\n           tarjan(i);\r\n     \r\n    NOANS=0;\/\/\u4e0d\u662f\u65e0\u89e3 \r\n    for (i=1;i&lt;=nSCC;i++)\/\/\u4f7f\u7528\u5e76\u67e5\u96c6\u601d\u60f3,\u5b58\u50a8\u6811 \r\n        father[i]=i;\r\n    \/\/\u5904\u7406\u6240\u6709\u6570\u5bf9 (A,B) \u4e0d\u540c\u5f3a\u8fde\u901a\u5206\u91cf\u4e4b\u95f4\u7684\u4efb\u610f\u6709\u5411\u8fb9\u4e3a\u5f3a\u8fde\u901a\u5206\u91cf\u4e4b\u95f4\u7684\u6709\u5411\u8fb9 \r\n    for (i=1;i&lt;=M;i++)\r\n        if (getf(link[i].f)!=getf(link[i].t)){\/\/\u5c5e\u4e8e\u4e0d\u540c\u5f3a\u8fde\u901a\u5206\u91cf \r\n           if (father[belong[link[i].f]]==belong[link[i].f]){\r\n              father[belong[link[i].f]]=belong[link[i].t];\/\/\u5efa\u6811\u7684\u8fc7\u7a0b \r\n              continue;\r\n              }else if (father[belong[link[i].f]]!=belong[link[i].t]){\r\n                    \/\/\u5982\u679c\u4ee5\u4e2a\u5f3a\u8fde\u901a\u5206\u91cf\u6709\u51fa\u5ea6 &gt;1\u8bf4\u660e\u8be5\u9898\u65e0\u89e3 \r\n                    NOANS=1;\/\/\u65e0\u89e3 \r\n                    break;\r\n                    }\r\n           father[belong[link[i].f]]=belong[link[i].t];\r\n           }\r\n    \/*\r\n    for (i=1;i&lt;=nSCC;i++)\r\n        printf(\"%3d 's Father=%4d\\n\",i,father[i]);\r\n    *\/\r\n    NANS=0;\r\n    for (i=1;i&lt;=nSCC;i++)\r\n        if (father[i]==i){\/\/\u6700\u7ec8Father\u662f\u81ea\u5df1\u7684\u53ea\u6709\u6700\u6709\u540d\u7684\u5f3a\u8fde\u901a\u5206\u91cf \r\n           ANS=i;\r\n           NANS++;\r\n           }\r\n    if (NOANS || (NANS&gt;1))\r\n       printf(\"0\");\r\n       else \r\n       printf(\"%d\",count[ANS]);\u8f93\u51fa \r\n    system(\"pause\");\r\n    }\r\nvoid tarjan(int x){\/\/\u8fd9\u4e2a\u53c2\u89c1Comzyh\u7684Tarjan\u8bb2\u89e3 \r\n     int i,t;\r\n     dfn[x]=low[x]=++rtime;\r\n     instack[x]=1;\r\n     stap[++top]=x;\r\n     for (i=first[x];i&lt;=timesr[x];i++){\r\n         t=link[i].t;\r\n         if (!dfn[t]){\r\n            tarjan(t);\r\n            if (low[t]&lt;low[x])\r\n               low[x]=low[t];\r\n            }else\r\n            if (instack[t] &amp;&amp; (dfn[t]&lt;low[x]))\r\n               low[x]=dfn[t];\r\n         }\r\n     if (dfn[x]==low[x]){\r\n        nSCC++;\r\n        do {\r\n           count[nSCC]++;\r\n           t=stap[top--];\r\n           add(t,x);\r\n           belong[t]=nSCC;\r\n           instack[t]=0;\r\n           }while (t!=x);\r\n        }\r\n     }\r\nint getf(int x){\/\/\u5e76\u67e5\u96c6 \r\n    if (f[x]==x)    \r\n       return x;\r\n    return f[x]=getf(f[x]);\r\n    }\r\nvoid add(int x,int y){\/\/add x to y                                \r\n    if (getf(x)==getf(y))\r\n       return ;\r\n    f[getf(x)]=getf(y);\r\n    }<\/pre>\n<p><a id=\"Comzyh-private-1236\" name=\"Comzyh-private-1236\"><\/a><\/p>\n<h5>\u518d\u8bf4POJ 1236 Network of Schools<\/h5>\n<p>\u9898\u610f\u53ca\u89e3\u6cd5(\u8f6c\u81ea<a href=\"http:\/\/www.cppblog.com\/mythit\/archive\/2009\/05\/25\/85718.html\" target=\"_blank\">\u6781\u9650\u5b9a\u5f8b<\/a>),\u5199\u7684\u5f88\u597d<\/p>\n<blockquote><p>\u9898\u76ee\u5927\u610f:N(2&lt;N&lt;100)\u5404\u5b66\u6821\u4e4b\u95f4\u6709\u5355\u5411\u7684\u7f51\u7edc,\u6bcf\u4e2a\u5b66\u6821\u5f97\u5230\u4e00\u5957\u8f6f\u4ef6\u540e,\u53ef\u4ee5\u901a\u8fc7\u5355\u5411\u7f51\u7edc\u5411\u5468\u8fb9\u7684\u5b66\u6821\u4f20\u8f93,\u95ee\u98981\uff1a\u521d\u59cb\u81f3\u5c11\u9700\u8981\u5411\u591a\u5c11\u4e2a\u5b66\u6821\u53d1\u653e\u8f6f\u4ef6,\u4f7f\u5f97\u7f51\u7edc\u5185\u6240\u6709\u7684\u5b66\u6821\u6700\u7ec8\u90fd\u80fd\u5f97\u5230\u8f6f\u4ef6.2,\u81f3\u5c11\u9700\u8981\u6dfb\u52a0\u51e0\u6761\u4f20\u8f93\u7ebf\u8def(\u8fb9),\u4f7f\u4efb\u610f\u5411\u4e00\u4e2a\u5b66\u6821\u53d1\u653e\u8f6f\u4ef6\u540e,\u7ecf\u8fc7\u82e5\u5e72\u6b21\u4f20\u9001,\u7f51\u7edc\u5185\u6240\u6709\u7684\u5b66\u6821\u6700\u7ec8\u90fd\u80fd\u5f97\u5230\u8f6f\u4ef6\u3002<\/p>\n<p>\u5177\u4f53\u7b97\u6cd5\uff1a\u5148\u7528Korasaju Algorithm\u6c42\u51fa\u6709\u5411\u56fe\u6240\u6709\u7684\u5f3a\u8fde\u901a\u5206\u91cf(<em>\u5f53\u7136Comzyh\u662f\u7528\u7684Tarjan<\/em>),\u7136\u540e\u5c06\u6240\u6709\u7684\u5f3a\u8fde\u901a\u5206\u91cf\u7f29\u6210\u4e00\u4e2a\u70b9(\u7f29\u70b9),\u8fd9\u6837\u539f\u6765\u7684\u6709\u5411\u56fe\u5c31\u7f29\u6210\u4e86\u4e00\u4e2aDAG\u56fe(\u6709\u5411\u65e0\u73af\u56fe);\u75282\u4e2a\u6570\u7ec4\u5206\u522b\u8bb0\u5f55\u65b0\u751f\u6210\u7684DAG\u56fe\u4e2d\u7684\u6bcf\u4e2a\u9876\u70b9(\u5305\u62ec\u539f\u6765\u7684\u9876\u70b9\u548c\u5f3a\u8fde\u901a\u5206\u91cf\u7684\u7f29\u70b9)\u662f\u5426\u6709\u51fa\u8fb9\u548c\u5165\u8fb9,\u6700\u540e\u904d\u5386\u6bcf\u4e2a\u9876\u70b9,\u5982\u679c\u6ca1\u6709\u5165\u8fb9,\u5219ans1++;\u5982\u679c\u6ca1\u6709\u51fa\u8fb9,ans2++.\u6700\u540e\u6240\u6c42\u5373\u4e3aans1\u548cmax(ans1,ans2).<\/p><\/blockquote>\n<p><span style=\"color: #ff0000;\"><strong>\u6ce8\u610f<\/strong><\/span>\u5982\u679c\u6700\u7ec8\u4ec5\u6709\u4e00\u4e2a\u5f3a\u8fde\u901a\u5206\u91cf,\u663e\u7136\u7b2c\u4e8c\u95ee\u5e94\u8f93\u51fa0\u800c\u4e0d\u662f1<br \/>\n\u8fd9\u9053\u9898\u7684\u4ee3\u7801\u6bd4\u4e0a\u4e00\u9898\u7b80\u6d01\u7684\u591a\u4e86<br \/>\nMY CODE:<\/p>\n<pre class=\"lang:default decode:true\">\/*\r\nProblem: 1236\t\tUser: comzyh\r\nMemory: 240K\t\tTime: 0MS\r\nLanguage: C++\t\tResult: Accepted\r\n*\/\r\n#include &lt;cstdio&gt;\r\n#include &lt;cstring&gt;\r\n#include &lt;cstdlib&gt;\r\nint tab[101][101];\r\nint DFN[101],LOW[101],stack[200],instack[200];\r\nint f[101];\r\nint belong[101];\r\nint top,RB,RT;\/\/\u6808\u9876,\u6700\u540e\u4e00\u4e2a\u5f3a\u8fde\u901a\u5206\u91cf\u7f16\u53f7,\u6700\u540e\u4e00\u4e2a\u65f6\u95f4\u6233 \r\nint ein[101],eout[101],ans,ans1,ans2;\r\nint N;\r\n\/\/Function\r\nvoid Tarjan(int x);\r\nint getf(int x);\r\ninline void add(int x,int y);\/\/add x to y\r\nint main()\r\n{\r\n    int i,j,to;\r\n    scanf(\"%d\",&amp;N);\r\n    memset(tab,0,sizeof(tab));\r\n    for (i=1;i&lt;=N;i++)\r\n        while (scanf(\"%d\",&amp;to),to)\r\n              tab[i][++tab[i][0]]=to;\r\n    \/\/\r\n    memset(DFN,0,sizeof(DFN));\r\n    memset(instack,0,sizeof(instack));\r\n    top=RB=RT=0;\r\n    for (i=1;i&lt;=N;i++)\r\n        f[i]=i;\r\n    for (i=1;i&lt;=N;i++)\r\n        if (!DFN[i])\r\n           Tarjan(i);\r\n    \/\/for (i=1;i&lt;=N;i++)printf(\"%4d belong to %4d\\n\",i,belong[i]);\r\n    memset(ein,0,sizeof(ein));\r\n    memset(eout,0,sizeof(eout));\r\n    for (i=1;i&lt;=N;i++)\r\n        if (tab[i][0])\r\n        {\r\n           eout[belong[i]]=1;\r\n           for (j=1;j&lt;=tab[i][0];j++)\r\n                if (getf(i)!= getf(tab[i][j]))ein[belong[tab[i][j]]]=1;\r\n        }\r\n    ans1=ans2=0;\r\n    for (i=1;i&lt;=RB;i++)\r\n    {\r\n        if (!ein[i] )ans1++;\r\n        if (!eout[i])ans2++;\r\n    }\r\n    ans=(ans1&gt;ans2)?ans1:ans2;\r\n    if (RB==1)ans=0;\/\/\u5982\u679c\u5c31\u4e00\u4e2a\u5206\u91cf,\u663e\u7136\u4e0d\u7528\u518d\u8fde\u63a5 \r\n    printf(\"%d\\n%d\",ans1,ans);\r\n    system(\"pause\");\r\n}\r\nvoid Tarjan(int x)\r\n{\r\n     int i,t;\/\/t= top \/ to \u591a\u7528\u9014, \r\n     stack[++top]=x;\r\n     DFN[x]=LOW[x]=++RT;\r\n     instack[x]=1;\r\n     for (i=1;i&lt;=tab[x][0];i++)\r\n     {\r\n         t=tab[x][i];\r\n         if (!DFN[t])\r\n         {\r\n            Tarjan(t);\r\n            if (LOW[t] &lt; LOW[x])\r\n               LOW[x] = LOW[t];\r\n         }\r\n         else\r\n         if (instack[t] &amp;&amp; DFN[t]&lt;LOW[x])\r\n            LOW[x]=DFN[t];\r\n     }\r\n     if (LOW[x] == DFN[x])\r\n     {\r\n        RB++;\r\n        do\r\n        {\r\n             t=stack[top--];\r\n             add(t,x);\r\n             belong[t]=RB;\r\n             instack[t]=0;\r\n        }while(t != x);\r\n     }\r\n}\r\nint getf(int x)\r\n{\r\n    if (f[x] == x)return x;\r\n    else return f[x]=getf(f[x]);\r\n}\r\ninline void add(int x,int y)\/\/add x to y\r\n{\r\n     int fx=getf(x),fy=getf(y);\r\n     if (fx == fy)return ;\r\n     f[fx]=fy;\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5148\u7ed9\u94fe\u63a5POJ 2186 Popular Cows ,POJ 1236 Network of Schools  [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[11,46],"tags":[45,50],"class_list":["post-534","post","type-post","status-publish","format-standard","hentry","category-programing","category-solution","tag-poj","tag-50"],"jetpack_publicize_connections":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6XQWE-8C","jetpack-related-posts":[],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/comzyh.com\/blog\/wp-json\/wp\/v2\/posts\/534","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/comzyh.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/comzyh.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/comzyh.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/comzyh.com\/blog\/wp-json\/wp\/v2\/comments?post=534"}],"version-history":[{"count":2,"href":"https:\/\/comzyh.com\/blog\/wp-json\/wp\/v2\/posts\/534\/revisions"}],"predecessor-version":[{"id":860,"href":"https:\/\/comzyh.com\/blog\/wp-json\/wp\/v2\/posts\/534\/revisions\/860"}],"wp:attachment":[{"href":"https:\/\/comzyh.com\/blog\/wp-json\/wp\/v2\/media?parent=534"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/comzyh.com\/blog\/wp-json\/wp\/v2\/categories?post=534"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/comzyh.com\/blog\/wp-json\/wp\/v2\/tags?post=534"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}