1. 程式人生 > >ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) D

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

see otherwise tex 並查集 examples tree per sta with

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 1to 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.

題意:一個門受到兩個開關控制,問最後能不能把門全部打開(詳情看輸入格式和解釋)

解法:

1 如果門是開的,那麽控制的兩個開關要麽一起關,要麽一起開

2 如果門是關的,那麽控制的兩個開關只需要開一個

3 不能全部開的條件,很幸運,經過分析發現 有一個控制關a門,也控制開a門,那麽這門是打不開了(矛盾了)

4 我們把開關看做點,門看做線,沒有+m看作是開門,+m看作關門,用並查集處理,找到一個開門和關門處於同一個集合的情況,矛盾不存在可行結果

#include<bits/stdc++.h>
typedef long long LL;
typedef unsigned long long ULL;
using namespace std;
const int maxn=1e5;
vector<int>Ve[2*maxn];
int tree[2*maxn];
int Find(int x)
{
    if(x==tree[x])
        return x;
    return tree[x]=Find(tree[x]);
}

void Merge(int x,int y)
{
    int fx=Find(x);
    int fy=Find(y);
    if(fx!=fy)
        tree[fx]=fy;
}
int door[maxn];
int main(){
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=2*m;i++){
        tree[i]=i;
    }
    for(int i=1;i<=n;i++){
        scanf("%d",&door[i]);
    }
    for(int i=1;i<=m;i++){
        int num;
        scanf("%d",&num);
        for(int j=1;j<=num;j++){
            int x;
            scanf("%d",&x);
            Ve[x].push_back(i);
        }
    }
    for(int i=1;i<=n;i++){
        int x=Ve[i][0];
        int y=Ve[i][1];
        if(door[i]){
            Merge(x,y);
            Merge(x+m,y+m);
        }else{
            Merge(x,y+m);
            Merge(x+m,y);
        }
    }
    int flag=1;
    for(int i=1;i<=m;++i){
        if(Find(i)==Find(i+m)){
            flag=0;
            break;
        }
    }
    if(flag)
        printf("YES\n");
    else
        printf("NO\n");
    return 0;
}

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