Hdu3715 2-sat + binary

Source: Internet
Author: User

Go deeper

Determine the maximum number of conditions allowed by a 0/1 array (size: N. Conditions are given in arrays A, B, and C.

Tucao: Well, it's been a long time! The key is to abstract the question model (the above sentence ). Perform the second sat in the future: What are points and what are conditions !, Then pay attention to the details. This time, a small error occurred:

When there is no solution, I and I + 1 are in the same SCC, and I did not have + 2 each time! But ++! Silly... Success! I always thought that I had written an error in binary mode...

# Include <iostream> # include <cstdio> # include <stack> # include <vector> using namespace STD; const int INF = 0x3f3f3f3f; const int maxv = 402; // MaxE = 500000; int dfn [maxv]; int low [maxv]; int vis [maxv]; int SCC [maxv]; int INS [maxv]; stack <int> sta; int times = 0; int numb = 0; vector <int> E (maxv); void Tarjan (int u) {dfn [u] = low [u] = times ++; INS [u] = 1; Sta. push (U); For (INT I = 0; I <E [u]. size (); I ++) {int v = E [u] [I]; If (! Vis [v]) {vis [v] = 1; Tarjan (V); If (low [v] <low [u]) low [u] = low [v];} else if (INS [v] & dfn [v] <low [u]) low [u] = dfn [v];} If (low [u] = dfn [u]) {numb ++; int cur; do {cur = Sta. top (); Sta. pop (); INS [cur] = 0; SCC [cur] = numb;} while (cur! = U) ;}} int n, m; int A [10005], B [10005], C [10005]; void Init () {numb = times = 0; for (INT I = 0; I <maxv; I ++) {SCC [I] = INS [I] = dfn [I] = low [I] = vis [I] = 0; E [I]. clear () ;}} bool build_solve (int x) {Init (); For (INT I = 0; I <= x; I ++) {If (C [I] = 2) {e [2 * A [I] + 1]. push_back (2 * B [I]); e [2 * B [I] + 1]. push_back (2 * A [I]);} else if (C [I] = 1) {e [2 * A [I]. push_back (2 * B [I]); e [2 * B [I]. push_back (2 * A [I]); e [2 * A [I] + 1]. push_back (2 * B [I] + 1); E [2 * B [I] + 1]. push_back (2 * A [I] + 1);} else if (C [I] = 0) {e [2 * A [I]. push_back (2 * B [I] + 1); E [2 * B [I]. push_back (2 * A [I] + 1) ;}} for (INT I = 0; I <= 2 * n-1; I ++) {If (! Vis [I]) {vis [I] = 1; Tarjan (I) ;}} for (INT I = 0; I <= 2 * N-2; I + = 2) // start to write it as I ++ !!!! Wa to kneel! If (SCC [I] = SCC [I + 1]) return 0; return 1 ;}void readin () {for (INT I = 0; I <m; I ++) scanf ("% d", & A [I], & B [I], & C [I]);} int main () {int t; scanf ("% d", & T); While (t --) {scanf ("% d", & N, & M ); init (); readin (); int L = 0, r = m, mid; while (L + 1 <r) {mid = (L + r)/2; if (build_solve (MID) L = mid; else r = mid;} printf ("% d \ n", L + 1) ;}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.