【Codeforces 776 D The Door Problem】+ 并查集

D. The Door Problem
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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.

选择一把钥匙会改变该钥匙控制的所有门的状态 ? 每个门都有两把钥匙控制?同一把钥匙要么用要么不用?(i 表示用,i + M 表示不用)

AC代码:

#include<cstdio>
const int K = 1e5 + 10;
int f[K * 2],a[K],b[K][3];
int find(int p){ return p == f[p] ? f[p] : f[p] = find(f[p]); }
void bc(int x,int y){
    int fx = find(x),fy = find(y);
    if(fx != fy) f[fx] = fy;
}
int main()
{
    int N,M,l;
    scanf("%d %d",&N,&M);
    for(int i = 1; i <= N; i++) scanf("%d",&a[i]);
    for(int i = 1; i <= M * 2; i++) f[i] = i;
    for(int i = 1 ; i <= M; i++){
        scanf("%d",&l);
        for(int p, j = 1; j <= l; j++)
            scanf("%d",&p),b[p][++b[p][0]] = i;
    }
    for(int i = 1 ; i <= N; i++){
        int x = b[i][1],y = b[i][2];
        if(!a[i]) bc(x,y + M),bc(x + M,y);
        else bc(x,y),bc(x + M,y + M);
    }
    for(int i = 1 ; i <= M; i++) if(find(i) == find(i + M)) {printf("NO\n");return 0;}
    printf("YES\n");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值