CodeForces 776D 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 twoswitches.

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 12 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 ≤ 1052 ≤ 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.

Example
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种操作,每种操作对应了门的开关,一种操作可以对应多个门的开关,问最后门是否可以满足同时开大,若不满足就输出NO,否则就输出YES。还是解释一下样例吧。

3 3         3:有3个门   3:接下来有3种操作
1 0 1       代表门的初始状态,1为开,0为关
3 1 2 3     (操作1) 3:表示操作1操作3个门,分别是门1,门2,门3
1 2         (操作2) 1: 表示操作2操作1个门,门2
2 1 3       (操作3) 2: 表示操作3操作2个门,门1,门3
YES          表示可以由 1 0 1 这种初始状态转化成1 1 1 这种状态
可以执行操作2,把门2给打开,就变成了1 1 1 这种状态
也可以先执行操作3,变成0 0 0 这种状态,然后执行操作1,即又转化给1 1 1 这种状态;
 
   
题解:一开始没有思路,队友说可以用并查集写出来,才开始有思路。
如果门的初始状态为开,那么管这个门的两个操作,要么两者不执行,要么两者都执行,只能这样才能保持门是开着的;如果门的初始状态为关,那么管理这个门的两种操作,只能是执行一个,另一个不执行。
因为每个门一定有两种操作,我们可以用一个op[][]数组表示操作的状态;op[i][0],op[i][1]表示管理门i的两种操作,我们设定op[i][0]表示管理门i的这个操作是执行的,op[i][0]+m 表示管理门i的这个操作是不执行的。所以对于一个门i初始状态为0的时候情况为:op[i][0],op[i][1]+m 
或者 :op[i][1],op[i][0]+m 两种情况,对于门i初始状态为1的时候情况为: op[i][0],op[i][1] 
或者:op[i][1]+m , op[i][0]+m 两种情况,我们只需要把这两种状态的各自的两种情况用并查集维护一下,然后判断op[i][a] 和 op[i][a]+m是否在同一集合中即可,具体代码如下:
#include <bits/stdc++.h>
const int maxn = 100005;
int op[maxn][2];//op0表示第一个操作,op1表示第二个操作
int  door[maxn];//门的初始状态
int   f[2*maxn];
void init(int n, int m) {
	for(int i = 0; i <= n; i++) {
		op[i][0] = 0;
		op[i][1] = 0;
	}
	for(int i = 0; i <= 2*m; i++) {
		f[i] = i;
	}
}

int getf(int v) {
	if(v != f[v]) f[v] = getf(f[v]);
	return f[v];
}

void merge(int v, int u) {
	int t1 = getf(v);
	int t2 = getf(u);
	if(t1 != t2) 
	f[t1] = t2;
	return ;
}

int main() {
	int n, m, x, xi;
	while(~scanf("%d%d",&n,&m)) {
		init(n, m);
		for(int i = 1; i <= n; i++) {
			scanf("%d",&door[i]);
		}
		for(int i = 1; i <= m; i++) {//操作 
			scanf("%d",&x);
			while(x--) {
				scanf("%d",&xi);
				if(op[xi][0] == 0) op[xi][0] = i;
				else op[xi][1] = i;              //表示操作门xi的两个操作 
			}
		}
		for(int i = 1; i <= n; i++) {
			if(door[i] == 1) {
				merge(op[i][0] , op[i][1]);      //表示两个操作都执行
				merge(op[i][0]+m , op[i][1]+m);  //表示两个操作都不执行
			}
			else {
				merge(op[i][0] , op[i][1]+m);    //表示只执行操作op0
				merge(op[i][1] , op[i][0]+m);    //表示只执行操作op1
			}
		}
		int k = 0;
		for(int i = 1; i <= m; i++) {
			if(getf(i) == getf(i+m)) {
				k = 1;
				break;
			}
		}
		if(k) printf("NO\n");
		else  printf("YES\n");
	}
	return 0; 
}


网上还有令外的题解,用染色法(二分图),或者2-sat的方法,如果有兴趣可以去搜索一下。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值