题目链接:http://codeforces.com/contest/776/problem/D

D. The Door Problem
time limit per 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 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
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 get [0, 0, 0] that means all doors are locked.

Then, after toggling switch 1, we get [1, 1, 1] that means all doors are unlocked.

It can be seen that for the first and for the third example inputs it is not possible to make all doors unlocked.

题意: 给你n个锁,m个开关,每个开关控制若干个锁;开关每次可以取反;

    让你动开关使得最后的全为1;

思路:2-SAT;

   but each door is controlled by exactly two switches.

   题目中这段加粗的话,每个锁只被两个开关控制;

   开关当点,所以当锁为1时,控制的两个开关必须同时使用,或者同时不使用,连边;

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x) cout<<"bug"<<x<<endl;
const int N=1e6+,M=1e7+,inf=1e9+;
const ll INF=1e18+,mod=1e9+; /// 数组大小
const int MAXNODE = * ; //两倍 struct TwoSat {
int n;
vector<int> g[MAXNODE];
int pre[MAXNODE], dfn[MAXNODE], dfs_clock, sccn, sccno[MAXNODE];
stack<int> S;
int mark[MAXNODE]; void init(int tot) {
n = tot * ;
for (int i = ; i < n; i+= ) {
g[i].clear();
g[i^].clear();
}
} void add_Edge(int u, int v) {
g[u].push_back(v);
g[v].push_back(u);
} void dfs_scc(int u) {
pre[u] = dfn[u] = ++dfs_clock;
S.push(u);
for (int i = ; i < g[u].size(); i++) {
int v = g[u][i];
if (!pre[v]) {
dfs_scc(v);
dfn[u] = min(dfn[u], dfn[v]);
} else if (!sccno[v]) dfn[u] = min(dfn[u], pre[v]);
}
if (pre[u] == dfn[u]) {
sccn++;
while () {
int x = S.top(); S.pop();
sccno[x] = sccn;
if (x == u) break;
}
}
} void find_scc() {
dfs_clock = sccn = ;
memset(sccno, , sizeof(sccno));
memset(pre, , sizeof(pre));
for (int i = ; i < n; i++)
if (!pre[i]) dfs_scc(i);
} bool solve() {
find_scc();
for (int i = ; i < n; i += ) {
if (sccno[i] == sccno[i + ]) return false;
mark[i / ] = (sccno[i] > sccno[i + ]);
}
return true;
}
} gao;
vector<int>v[N];
int r[N];
int main()
{
int n,m;
scanf("%d%d",&n,&m);
gao.init(m);
for(int i=;i<=n;i++)
scanf("%d",&r[i]);
for(int i=;i<=m;i++)
{
int x,z;
scanf("%d",&x);
for(int j=;j<x;j++)
{
scanf("%d",&z);
v[z].push_back(i);
}
}
for(int i=;i<=n;i++)
{
if(r[i])
{
gao.add_Edge(v[i][]*+,v[i][]*+);
gao.add_Edge(v[i][]*,v[i][]*);
}
else
{
gao.add_Edge(v[i][]*+,v[i][]*);
gao.add_Edge(v[i][]*,v[i][]*+);
}
}
if(gao.solve())puts("YES");
else puts("NO");
return ;
}

ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) D. The Door Problem 2-SAT的更多相关文章

  1. ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) A map B贪心 C思路前缀

    A. A Serial Killer time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  2. ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) A

    Our beloved detective, Sherlock is currently trying to catch a serial killer who kills a person each ...

  3. ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined)

    前四题比较水,E我看出是欧拉函数傻逼题,但我傻逼不会,百度了下开始学,最后在加时的时候A掉了 AC:ABCDE Rank:182 Rating:2193+34->2227 终于橙了,不知道能待几 ...

  4. 【2-SAT】【并查集】ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) D. The Door Problem

    再来回顾一下2-SAT,把每个点拆点为是和非两个点,如果a能一定推出非b,则a->非b,其他情况同理. 然后跑强连通分量分解,保证a和非a不在同一个分量里面. 这题由于你建完图发现都是双向边,所 ...

  5. 【枚举】【前缀和】【map】ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) C. Molly's Chemicals

    处理出前缀和,枚举k的幂,然后从前往后枚举,把前面的前缀和都塞进map,可以方便的查询对于某个右端点,有多少个左端点满足该段区间的和为待查询的值. #include<cstdio> #in ...

  6. ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) D

    Moriarty has trapped n people in n distinct rooms in a hotel. Some rooms are locked, others are unlo ...

  7. ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) C

    Molly Hooper has n different kinds of chemicals arranged in a line. Each of the chemicals has an aff ...

  8. ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) B

    Sherlock has a new girlfriend (so unlike him!). Valentine's day is coming and he wants to gift her s ...

  9. ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) C. Molly's Chemicals

    感觉自己做有关区间的题目方面的思维异常的差...有时简单题都搞半天还完全没思路,,然后别人提示下立马就明白了...=_= 题意:给一个含有n个元素的数组和k,问存在多少个区间的和值为k的次方数. 题解 ...

随机推荐

  1. [py][mx]django城市-教学机构-教师模型设计

    分析下城市-教学机构-教师模型设计 CourseOrg 课程信息 Teacher 教师信息 CityDict 城市信息 代码 from datetime import datetime from dj ...

  2. spring的bean容器加载

    1.在单独使用的时候可以通过ClassPathXmlApplicationContext(“配置文件.xml”);来启动容器. 2.在MVC下是通过启动servlet容器,初始化DispatcherS ...

  3. RAC禁用DRM特性

    查看"_gc"开头的隐藏参数值: set linesize 333 col name for a35 col description for a66 col value for a ...

  4. Docker深入浅出3-镜像管理

    当运行容器的时候,使用的镜像如果在本地中不存在,docker就会自动从docker镜像仓库中下载,默认是从dockerhub公共镜像源下载. 1:镜像列表 我们可以使用docker images [r ...

  5. camera原理

    1)Color Filter Array---CFA 图像传感器都采用一定的模式来采集图像数据,常用的有 BGR 模式和 CFA 模式.BGR 模式是一种可直接进行显示和压缩等处理的图像数据模式,它 ...

  6. 改变 select下拉框 样式

    select{ outline: none; text-indent: 10px; height: 45px; line-height: 45px; width: 100%; border:1px s ...

  7. LINUX环境变量(二)

    一.Shell变量分为本地变量和环境变量. 1.本地变量:在用户现有运行的脚本中使用 a) 定义本地变量 格式: variable-name=value b) 显示本地变量 格式: set  c) 清 ...

  8. Python中的is和==的区别,==判断值是否相等,is判断地址是否一致

    Python中的is和==的区别 Python中的对象包含三要素:id.type.value. 其中id用来唯一标示一个对象,type标识对象的类型,value是对象的值. is判断的是a对象是否就是 ...

  9. java多线程----ReentrantReadWriteLock

    package com.test; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks ...

  10. python中的property

    提示:这篇博文参考了两个博客,第一篇博文地址为:https://www.cnblogs.com/Lambda721/p/6132206.html,另一篇博文地址如下:关于python的property ...