HDU 3062 Party 2-SAT beginners

Source: Internet
Author: User

Problem Description
N couples are invited to a party. Due to venue issues, only one of them can attend. Among the 2n individuals, some people have great conflicts (of course, there is no conflict between husband and wife), and the two who have conflicts will not appear at the same time. Is there a possibility that n people will attend at the same time?
 

Input
N: n couples are invited (n <= 1000)
M: represents a conflict between m pairs (m <(n-1) * (n-1 ))

In the next m row, there are four numbers in each row, namely A1, A2, C1, and C2.
A1 and A2 indicate the number of the husband and wife respectively.
C1, C2 indicates the wife or husband, 0 indicates the wife, and 1 indicates the husband.
Husband and wife numbers from 0 to n-1

 

Output
If one condition exists, the output is YES.
Otherwise, NO

 

Sample Input
2
1
0 1 1 1

Sample Output
YES

Source
2009 Multi-University Training Contest 16-Host by NIT
 

Recommend
Lcy

 

Due to question. N couples need N people. That is, every couple has a person.


That can be said. Provide a contradiction
If it is I, j, 0, 0, you can connect to the directed edge i0-> j1, j0-> i1. If i0 is displayed, j1. and so on.
If it is I, j, you can connect the directed edge i0-> j0, j1-> i1
If it is I, j, you can connect the directed edge i1-> j1, j0-> i0
If it is I, j, you can connect the directed edge i1-> j0, j1-> i0


In this way, a directed graph with all constraints is built. Because it is only a judgment problem, you only need to run a strongly connected algorithm on the directed graph created in this way, and then judge all i0, whether j0 exists in a strongly connected component.

 


 

/** @ Author ipqhjjybj * @ date 20130702 **/# include <cstdio> # include <cmath> # include <cstdlib> # include <ctime> # include <iostream> # include <cmath> # include <algorithm> # include <numeric> # include <utility> # include <cstring> # include <vector> # include <stack> # include <queue> # include <map> # include <string> using namespace std; # define inf 0x3f3f3f # define MAXN 2005 # define clr (x, k) memset (X), (k), sizeof (x) # define clrn (x, k) memset (x), (k), (n + 1) * sizeof (int) # define cpy (x, k) memcpy (x), (k), sizeof (x) # define Base 10000 typedef vector <int> vi; typedef stack <int> si; typedef vector <string> vs; # define sz (a) int (). size () # define pb push_back # define all (c ). begin (), (c ). end () # define rep (I, n) for (int I = 0; I <n; ++ I) # define foreach (it, c) for (vi :: iterator it = (c ). begin (); it! = (C). end (); ++ it) # define max (a, B) (a)> (B )? (A) :( B) # define min (a, B) (a) <(B )? (A) :( B) vector <int> vec [MAXN]; int n, m; int id [MAXN], pre [MAXN], low [MAXN], s [MAXN], stop, cnt, scnt; void init () {int u, v; for (int I = 0; I <n + n; I ++) vec [I]. clear (); for (int I = 0, a, B, c, d; I <m; I ++) {scanf ("% d", & a, & B, & c, & d); u = (a <1) + c; v = (B <1) + d; vec [u]. push_back (v ^ 1); vec [v]. push_back (u ^ 1);} stop = cnt = scnt = 0; clr (pre,-1); clr (id, 0);} void Tarjan (int v, int n) {int t, minc = low [v] = pre [V] = cnt ++; vector <int>: iterator pv; s [stop ++] = v; for (pv = vec [v]. begin (); pv! = Vec [v]. end (); ++ pv) {if (-1 = pre [* pv]) Tarjan (* pv, n); if (low [* pv] <minc) minc = low [* pv];} if (minc <low [v]) {low [v] = minc; return ;} do {id [t = s [-- stop] = scnt; low [t] = n;} while (t! = V); ++ scnt;} int main () {// freopen ("3062.in"," r ", stdin); int I; while (scanf (" % d ", & n )! = EOF & n) {scanf ("% d", & m); init (); for (I = 0; I <n + n; I ++) if (-1 = pre [I]) Tarjan (I, n + n); // determine bool flag = true; for (I = 0; I <n; I ++) if (id [I <1] = id [(I <1) ^ 1]) {flag = false; break;} if (flag) printf ("YES \ n"); else printf ("NO \ n");} return 0 ;} /** @ author ipqhjjybj * @ date 20130702 **/# include <cstdio> # include <cmath> # include <cstdlib> # include <ctime> # include <iostream> # include <cmath> # include <algo Rithm> # include <numeric> # include <utility> # include <cstring> # include <vector> # include <stack> # include <queue> # include <map> # include <string> using namespace std; # define inf 0x3f3f3f # define MAXN 2005 # define clr (x, k) memset (x), (k), sizeof (x) # define clrn (x, k) memset (x), (k), (n + 1) * sizeof (int) # define cpy (x, k) memcpy (x), (k ), sizeof (x) # define Base 10000 typedef vector <int> vi; typedef stack <int> si; typ Edef vector <string> vs; # define sz (a) int (). size () # define pb push_back # define all (c ). begin (), (c ). end () # define rep (I, n) for (int I = 0; I <n; ++ I) # define foreach (it, c) for (vi :: iterator it = (c ). begin (); it! = (C). end (); ++ it) # define max (a, B) (a)> (B )? (A) :( B) # define min (a, B) (a) <(B )? (A) :( B) vector <int> vec [MAXN]; int n, m; int id [MAXN], pre [MAXN], low [MAXN], s [MAXN], stop, cnt, scnt; void init () {int u, v; for (int I = 0; I <n + n; I ++) vec [I]. clear (); for (int I = 0, a, B, c, d; I <m; I ++) {scanf ("% d", & a, & B, & c, & d); u = (a <1) + c; v = (B <1) + d; vec [u]. push_back (v ^ 1); vec [v]. push_back (u ^ 1);} stop = cnt = scnt = 0; clr (pre,-1); clr (id, 0);} void Tarjan (int v, int n) {int t, minc = low [v] = pre [v] = cn T ++; vector <int>: iterator pv; s [stop ++] = v; for (pv = vec [v]. begin (); pv! = Vec [v]. end (); ++ pv) {if (-1 = pre [* pv]) Tarjan (* pv, n); if (low [* pv] <minc) minc = low [* pv];} if (minc <low [v]) {low [v] = minc; return ;} do {id [t = s [-- stop] = scnt; low [t] = n;} while (t! = V); ++ scnt;} int main () {// freopen ("3062.in"," r ", stdin); int I; while (scanf (" % d ", & n )! = EOF & n) {scanf ("% d", & m); init (); for (I = 0; I <n + n; I ++) if (-1 = pre [I]) Tarjan (I, n + n); // determine bool flag = true; for (I = 0; I <n; I ++) if (id [I <1] = id [(I <1) ^ 1]) {flag = false; break;} if (flag) printf ("YES \ n"); else printf ("NO \ n");} 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.