Poj 2723 get Luffy out (2-sat + binary)

Source: Internet
Author: User

[Question link]

Http://poj.org/problem? Id = 2723

[Topic]

There are 2 * n different locks, each of which has a key, so there are 2 * n keys. 2 * n pairs of keys are divided into N groups.

There is an M-level building with a door on each floor. There are two locks on each door, which may be the same or different. Before going to a certain floor, you must open at least one lock on the door.

You need to select a key from each set of keys and use these keys to go to the building. How many floors can you reach?

[Idea]

For each set of keys, only one key is required. Therefore, the two-Sat model is used.

The question is, how can we find contradictions and add edges?

For two locks on one door, if the keys of the two locks are in the same group, either of them can be used.

If the two keys belong to different groups, if the keys are A1 and B1, the two components are (a1, a2) (B1, B2 ), a2 and B2 must not be selected at the same time, because once selected, there will be no A1 or B1 to open this door.

Therefore, <A2, B2> is a contradiction. Add edge A1-> B2, B2-> A1

Then the question is how many doors can be opened at most, then the maximum number of doors can be opened in two minutes.

[Code]

# Include <iostream> # include <queue> # include <cstdio> # include <cstring> using namespace STD; const int maxn = 2100; const int VN = maxn * 2; const int en = Vn; int n, m; struct edge {int V, next ;}; struct graph {int size, head [VN]; edge e [En]; void Init () {size = 0; memset (Head,-1, sizeof (head) ;}; void addedge (int u, int v) {e [size]. V = V; E [size]. next = head [u]; head [u] = size ++;} g; Class two_sat {public: bool C Heck (const graph & G, const int N) {SCC (G, 2 * n); For (INT I = 0; I <2 * n; I + = 2) if (belong [I] = belong [I ^ 1]) return false; return true;} PRIVATE: void Tarjan (const graph & G, const int U) {int V; dfn [u] = low [u] = ++ idx; STA [top ++] = u; instack [u] = true; For (int e = G. head [u]; e! =-1; E = G. E [e]. next) {v = G. E [e]. v; If (dfn [v] =-1) {Tarjan (G, V); low [u] = min (low [u], low [v]);} else if (instack [v]) {LOW [u] = min (low [u], dfn [v]);} if (low [u] = dfn [u]) {++ bcnt; do {v = sta [-- top]; instack [v] = false; belong [v] = bcnt;} while (u! = V) ;}} void SCC (const graph & G, const int N) {idx = bcnt = Top = 0; memset (dfn,-1, sizeof (dfn )); memset (instack, 0, sizeof (instack); For (INT I = 0; I <n; ++ I) if (dfn [I] =-1) tarjan (G, I);} PRIVATE: int idx, top, bcnt; int dfn [VN]; int low [VN]; int belong [VN]; int sta [VN]; bool instack [VN];} sat; int key [VN]; int door [maxn] [2]; int idx [VN]; int main () {While (~ Scanf ("% d", & N, & M) & N + M) {for (INT I = 0; I <2 * n; ++ I) {scanf ("% d", & Key [I]); idx [Key [I] = I;} For (INT I = 0; I <m; + + I) scanf ("% d", & door [I] [0], & door [I] [1]); int L = 0, R = m + 1, mid; int ans = 0; while (L <r) {mid = (L + r)> 1; // create a graph G. init (); For (INT I = 0; I <mid; ++ I) {int a1 = door [I] [0], b1 = door [I] [1]; int a2 = Key [idx [a1] ^ 1], b2 = Key [idx [B1] ^ 1]; if (A2 = b1) continue; If (a1 = b1) g. addedge (idx [B1], idx [B1] ^ 1); else {G. addedge (idx [a1], idx [B1] ^ 1); G. addedge (idx [B1], idx [a1] ^ 1) ;}} if (SAT. check (G, n) {ans = mid; L = Mid + 1;} else r = mid;} printf ("% d \ n", ANS );} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.