Codeforces round D the Door Problem 2-sat__codeforces

Source: Internet
Author: User
Tags switches
D. The Door Problem time limit/test 2 seconds memory limit per test 256 megabytes input standard input output standard Output

Moriarty has trapped n people in n distinct rooms in a hotel. Some rooms are locked, others are. But, there is a condition this people in the "can only" escape when all the doors are the unlocked. There Arem switches. Each switch control doors of some rooms, but each door is controlled byexactly two.

You are given the initial configuration of the doors. toggling any switch, which is, turning it at when it was off, or turning it off, and it is, toggles the condition of the Doors that this switch controls. Say, we toggled switch1, which were connected to room 1, 2 and 3 which were respectively locked, unlocked and unlocked. Then, after toggling the switch, they become unlocked, locked and locked.

You are need to tell Sherlock, if there exists a way to unlock all doors in the same time. Input

The "a" of input contains two integers n andm (2≤n≤105,2≤m≤105)-the number of rooms and the number of Switche S.

Next line contains n space-separated integersr1, R2, ..., RN (0≤ri≤1) which tell the status of room doors. The i-th room is locked Ifri = 0, otherwise it is unlocked.

The i-th of next m lines contains a integer XI (0≤xi≤n) followed byxi distinct integers separated by spaces, denoting The number of rooms controlled by-thei-th switch followed by the room numbers, this switch controls. It is guaranteed this room numbers are in the range from1 to n. It is guaranteed the door is controlled by exactly two switches. Output

Output "YES" without quotes, if it is possible to open all doors in the same time, otherwise output "NO" without quotes. Examples Input

3 3 1 0 1 2 1 3 2 1 2 2 2-
3
Output
NO
Input
3 3 1 0 1 3 1 2 3 1 2 2 1-
3
Output
YES
Input
3 3 1 0 1 3 1 2 3 2 1 2 1-
3
Output
NO
Note

In the second example input, the initial statuses of the doors are [1, 0, 1] (0 means locked, 1-unlocked).

After toggling switch 3, we have [0, 0, 0] That means all doors are locked.

Then, after toggling switch 1, we have [1, 1, 1] That means all doors are unlocked.

It can be seen this for the "the" the "the" and "the" third example inputs it is isn't possible to make all doors unlocked.


/* Topic Description: There are n doors, m switches, each door by the M switch in the two control, if the state of the door is 0 (off), then change the control of the door of a switch, the door state will become 1 (open), if the state of the door is 1 (open), then change the control of the door of a switch, the door state becomes
           0 (off).
                        
    Now known n the initial state of the gate, and the M switch control which doors, ask whether by pressing some switches, so that the state of the whole department into 1. Train of thought: see every door exactly two switch control, think 2-sat solve.
       For each door, the switch known to control the door is x,y, if the initial state of the gate is 1, then the addition of the statement (!x∨y) ∧ (x∨!y), which means that x and Y must be in the same state, if the gate has an initial status of 0, then add the statement (!x∨!y) ∧ (x∨y), indicating that x and Y must be One turn off, run 2-sat get results: For a condition, not necessarily with a ∨ statement to reflect, can be used to ∧ a number of ∨ with the connection statement to express * * * #pragma warning (disable:4786) #pragm A COMMENT (linker, "/stack:102400000,102400000") #include <iostream> #include <cstdio> #include <cstring > #include <algorithm> #include <stack> #include <queue> #include <map> #include <set> include<vector> #include <cmath> #include <string> #include <sstream> #include <bitset> # Define LL long #define (i,f_start,f_end) for (int i=f_start;i<=f_end;++i) #define MEM (a,x) memset (A,x,sizeof (A ) #define Lson l,m,x<<1 #define RSOn m+1,r,x<<1|1 using namespace std;
const int INF = 0X3F3F3F3F;
const INT mod = 1e9 + 7;
Const double PI = ACOs (-1.0);
const double eps=1e-6;
const int MAXN = 1e5 + 5;
vector<int>v[maxn];
int DOOR[MAXN];
    struct Twosat {int n;
    VECTOR&LT;INT&GT;G[MAXN * 2];
    BOOL MARK[MAXN * 2];

    int S[MAXN * 2], C;
        bool Dfs (int x) {if (mark[x^1]) return false;
        if (Mark[x]) return true;
        Mark[x] = true;
        S[c++] = x;
        for (int i = 0; i < g[x].size (); i++) {if (!dfs (g[x][i)), return false;
    return true;
        } void init (int n) {this-> n = n;
        for (int i = 0; i < n * 2; i++) {g[i].clear ();
    Mem (mark, 0);
        } void Add_clause (int x, int xval, int y, int yval) {x = x * 2 + xval;
        y = y * 2 + yval;
        g[x ^ 1].push_back (y);
    G[y ^ 1].push_back (x);
        BOOL Solve () {for (int i = 0; i < n * 2; i+=2) {if (!mark[i] &&!mark[i + 1]) {c = 0;       if (!dfs (i)) {while (C > 0) mark[s[--c]] = false;
                I've tried to mark I as true, but I can't, so when I try to mark I+1 as true, I first erase the effect of I if (!dfs (i + 1)) return false;
    }} return true;
}}twosat;
    int main () {int n, m, x, y;
    scanf ("%d%d", &n, &m);
    for (int i = 0; i< N; i++) {scanf ("%d", &door[i]);
        for (int i = 0; i < m i++) {scanf ("%d", &x);
            for (int j = 0; j<x; j +) {scanf ("%d", &y);
        V[y-1].push_back (i);
    } twosat.init (M);
            for (int i = 0; i < n; i++) {if (Door[i]) {twosat.add_clause (v[i][0], 0, v[i][1], 1);
        Twosat.add_clause (V[i][0], 1, v[i][1], 0); } else{Twosat.add_clause (V[i][0], 0, V[i][1], 0);
        Twosat.add_clause (V[i][0], 1, v[i][1], 1);
    } bool ans = twosat.solve ();
    if (ans) puts ("YES");
    Else puts ("NO");
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.