{"id":76,"date":"2011-03-20T07:53:20","date_gmt":"2011-03-19T23:53:20","guid":{"rendered":"http:\/\/www.shuizilong.com\/house\/?p=76"},"modified":"2012-03-03T07:53:34","modified_gmt":"2012-03-02T23:53:34","slug":"pku-2441-arrange-the-bulls","status":"publish","type":"post","link":"https:\/\/www.shuizilong.com\/house\/archives\/pku-2441-arrange-the-bulls\/","title":{"rendered":"PKU 2441. Arrange the Bulls"},"content":{"rendered":"<h3>Brief description :<\/h3>\n<p>\u4e8c\u5206\u56fe\u5b8c\u5907\u5339\u914d\u8ba1\u6570\uff0c\u6240\u8c13\u5b8c\u5907\u5339\u914d\u662f\u8981\u6c42\u53ef\u4ee5\u5b8c\u5168\u8986\u76d6\u4e00\u8fb9\u7684\u70b9\u3002<br \/>\n<!--more--><\/p>\n<pre lang=\"cpp\" file=\"A.cpp\">\r\n#include <iostream>\r\nusing namespace std;\r\nconst int N = 20, S = 1 << 20;\r\nbool G[N][N];\r\nint F[2][S], n, m, p, q, up, ans;\r\n\r\nvoid init(){\r\n\tmemset(G, false, sizeof(G));\r\n\tint t, y;\r\n\tfor (int x=0;x<n;x++){\r\n\t\tcin >> t;\r\n\t\tfor (int i=0;i<t;i++){\r\n\t\t\tcin >> y; y--;\r\n\t\t\tG[x][y] = true;\r\n\t\t}\r\n\t}\r\n\tup = 1 << m;\r\n}\r\n\r\n\r\nvoid solve(){\r\n\tmemset(F, 0, sizeof(F));\r\n\tF[p = 0][0] = 1;\r\n\tfor (int i=0;i<n;i++){\r\n\t\tq = p, p = 1 - p;\r\n\t\tmemset(F[p], 0, sizeof(F[p]));\r\n\t\tfor (int s=0;s<up;s++){\r\n\t\t\tif (!F[q][s]) continue;\r\n\t\t\tfor (int j=0;j<m;j++)\r\n\t\t\t\tif (!(s&#038;(1<<j)) &#038;&#038; G[i][j]) {\r\n\t\t\t\t\tF[p][s|1<<j] += F[q][s];\r\n\t\t\t\t\t\/\/cout << i << \" \" << s << endl;\r\n\t\t\t\t}\r\n\t\t}\r\n\t}\r\n\tans = 0;\r\n\tfor (int s=0;s<up;s++)\r\n\t\tans += F[p][s];\r\n\t\r\n}\r\n\r\nint main(){\r\n\tfreopen(\"in.txt\", \"r\", stdin);\r\n\twhile (cin >> n >> m){\r\n\t\tinit(); solve();\r\n\t\tcout << ans << endl;\r\n\t}\r\n}\r\n\r\n\r\n\r\n\r\n\/\/ PKU 2441 Arrange the Bulls\r\n\r\n<\/pre>\n<pre lang=\"cpp\" file=\"B.cpp\">\r\n#include <iostream>\r\nusing namespace std;\r\nconst int N = 20, S = 1 << 20;\r\nbool G[N+1][N]; int n, m;\r\nint s, d, ans;\r\n\r\n\r\nstruct hashTable {\r\n\tint state[S], pos[S], sz;\r\n\tint key[S];\r\n\t\r\n\tinline void clear() {\r\n\t\tsz = 0; \r\n\t\tmemset(pos, -1, sizeof(pos));\r\n\t}\r\n\tinline void insert(int s) {\r\n\t\t\/\/cout << s << \" \" << d << endl;\r\n\t\tif (pos[s]!= -1) {\r\n\t\t\tkey[pos[s]] += d;\r\n\t\t\treturn;\r\n\t\t}\r\n\t\tstate[sz] = s, key[sz] = d;\r\n\t\tpos[s] = sz++;\r\n\t}\r\n\tinline int search(int s) {\r\n\t\tif (pos[s]!=-1) return key[pos[s]];\r\n\t\treturn 0;\r\n\t}\r\n} H[2] , *src , *des;\r\n\r\n\r\n\r\n\r\nvoid init(){\r\n\tmemset(G, false, sizeof(G));\r\n\tint t, y;\r\n\tfor (int x=1;x<=n;x++){\r\n\t\tcin >> t;\r\n\t\tfor (int i=0;i<t;i++){\r\n\t\t\tcin >> y; y--;\r\n\t\t\tG[x][y] = true;\r\n\t\t}\r\n\t}\r\n}\r\n\r\n\r\nvoid solve(){\r\n\tsrc = H, des = src + 1, d = 1;\r\n\tdes->clear(), des->insert(0);\r\n\r\n\t\r\n\tfor (int i=1;i<=n-1;i++){\r\n\t\tswap(src, des), des->clear();\r\n\t\tfor (int ii=0;ii<src->sz;ii++){\r\n\t\t\ts = src->state[ii], d = src->key[ii];\r\n\t\t\tfor (int j=0;j<m;j++)\r\n\t\t\t\tif (!(s&#038;(1<<j)) &#038;&#038; G[i][j])\r\n\t\t\t\t\tdes->insert(s|1<<j);\r\n\t\t}\r\n\t}\r\n\t\r\n\tfor (int ii=0;ii<des->sz;ii++){\r\n\t\ts = des->state[ii], d = des->key[ii];\r\n\t\tfor (int j=0;j<m;j++)\r\n\t\t\tif (!(s&#038;(1<<j)) &#038;&#038; G[n][j])\r\n\t\t\t\tans += d;\r\n\t}\r\n}\r\n\r\nint main(){\r\n\t\/\/freopen(\"in.txt\", \"r\", stdin);\r\n\twhile (cin >> n >> m){\r\n\t\tinit(); solve();\r\n\t\tcout << ans << endl;\r\n\t}\r\n}\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=2441\">http:\/\/poj.org\/problem?id=2441<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Brief description : \u4e8c\u5206\u56fe\u5b8c\u5907\u5339\u914d\u8ba1\u6570\uff0c\u6240\u8c13\u5b8c\u5907\u5339\u914d\u662f\u8981\u6c42\u53ef\u4ee5\u5b8c\u5168\u8986\u76d6\u4e00\u8fb9\u7684\u70b9\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":[1],"tags":[],"class_list":["post-76","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2tdP7-1e","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/76","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=76"}],"version-history":[{"count":0,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/76\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/media?parent=76"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/categories?post=76"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/tags?post=76"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}