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. React基础知识点全解

    •      propTypes.defaultProps 作为 properties 定义,也可以在组件外部通过键值对方式进行设置. •      设置组件初始的 state不支持 getIniti ...

  2. 一个通用Makefile的编写

    作者:杨老师,华清远见嵌入式学院讲师. 我们在Linux环境下开发程序,少不了要自己编写Makefile,一个稍微大一些的工程下面都会包含很多.c的源文件.如果我们用gcc去一个一个编译每一个源文件的 ...

  3. MVC传递数据-传递对象或对象集合

    前言 本文主要介绍从View(或者js)文件向Controller提交对象或者对象集合.比方.将表格中的一行数据作为一个对象提交.或将多行数据作为一个集合提交到Controller. 回想 从View ...

  4. Systemd启动图形界面过程

    1 启动命令 systemctl isolate graphical.target 2 启动过程: 文件:/etc/systemd/system/graphical.target 来自:systemd ...

  5. HDU5233

    Gunner II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total ...

  6. spark 朴素贝叶斯

    训练代码(scala) import org.apache.spark.mllib.classification.{NaiveBayes,NaiveBayesModel} import org.apa ...

  7. sql/plus无法显示数据库问题

    登录PL/SQL Developer 这里省略Oracle数据库和PL/SQL Developer的安装步骤,注意在安装PL/SQL Developer软件时,不要安装在Program Files ( ...

  8. SAS小记

    2011年8月13日 最近一直在跟着李东风的<统计软件教程>学习SAS,刚刚学完初等统计,感觉还没入门,找不到matlab编程时那种手顺的感觉.继续学习吧,加油!     最近用spss处 ...

  9. 启动项目报错:org.springframework.beans.factory.UnsatisfiedDependencyException

    dubbo项目: 启动项目报错:(web端) org.springframework.beans.factory.UnsatisfiedDependencyException: Error creat ...

  10. (转载)RecyclerView之ItemDecoration由浅入深

    RecyclerView之ItemDecoration由浅入深 作者 小武站台 关注 2016.09.19 18:20 字数 1155 阅读 10480评论 15喜欢 91赞赏 3 译文的GitHub ...