Lamp
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1308 Accepted Submission(s): 434
Special Judge
Problem Description
There are several switches and lamps in the room, however, the connections between them are very complicated. One lamp may be controlled by several switches, and one switch may controls at most two lamps. And what’s more, some connections are reversed by mistake, so it’s possible that some lamp is lighted when its corresponding switch is “OFF”!
To make things easier, we number all the lamps from 1 to N, and all the switches 1 to M. For each lamps, we give a list of switches controlling it. For example, for Lamp 1, the list is “1 ON 3 OFF 9 ON”, that means Lamp 1 will be lighted if the Switch 1 is at the “ON” state OR the Switch 3 is “OFF” OR the Switch 9 is “ON”.
Now you are requested to turn on or off the switches to make all the lamps lighted.
To make things easier, we number all the lamps from 1 to N, and all the switches 1 to M. For each lamps, we give a list of switches controlling it. For example, for Lamp 1, the list is “1 ON 3 OFF 9 ON”, that means Lamp 1 will be lighted if the Switch 1 is at the “ON” state OR the Switch 3 is “OFF” OR the Switch 9 is “ON”.
Now you are requested to turn on or off the switches to make all the lamps lighted.
Input
There are several test cases in the input. The first line of each test case contains N and M (1 <= N,M <= 500), then N lines follow, each indicating one lamp. Each line begins with a number K, indicating the number of switches controlling this lamp, then K pairs of “x ON” or “x OFF” follow.
Output
Output one line for each test case, each contains M strings “ON” or “OFF”, indicating the corresponding state of the switches. For the solution may be not unique, any correct answer will be OK. If there are no solutions, output “-1” instead.
Sample Input
2 2 2 1 ON 2 ON 1 1 OFF 2 1 1 1 ON 1 1 OFF
Sample Output
OFF ON -1
Source
题意:有n盏灯,m个开关,每个灯在某些开关在ON状态或OFF状态下是打开的,问m个开关分别在什么状态下,所有灯是开的,若不能使所有灯打开则输出-1
解题思路:对每个开关的两个状态标记下哪些灯是打开的,然后就是舞蹈链的重复覆盖问题了,不过在选取了一盏灯的一个状态后,就不能选它的另一个状态
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cctype>
#include <map>
#include <cmath>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
const int maxn = 300005;
int n, m, x, y;
char ch[10];
vector<int>f[maxn][2];
struct DLX
{
int L[maxn], R[maxn], U[maxn], D[maxn];
int row[maxn], col[maxn], sum[maxn], ans[maxn];
int n, m, num, cnt;
int vis[maxn], flag[maxn];
void add(int k, int l, int r, int u, int d, int x, int y)
{
L[k] = l; R[k] = r; U[k] = u;
D[k] = d; row[k] = x; col[k] = y;
}
void reset(int n, int m)
{
this->n = n; this->m = m;
for (int i = 0; i <= m; i++)
{
add(i, i - 1, i + 1, i, i, 0, i);
sum[i] = 0;
}
L[0] = m, R[m] = 0, cnt = m + 1;
}
void insert(int x, int y)
{
int temp = cnt - 1;
if (row[temp] != x)
{
add(cnt, cnt, cnt, U[y], y, x, y);
U[D[cnt]] = cnt; D[U[cnt]] = cnt;
}
else
{
add(cnt, temp, R[temp], U[y], y, x, y);
R[L[cnt]] = cnt; L[R[cnt]] = cnt;
U[D[cnt]] = cnt; D[U[cnt]] = cnt;
}
sum[y]++, cnt++;
}
void Remove(int k)
{
for (int i = D[k]; i != k; i = D[i])
{
L[R[i]] = L[i];
R[L[i]] = R[i];
}
}
void Resume(int k)
{
for (int i = U[k]; i != k; i = U[i]) L[R[i]] = R[L[i]] = i;
}
int A()
{
int dis = 0;
for (int i = R[0]; i != 0; i = R[i]) vis[i] = 0;
for (int i = R[0]; i != 0; i = R[i])
if (!vis[i])
{
dis++, vis[i] = 1;
for (int j = D[i]; j != i; j = D[j])
for (int k = R[j]; k != j; k = R[k])
vis[col[k]] = 1;
}
return dis;
}
bool Dfs(int k)
{
if (!R[0]) { num = min(num, k); return true; }
else //if (k + A() < num)
{
int now = R[0];
for (int i = R[0]; i != 0; i = R[i])
if (sum[now] > sum[i]) now = i;
for (int i = D[now]; i != now; i = D[i])
if (!flag[row[i] ^ 1])
{
ans[k] = row[i];
flag[row[i]] = 1;
Remove(i);
for (int j = R[i]; j != i; j = R[j]) Remove(j);
if (Dfs(k + 1)) return true;
for (int j = L[i]; j != i; j = L[j]) Resume(j);
Resume(i);
flag[row[i]] = 0;
}
}
return false;
}
void display()
{
memset(vis, 0, sizeof vis);
for (int i = 0; i < num; i++) vis[ans[i] >> 1] = ans[i] & 1;
for (int i = 1; i + i < n; i++)
{
if (!vis[i]) printf("ON");
else printf("OFF");
if (i + i == n - 1) printf("\n");
else printf(" ");
}
}
void mul()
{
memset(flag, 0, sizeof flag);
num = 0x7FFFFFFF;
}
}dlx;
int main()
{
while (~scanf("%d%d", &n, &m))
{
dlx.reset(m + m + 1, n);
for (int i = 1; i <= m; i++) f[i][0].clear(), f[i][1].clear();
for (int i = 1; i <= n; i++)
{
scanf("%d", &x);
for (int j = 0; j < x; j++)
{
scanf("%d%s", &y, ch);
if (ch[1] == 'N') f[y][0].push_back(i);
else f[y][1].push_back(i);
}
}
for (int i = 1; i <= m; i++)
for (int j = 0; j < 2; j++)
for (int k = 0; k < f[i][j].size(); k++)
dlx.insert(2 * i + j, f[i][j][k]);
dlx.mul();
if (dlx.Dfs(0)) dlx.display();
else printf("-1\n");
}
return 0;
}