DLX反复覆盖模版题:

每一个开关两个状态。但仅仅能选一个,建2m×n的矩阵跑DLX模版。。

。。

Lamp

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 855    Accepted Submission(s): 265

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

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm> using namespace std; const int maxn=1010,maxm=510;
const int maxnode=maxn*maxm;
const int INF=0x3f3f3f3f; int n,m;
bool flag; struct DLX
{
int n,m,size;
int U[maxnode],D[maxnode],R[maxnode],L[maxnode],Row[maxnode],Col[maxnode];
int H[maxn],S[maxm];
int ansd,ans[maxn];
int LAMP[maxn];
bool vis[maxn];
void init(int _n,int _m)
{
n=_n; m=_m;
for(int i=0;i<=m;i++)
{
S[i]=0; U[i]=D[i]=i;
L[i]=i-1; R[i]=i+1;
}
R[m]=0; L[0]=m;
size=m;
for(int i=1;i<=n;i++)
{
vis[i]=false;
LAMP[i]=0;
H[i]=-1;
}
flag=false;
}
void Link(int r,int c)
{
++S[Col[++size]=c];
Row[size]=r;
D[size]=D[c];
U[D[c]]=size;
U[size]=c;
D[c]=size;
if(H[r]<0) H[r]=L[size]=R[size]=size;
else
{
R[size]=R[H[r]];
L[R[H[r]]]=size;
L[size]=H[r];
R[H[r]]=size;
}
}
void remove(int c)
{
for(int i=D[c];i!=c;i=D[i])
L[R[i]]=L[i],R[L[i]]=R[i];
}
void resume(int c)
{
for(int i=U[c];i!=c;i=U[i])
L[R[i]]=R[L[i]]=i;
}
void Dance(int d)
{
if(flag) return ;
if(R[0]==0)
{
///find ans
for(int i=0;i<d;i++)
{
int lamp=(ans[i]+1)/2;
if(ans[i]%2) LAMP[lamp]=1;
}
for(int i=1;i<=n/2;i++)
{
if(LAMP[i]==1) printf("ON");
else printf("OFF");
if(i!=n/2) putchar(32); else putchar(10);
}
flag=true;
return ;
}
int c=R[0];
for(int i=R[0];i!=0;i=R[i])
{
if(S[i]<S[c]) c=i;
}
for(int i=D[c];i!=c;i=D[i])
{
if(vis[Row[i]]) continue;
int r1=Row[i],r2=Row[i];
if(r1%2==0) r2--;else r2++;
vis[r1]=true; vis[r2]=true;
remove(i);
for(int j=R[i];j!=i;j=R[j]) remove(j);
ans[d]=Row[i];
Dance(d+1);
for(int j=L[i];j!=i;j=L[j]) resume(j);
resume(i);
vis[r1]=false; vis[r2]=false;
}
}
}; DLX dlx; int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
dlx.init(2*m,n);
for(int i=1;i<=n;i++)
{
int k;
scanf("%d",&k);
for(int j=0;j<k;j++)
{
int p; char sw[20];
scanf("%d%s",&p,sw);
if(sw[1]=='N') dlx.Link(2*p-1,i);
else if(sw[1]=='F') dlx.Link(2*p,i);
}
}
dlx.Dance(0);
if(flag==false) puts("-1");
}
return 0;
}

HDOJ 2828 Lamp DLX反复覆盖的更多相关文章

  1. [DLX反复覆盖] hdu 2828 Lamp

    题意: 有N个灯M个开关 每一个灯的ON和OFF状态都能控制一个灯是否亮 给出N行,代表对于每一个灯 哪些开关的哪个状态能够使得第i个灯亮 思路: 这里须要注意一个问题 假设开关1的ON 状态和开关2 ...

  2. FZU 1686 神龙的难题(DLX反复覆盖)

    FZU 1686 神龙的难题 pid=1686" target="_blank" style="">题目链接 题意:中文题 思路:每个1看成列, ...

  3. FZU 1686 神龙的难题 DLX反复覆盖

    DLX反复覆盖: 须要一个A*函数剪支 Problem 1686 神龙的难题 Accept: 462    Submit: 1401 Time Limit: 1000 mSec    Memory L ...

  4. HDU 5046 Airport(DLX反复覆盖)

    HDU 5046 Airport 题目链接 题意:给定一些机场.要求选出K个机场,使得其它机场到其它机场的最大值最小 思路:二分+DLX反复覆盖去推断就可以 代码: #include <cstd ...

  5. HDU 4735 Little Wish~ lyrical step~(DLX , 反复覆盖)

    解题思路: DLX 的模板题.反复覆盖. #include <stdio.h> #include <string.h> #include <iostream> #i ...

  6. [ACM] FZU 1686 神龙的难题 (DLX 反复覆盖)

    Problem 1686 神龙的难题 Accept: 444    Submit: 1365 Time Limit: 1000 mSec    Memory Limit : 32768 KB  Pro ...

  7. [DLX反复覆盖] hdu 3656 Fire station

    题意: N个点.再点上建M个消防站. 问消防站到每一个点的最大距离的最小是多少. 思路: DLX直接二分推断TLE了. 这时候一个非常巧妙的思路 我们求的距离一定是两个点之间的距离 因此我们把距离都求 ...

  8. [DLX反复覆盖] poj 1084 Square Destroyer

    题意: n*n的矩形阵(n<=5),由2*n*(n+1)根火柴构成,那么当中会有非常多诸如边长为1,为2...为n的正方形,如今能够拿走一些火柴,那么就会有一些正方形被破坏掉. 求在已经拿走一些 ...

  9. [ACM] HDU 2295 Radar (二分法+DLX 重复覆盖)

    Radar Problem Description N cities of the Java Kingdom need to be covered by radars for being in a s ...

随机推荐

  1. IT同行请教我如何培养读书习惯,结果就是“读了1本书,并写下'读《成交》有感'一文”

    前段时间,我把CSDN博客的签名加上了"读过100+本经典书籍". 一个经常关注我CSDN博客的老乡,问我是如何做到的. 该老乡,准确来说是前辈,该前辈买了很多技术读物却没有耐心读 ...

  2. 【codeforces 500E】New Year Domino

    [题目链接]:http://codeforces.com/problemset/problem/500/E [题意] 有n个多米诺骨牌; 你知道它们的长度; 然后问你,如果把第i骨牌往后推倒,然后要求 ...

  3. ubuntu16.04安装opencl

    参考链接:https://www.jianshu.com/p/ad808584ce26 安装OpenCL OpenCL是一系列库和头文件,需要根据硬件安装对应的SDK,也就是说,如果希望使用Intel ...

  4. BigDecimal相除异常

    使用两个BigDecimal类型的数字做除法运算时,出现了一个如下的异常信息: 1 java.lang.ArithmeticException: Non-terminating decimal exp ...

  5. quartz.net定时任务框架详解

    C#做定时任务:一是Timer:而是Quartz.net:本文就学习一下Quartz.net框架 Quartz.net非常的灵活,开发人员能用很少的代码就能完成“每隔一小时执行”.“每天2点执行”.“ ...

  6. 头像文件上传 方法一:from表单 方法二:ajax

    方法一:from表单 html 设置form表单,内包含头像预览div,内包含上传文件input 设置iframe用来调用函数传参路径 <!--表单提交成功后不跳转处理页面,而是将处理数据返回给 ...

  7. css3 字体、2D转换、3D转换

    学习篇之CSS3 字体.2D转换.3D转换 一.字体 @font-face 将字体文件存放到 web 服务器上,通过CSS3 @font-face规则中定义,它会在需要时被自动下载到用户的计算机上. ...

  8. 准备把平台挪到linux

    在上午准备周末胡老师的课程考核的Ppt时,逐渐我觉得不得不把平台挪到linux了.很多并行的应用不只是在linux上效率更高,而且很多包都在linux上.另外如果不及早挪到Linux上,后面遇到的问题 ...

  9. Codeforces 987A. Infinity Gauntlet(手速题,map存一下输出即可)

    解法: 1.先将对应的字符串存入map. 2.然后将输入的串的second置为空. 3.输出6-n,输出map中的非空串. 代码: #include <bits/stdc++.h> usi ...

  10. solarwinds之数据库

      1.              Orion配置向导     2.              连接数据库     3.              创建一个新的数据库     4.           ...