{"id":69,"date":"2011-03-25T07:47:54","date_gmt":"2011-03-24T23:47:54","guid":{"rendered":"http:\/\/www.shuizilong.com\/house\/?p=69"},"modified":"2012-03-03T07:48:12","modified_gmt":"2012-03-02T23:48:12","slug":"poj-3207-ikkis-story-iv-pandas-trick","status":"publish","type":"post","link":"https:\/\/www.shuizilong.com\/house\/archives\/poj-3207-ikkis-story-iv-pandas-trick\/","title":{"rendered":"POJ 3207. Ikki&#8217;s Story IV &#8211; Panda&#8217;s Trick"},"content":{"rendered":"<h3>Brief description :<\/h3>\n<p>\u5706\u73af\u4e0a\u5747\u5300\u7684\u9644\u7740\u7740 n \u4e2a\u70b9\uff0c\u8fde\u63a5\u7740\u8fd9\u4e9b\u70b9\u7684 m \u6761\u66f2\u7ebf\uff0c\u5b83\u4eec\u6216\u8005\u5728\u5706\u5185\u6216\u8005\u5728\u5706\u5916\u3002<br \/>\n\u786e\u8ba4\u662f\u5426\u5b58\u5728\u4e00\u79cd\u5212\u5206\u65b9\u6848\u4f7f\u5f97\u6240\u6709\u7684\u66f2\u7ebf\u4e0d\u76f8\u4ea4\u3002<br \/>\n<!--more--><\/p>\n<h3>Analyse :<\/h3>\n<p>\u7591\u4f3c\u662f\u4e00\u79cd\u5f31\u5316\u7684 2-SAT \uff08\u65e0\u5411\u3002\u3002\uff09\u3002\u3002\u3002<br \/>\n\u3002\u3002\u3002\u662f\u53ef\u4ee5\u7528\u5e76\u67e5\u96c6\u76f4\u63a5\u5199\u7684\u5427\u3002\u3002<\/p>\n<pre lang=\"cpp\" file=\"2-SAT.cpp\">\r\n#include <iostream>\r\n#include <vector>\r\nusing namespace std;\r\n\r\nconst int INF = 0x7fffffff;\r\nconst int N = 1000, M = 500;\r\nvector<int> adj[N];\r\nint A[M], B[M];\r\n\r\nint d[N], l[N], f[N];\r\nint st[N], top, cnt;\r\nint n, m;\r\n\r\nvoid dfs(int u){\r\n\td[u] = l[u] = ++cnt;\r\n\tst[top++] = u;\r\n\tfor (int i=0;i<adj[u].size();i++){\r\n\t\tint v = adj[u][i];\r\n\t\tif (d[v] < d[u]){\r\n\t\t\tif (d[v] == 0) dfs(v);\r\n\t\t\tl[u] = min(l[u], l[v]);\r\n\t\t}\r\n\t}\r\n\t\r\n\tif (l[u]==d[u]){\r\n\t\tdo{\r\n\t\t\tf[st[--top]] = u;\r\n\t\t\td[st[top]] = INF;\r\n\t\t}while (st[top]!=u);\r\n\t}\r\n}\r\n\r\nvoid init(){\t\r\n\tmemset(adj, 0, sizeof(adj));\r\n\t\r\n\tfor (int i = 0; i < m; i ++){\r\n\t\tscanf(\"%d%d\", &#038;A[i], &#038;B[i]);\r\n\t\tif (B[i] < A[i]) swap(A[i], B[i]); \/\/#\r\n\t}\r\n\t\r\n\tfor (int i = 0; i < m; i ++)\r\n\t\tfor (int j = i + 1; j < m; j ++)\r\n\t\t\tif (A[j] < A[i]) swap(A[i], A[j]), swap(B[i], B[j]);\r\n\t\r\n\tfor (int i = 0; i < m; i ++)\r\n\t\tfor (int j = i + 1; j < m; j ++)\r\n\t\t\tif (B[i] < B[j] &#038;&#038; B[i] > A[j]){\r\n\t\t\t\tadj[2*i+1].push_back(2*j), adj[2*j].push_back(2*i+1);\r\n\t\t\t\tadj[2*j+1].push_back(2*i), adj[2*i].push_back(2*j+1);\r\n\t\t\t}\r\n\tn = m;\r\n}\r\n\r\nvoid solve(){\r\n\tmemset(d, 0, sizeof(d));\r\n\tcnt = top = 0;\r\n\tfor (int i=0;i<2*n;i++)\r\n\t\tif (!d[i]) dfs(i);\r\n\t\r\n}\r\n\r\nbool check(){\r\n\tfor (int i=0;i<n;i++)\r\n\t\tif (f[2*i] == f[2*i+1]) return false;\r\n\treturn true;\r\n}\r\n\r\nint main(){\r\n\t\/\/freopen(\"in.txt\", \"r\", stdin);\r\n\twhile (scanf(\"%d%d\", &#038;n, &#038;m) != EOF){\r\n\t\tinit(); solve();\r\n\t\tcout << (check() ? \"panda is telling the truth...\" : \"the evil panda is lying again\") << endl;\r\n\t}\r\n}\r\n<\/pre>\n<pre lang=\"cpp\" file=\"UFS.cpp\">\r\n#include <iostream>\r\n#include <vector>\r\nusing namespace std;\r\n\r\nconst int N = 1000, M = 500;\r\n\r\nint P[N], R[N]; \/\/ Ufs...\r\nint A[M], B[M];\r\nint n, m;\r\n\r\n\r\n\r\nvoid Make(int x){\r\n\tP[x] = x, R[x] = 0;\r\n}\r\nint Find(int x){\r\n\tif (P[x] != x) P[x] = Find(P[x]);\r\n\treturn P[x];\r\n}\r\n\r\nvoid Union(int x, int y){\r\n\tx = Find(x), y = Find(y);\r\n\tif (R[x] < R[y]){\r\n\t\tP[y] = x;\r\n\t}\r\n\telse {\r\n\t\tP[x] = y;\r\n\t\tif (R[x] == R[y]) R[y] ++ ;\r\n\t}\r\n}\r\n\r\n\r\n\r\n\r\nvoid init(){\t\r\n\tfor (int i = 0; i < m; i ++){\r\n\t\tscanf(\"%d%d\", &#038;A[i], &#038;B[i]);\r\n\t\t\/\/if (B[i] < A[i]) swap(A[i], B[i]); \/\/# \u5176\u5b9e\u662f\u5b8c\u5168\u4e0d\u5fc5\u8981\u7684\u5427\u3002\u3002\u3002\u3002\u3002\r\n\t}\r\n\t\r\n\tfor (int i = 0; i < m; i ++)\r\n\t\tfor (int j = i + 1; j < m; j ++)\r\n\t\t\tif (A[j] < A[i]) swap(A[i], A[j]), swap(B[i], B[j]);\r\n\tn = m;\r\n}\r\n\r\nvoid solve(){\r\n\tfor (int i = 0 ;i < 2*n; i ++)\r\n\t\tMake(i);\r\n\t\r\n\tfor (int i = 0; i < n; i ++)\r\n\t\tfor (int j = i + 1; j < n; j ++)\r\n\t\t\tif (B[i] < B[j] &#038;&#038; B[i] > A[j])\r\n\t\t\t\tUnion(2*i, 2*j+1), Union(2*j, 2*i+1);\r\n}\r\n\r\nbool check(){\r\n\tfor (int i=0;i<n;i++)\r\n\t\tif (Find(2*i) == Find(2*i+1)) return false;\r\n\treturn true;\r\n}\r\n\r\nint main(){\r\n\t\/\/freopen(\"in.txt\", \"r\", stdin);\r\n\twhile (scanf(\"%d%d\", &#038;n, &#038;m) != EOF){\r\n\t\tinit(); solve();\r\n\t\tcout << (check() ? \"panda is telling the truth...\" : \"the evil panda is lying again\") << endl;\r\n\t}\r\n}\r\n<\/pre>\n<p><bk><bk><bk><bk><br \/>\n<bk><bk><bk><bk><\/p>\n<h3>External link :<\/h3>\n<p><a href=\"http:\/\/poj.org\/problem?id=3207\">http:\/\/poj.org\/problem?id=3207<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Brief description : \u5706\u73af\u4e0a\u5747\u5300\u7684\u9644\u7740\u7740 n \u4e2a\u70b9\uff0c\u8fde\u63a5\u7740\u8fd9\u4e9b\u70b9\u7684 m \u6761\u66f2\u7ebf\uff0c\u5b83\u4eec\u6216\u8005\u5728\u5706\u5185\u6216\u8005\u5728\u5706\u5916\u3002 \u786e\u8ba4\u662f\u5426\u5b58\u5728\u4e00\u79cd\u5212\u5206\u65b9\u6848\u4f7f\u5f97\u6240\u6709\u7684\u66f2\u7ebf\u4e0d\u76f8\u4ea4\u3002<\/p>\n","protected":false},"author":1,"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":[19],"tags":[],"class_list":["post-69","post","type-post","status-publish","format-standard","hentry","category-poj"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2tdP7-17","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/69","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/comments?post=69"}],"version-history":[{"count":0,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/69\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/media?parent=69"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/categories?post=69"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/tags?post=69"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}