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 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.
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 "YES" without quotes, if it is possible to open all doors at the same time, otherwise output "NO" without quotes.
3 3 1 0 1 2 1 3 2 1 2 2 2 3
NO
3 3 1 0 1 3 1 2 3 1 2 2 1 3
YES
3 3 1 0 1 3 1 2 3 2 1 2 1 3
NO
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
2种解法:
第一种是并查集,
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <queue>
#define mem(p,k) memset(p,k,sizeof(p));
#define rep(a,b,c) for(int a=b;a<c;a++)
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define inf 0x6fffffff
#define ll long long
using namespace std;
struct SD{
int x,y,f;
char c;
}p,pp;
char mapp[50][50];
int nex[4][2]={0,1 ,1,0 ,0,-1 ,-1,0};
int n,m,x,y,k,l,sum,cur;
int a[210000],f[410000];
vector<int> v[210000];
int fin(int x){
return x==f[x]?x:f[x]=fin(f[x]);
}
void merg(int x,int y){
int xx=fin(x),yy=fin(y);
if(xx!=yy)f[xx]=yy;
}
int main()
{
int t;
while(cin>>n>>m){
rep(i,1,n+1)scanf("%d",a+i),v[i].clear();
rep(i,0,400001)f[i]=i;
rep(i,1,m+1){
scanf("%d",&k);
while(k--){
scanf("%d",&x);
v[x].pb(i);
}
}
rep(i,1,n+1){
x=v[i][0],y=v[i][1];
if(a[i]){
merg(x,y);
merg(x+m,y+m);
}
else{
merg(x+m,y);
merg(x,y+m);
}
}
cur=0;
rep(i,1,m+1){
if(fin(i)==fin(i+m)){
cur=1;break;
}
}
if(cur)cout<<"NO"<<endl;
else cout<<"YES"<<endl;
}
return 0;
}
第二种;
DFS+01染色
代码;
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <queue>
#define mem(p,k) memset(p,k,sizeof(p));
#define rep(a,b,c) for(int a=b;a<c;a++)
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define inf 0x6fffffff
#define ll long long
using namespace std;
struct SD{
int x,f;
}p,pp;
char mapp[50][50];
int nex[4][2]={0,1 ,1,0 ,0,-1 ,-1,0};
int n,m,x,y,k,l,sum,cur;
int a[210000],f[410000];
vector<struct SD> v[210000],vec[210000];
int fin(int x){
return x==f[x]?x:f[x]=fin(f[x]);
}
void merg(int x,int y){
int xx=fin(x),yy=fin(y);
if(xx!=yy)f[xx]=yy;
}
int dfs(int x){
for(int i=0;i<vec[x].size();i++){
if(f[vec[x][i].x]==-1){
f[vec[x][i].x]=f[x]^vec[x][i].f^1;
if(!dfs(vec[x][i].x))return 0;
}
else if(f[vec[x][i].x]!=f[x]^vec[x][i].f^1){
return 0;
}
}
return 1;
}
int main()
{
int t;
while(cin>>n>>m){
rep(i,1,n+1)scanf("%d",a+i),v[i].clear();
rep(i,1,m+1)vec[i].clear();
rep(i,1,m+1){
scanf("%d",&k);
while(k--){
scanf("%d",&x);
p.x=i;
v[x].pb(p);
}
}
rep(i,1,n+1){
p.x=v[i][0].x,p.f=a[i];
vec[v[i][1].x].pb(p);
p.x=v[i][1].x;
vec[v[i][0].x].pb(p);
}
mem(f,-1);
cur=0;
rep(i,1,m+1)
if(f[i]==-1){
f[i]=0;
if(!dfs(i)){
cur=1;break;
}
}
if(cur)cout<<"NO"<<endl;
else cout<<"YES"<<endl;
}
return 0;
}