题目链接:http://codeforces.com/contest/776/problem/D
2 seconds
256 megabytes
standard input
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.
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.
题意: 给你n个锁,m个开关,每个开关控制若干个锁;开关每次可以取反;
让你动开关使得最后的全为1;
思路:2-SAT;
but each door is controlled by exactly two switches.
题目中这段加粗的话,每个锁只被两个开关控制;
开关当点,所以当锁为1时,控制的两个开关必须同时使用,或者同时不使用,连边;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x) cout<<"bug"<<x<<endl;
const int N=1e6+,M=1e7+,inf=1e9+;
const ll INF=1e18+,mod=1e9+; /// 数组大小
const int MAXNODE = * ; //两倍 struct TwoSat {
int n;
vector<int> g[MAXNODE];
int pre[MAXNODE], dfn[MAXNODE], dfs_clock, sccn, sccno[MAXNODE];
stack<int> S;
int mark[MAXNODE]; void init(int tot) {
n = tot * ;
for (int i = ; i < n; i+= ) {
g[i].clear();
g[i^].clear();
}
} void add_Edge(int u, int v) {
g[u].push_back(v);
g[v].push_back(u);
} void dfs_scc(int u) {
pre[u] = dfn[u] = ++dfs_clock;
S.push(u);
for (int i = ; i < g[u].size(); i++) {
int v = g[u][i];
if (!pre[v]) {
dfs_scc(v);
dfn[u] = min(dfn[u], dfn[v]);
} else if (!sccno[v]) dfn[u] = min(dfn[u], pre[v]);
}
if (pre[u] == dfn[u]) {
sccn++;
while () {
int x = S.top(); S.pop();
sccno[x] = sccn;
if (x == u) break;
}
}
} void find_scc() {
dfs_clock = sccn = ;
memset(sccno, , sizeof(sccno));
memset(pre, , sizeof(pre));
for (int i = ; i < n; i++)
if (!pre[i]) dfs_scc(i);
} bool solve() {
find_scc();
for (int i = ; i < n; i += ) {
if (sccno[i] == sccno[i + ]) return false;
mark[i / ] = (sccno[i] > sccno[i + ]);
}
return true;
}
} gao;
vector<int>v[N];
int r[N];
int main()
{
int n,m;
scanf("%d%d",&n,&m);
gao.init(m);
for(int i=;i<=n;i++)
scanf("%d",&r[i]);
for(int i=;i<=m;i++)
{
int x,z;
scanf("%d",&x);
for(int j=;j<x;j++)
{
scanf("%d",&z);
v[z].push_back(i);
}
}
for(int i=;i<=n;i++)
{
if(r[i])
{
gao.add_Edge(v[i][]*+,v[i][]*+);
gao.add_Edge(v[i][]*,v[i][]*);
}
else
{
gao.add_Edge(v[i][]*+,v[i][]*);
gao.add_Edge(v[i][]*,v[i][]*+);
}
}
if(gao.solve())puts("YES");
else puts("NO");
return ;
}