HDU 1816, POJ 2723 Get Luffy out (2-sat)

Source: Internet
Author: User

HDU 1816, POJ 2723 Get Luffy out

pid=1816 "target=" _blank "style=" "> Topic Links

Test instructions: N string key. Each string of 2, can only choose one. Then there are n gates, each door has two locks, open one will be able to pass, asked to choose some keys, up to the number of doors

Idea: The number of two passes. Then for the key to build the edge of at least one does not choose, the door to build edge at least one choice, and then 2-sat to do it.
It starts with 1 nodes per string of keys, but it's possible that the data behind it may have a key that's different from now on (it's unreasonable), so this is a good way to kneel.

Code:

#include <cstdio> #include <cstring> #include <cstdlib> #include <vector> #include < algorithm>using namespace Std;const int maxnode = 2105;struct twoset {int n;vector<int> g[maxnode * 2];bool mark[ Maxnode * 2];int S[maxnode * 2], sn;void init (int tot) {n = tot * 2;for (int i = 0; i < n; i + = 2) {g[i].clear (); G[i^1] . Clear ();} Memset (Mark, False, sizeof);} void Add_edge (int u, int uval, int v, int vval) {u = u * 2 + uval;v = v * 2 + vval;g[u^1].push_back (v); G[v^1].push_back (U) ;} void Delete_edge (int u, int uval, int v, int vval) {u = u * 2 + uval;v = v * 2 + vval;g[u^1].pop_back (); G[v^1].pop_back (); }bool dfs (int u) {if (mark[u^1]) return false;if (Mark[u]) return true;mark[u] = true; s[sn++] = u;for (int i = 0; i < g[u].size (); i++) {int v = g[u][i];if (!dfs (v)) return false;} return true;} BOOL Solve () {for (int i = 0; i < n, i + = 2) {if (!mark[i] &&!mark[i + 1]) {sn = 0;if (!dfs (i)) {for (int j = 0) ; J < SN; J + +) Mark[s[j]] = FALSE;SN = 0;if (!dfs (i + 1)) return false;}}} return true;}} gao;const int n = 2055;int N, m;int x[n], Y[n];int K1[n], k2[n];bool judge (int d) {gao.init (2 * N); for (int i = 0; i < N i++) Gao.add_edge (X[i], 0, Y[i], 0); for (int i = 1; I <= D; i++) Gao.add_edge (K1[i], 1, k2[i], 1); return Gao.solve ();} int main () {while (~scanf ("%d%d", &n, &m) && N) {for (int i = 0; i < n; i++) scanf ("%d%d", &x[i], &A Mp;y[i]); for (int i = 1; I <= m; i++) scanf ("%d%d", &k1[i], &k2[i]); int l = 0, r = m + 1;while (L < r) {int m id = (L + R)/2;if (judge (mid)) L = mid + 1;else r = Mid;} printf ("%d\n", L-1);} return 0;}


HDU 1816, POJ 2723 Get Luffy out (2-sat)

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.