zoukankan      html  css  js  c++  java
  • D. The Door Problem

    Moriarty has trapped n people in n distinct rooms in a hotel. Some rooms are locked, others are unlocked. But, there is a condition that the people in the hotel can only escape when all the doors are unlocked at the same time. There are m switches. Each switch control doors of some rooms, but each door is controlled by exactly two switches.

    You are given the initial configuration of the doors. Toggling any switch, that is, turning it ON when it is OFF, or turning it OFF when it is ON, toggles the condition of the doors that this switch controls. Say, we toggled switch 1, which was 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 need to tell Sherlock, if there exists a way to unlock all doors at the same time.

    Input

    First line of input contains two integers n and m (2 ≤ n ≤ 105, 2 ≤ m ≤ 105) — the number of rooms and the number of switches.

    Next line contains n space-separated integers r1, r2, ..., rn (0 ≤ ri ≤ 1) which tell the status of room doors. The i-th room is locked if ri = 0, otherwise it is unlocked.

    The i-th of next m lines contains an integer xi (0 ≤ xi ≤ n) followed by xi distinct integers separated by space, denoting the number of rooms controlled by the i-th switch followed by the room numbers that this switch controls. It is guaranteed that the room numbers are in the range from 1 to n. It is guaranteed that each door is controlled by exactly two switches.

    Output

    Output "YES" without quotes, if it is possible to open all doors at 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

    太久没有做过图论的东西,真的是我的锅,这种垃圾的2-SAT都能写错,我也是真的很服气啊。
    注意两个问题第一就是模板的问题,第二还是模板的问题,反正。

     有个地方是这样写的,还有就是{ void add(int x,int xval,int y,int yval) } 这个加的边就是xUy的边,所以说千万别加边加错了。

    然后这个问题就是和普通的2-sat的问题一样了,好像这个问题还可以用并查集来做(雾)、

  • 相关阅读:
    敏捷不是XP(口水文)
    利用异或的特性解决,找出重复数的问题,应该是目前最优算法。
    开源和免费那些事儿(二)
    开源和免费那些事儿
    LINQ本质 外篇 JOIN补遗
    在北京求.NET开发职位,人已经到达北京
    软件是邪恶的
    最近遇到的两个面试题兼卖身广告
    谈谈信仰和银弹。
    继续高阶函数好玩有用的扩展(网吧行文)
  • 原文地址:https://www.cnblogs.com/Heilce/p/6476406.html
Copyright © 2011-2022 走看看