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个锁,给出门初始状态(0为锁,1为未锁)和每个锁管辖的门(每个门有且只有两个锁管辖,当其中一个锁状态发改变时,门的状态随之改编)问能否在同一时间所有的门都是开的。
题解:
每个门由两个锁掌控,就变成一个2-sat问题。根据门的状态建边即可。
代码:
#include<bits/stdc++.h>
using namespace std;
struct Edge
{
int from,to,next;
}e[15000000];
int stackk[500000];
int color[500000];
int dfn[500000];
int low[500000];
int vis[500000];
int head[505000];
vector<int >s[120050];
int num[120050];
int a[120050];
int cont,cnt,sig,tt;
void add(int from,int to)
{
e[cont].to=to;
e[cont].next=head[from];
head[from]=cont++;
}
void Tarjan(int u)
{
vis[u]=1;
dfn[u]=low[u]=cnt++;
stackk[++tt]=u;
for(int i=head[u];i!=-1;i=e[i].next)
{
int v=e[i].to;
if(vis[v]==0)Tarjan(v);
if(vis[v]==1)low[u]=min(low[u],low[v]);
}
if(dfn[u]==low[u])
{
sig++;
do
{
color[stackk[tt]]=sig;
vis[stackk[tt]]=-1;
}
while(stackk[tt--]!=u);
}
}
int Slove(int n)
{
cnt=1,sig=0,tt=-1;
memset(stackk,0,sizeof(stackk));
memset(color,0,sizeof(color));
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(vis,0,sizeof(vis));
for(int i=1;i<=n*2;i++)
{
if(vis[i]==0)Tarjan(i);
}
for(int i=1;i<=n;i++)
{
if(color[i]==color[i+n])return 0;
}
return 1;
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
cont=0;
memset(head,-1,sizeof(head));
for(int i=1;i<=100040;i++)s[i].clear();
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
for(int i=1;i<=m;i++)
{
int k;
scanf("%d",&k);
while(k--)
{
int num;
scanf("%d",&num);
s[num].push_back(i);
}
}
for(int i=1;i<=n;i++)
{
int u=s[i][0];
int v=s[i][1];
if(a[i]==0)
{
add(u,v+m);
add(v+m,u);
add(v,u+m);
add(u+m,v);
}
if(a[i]==1)
{
add(u,v);
add(v,u);
add(u+m,v+m);
add(v+m,u+m);
}
}
int ans=Slove(m);
if(ans==1)printf("YES\n");
else printf("NO\n");
}
}