HDU 3715 Go Deeper (2-sat)

Source: Internet
Author: User

HDU 3715 Go Deeper

Topic links

Test instructions: Constructs an X array according to the test instructions function. Q Maximum Recursive layer number

Idea: Convert to 2-sat problem, because X can only be 0. 1,c can only be 0, 1. 2 So the problem is good to do, for 0, 1, 2 corresponding to 3 kinds of expression, and then the depth of two, engage 2-sat can

Code:

#include <cstdio> #include <cstring> #include <cstdlib> #include <vector> #include < algorithm>using namespace Std;const int maxnode = 205;struct twoset {int n;vector<int> g[maxnode * 2];bool Mark[M Axnode * 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 = 10005;int T, N, M;int A[n], B[n], c[n];bool judge (int dep) {gao.init (n); for (int i = 0; i < dep; i++) {if (c[i] = = 0) Gao.add_edge (a[i], 1, b[i], 1); else if (c[i] = = 1) {Gao.add_edge (A[i], 0, A[i], 1); Gao.add_edge (A[i], 0, b [i], 1); Gao.add_edge (B[i], 0, A[i], 1); Gao.add_edge (B[i], 0, B[i], 1);} Elsegao.add_edge (A[i], 0, B[i], 0);} return Gao.solve ();} int main () {scanf ("%d", &t), while (t--) {scanf ("%d%d", &n, &m), for (int i = 0; i < m; i++) scanf ("%d%d%d", & Amp;a[i], &b[i], &c[i]); int l = 0, r = m + 1;while (L < r) {int mid = (L + R)/2;if (judge (mid)) L = mid + 1;e LSE r = Mid;} printf ("%d\n", L-1);} return 0;}


Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

HDU 3715 Go Deeper (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.