HDOJ 2828 Lamp DLX反复覆盖
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
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.
pairs of “x ON” or “x OFF” follow.
2 2
2 1 ON 2 ON
1 1 OFF
2 1
1 1 ON
1 1 OFF
OFF ON
-1
#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反复覆盖的更多相关文章
- [DLX反复覆盖] hdu 2828 Lamp
题意: 有N个灯M个开关 每一个灯的ON和OFF状态都能控制一个灯是否亮 给出N行,代表对于每一个灯 哪些开关的哪个状态能够使得第i个灯亮 思路: 这里须要注意一个问题 假设开关1的ON 状态和开关2 ...
- FZU 1686 神龙的难题(DLX反复覆盖)
FZU 1686 神龙的难题 pid=1686" target="_blank" style="">题目链接 题意:中文题 思路:每个1看成列, ...
- FZU 1686 神龙的难题 DLX反复覆盖
DLX反复覆盖: 须要一个A*函数剪支 Problem 1686 神龙的难题 Accept: 462 Submit: 1401 Time Limit: 1000 mSec Memory L ...
- HDU 5046 Airport(DLX反复覆盖)
HDU 5046 Airport 题目链接 题意:给定一些机场.要求选出K个机场,使得其它机场到其它机场的最大值最小 思路:二分+DLX反复覆盖去推断就可以 代码: #include <cstd ...
- HDU 4735 Little Wish~ lyrical step~(DLX , 反复覆盖)
解题思路: DLX 的模板题.反复覆盖. #include <stdio.h> #include <string.h> #include <iostream> #i ...
- [ACM] FZU 1686 神龙的难题 (DLX 反复覆盖)
Problem 1686 神龙的难题 Accept: 444 Submit: 1365 Time Limit: 1000 mSec Memory Limit : 32768 KB Pro ...
- [DLX反复覆盖] hdu 3656 Fire station
题意: N个点.再点上建M个消防站. 问消防站到每一个点的最大距离的最小是多少. 思路: DLX直接二分推断TLE了. 这时候一个非常巧妙的思路 我们求的距离一定是两个点之间的距离 因此我们把距离都求 ...
- [DLX反复覆盖] poj 1084 Square Destroyer
题意: n*n的矩形阵(n<=5),由2*n*(n+1)根火柴构成,那么当中会有非常多诸如边长为1,为2...为n的正方形,如今能够拿走一些火柴,那么就会有一些正方形被破坏掉. 求在已经拿走一些 ...
- [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 ...
随机推荐
- 前端的标配:npm是什么及其安装(含cnpm)
前端的标配:npm是什么及其安装 一:npm是什么及其来源 参考来源:npm是干什么的 总结:不需要去相关的网站下载依赖,用一个工具把这些依赖集中起来管理 NPM 的思路大概是这样的: 1)买个服务器 ...
- 简述Vue的响应式原理
当一个Vue实例创建时,vue会遍历data选项的属性,用 Object.defineProperty 将它们转为getter/setter并且在内部追踪相关依赖,在属性被访问和修改时通知变化.每个组 ...
- IE6 浏览器常见兼容问题 共23个
1.<!DOCTYPE HTML>文档类型的声明. 产生条件:IE6浏览器,当我们没有书写这个文档声明的时候,会触发IE6浏览器的怪异解析现象: 解决办法:书写文档声明. 2.不同浏览器当 ...
- nutch如何修改regex-urlfilter.txt爬取符合条件的链接
例如我在爬取学生在线的时候,发现爬取不到特定的通知,例如<中粮福临门助学基金申请公告>,通过分析发现原来通知的链接被过滤掉了,下面对过滤url的配置文件regex-urlfilter.tx ...
- sigprocmask和信号阻塞
注意阻塞和忽略,是有区别的. 阻塞只是暂时的,忽略是就没了. 参数:how:用于指定信号修改的方式,可能选择有三种:SIG_BLOCK //加入信号到进程屏蔽.SIG_UNBLOCK //从进程屏蔽里 ...
- HDU 4372
想了很久,终于想到了.... 向后看到F,向前看到B,假如把N-1个楼分成F+B个组,则把每个组最高的楼作为看到的楼,那么,其实在确定每一组的最高楼时,左边或右边的最高楼的顺序已经确定了.由于是排列数 ...
- 带输出參数的存储过程的定义,以及在aso.net中调用
ALTER proc [dbo].[mp_w_RechargePortalPayPal_All] ( @PayPalOrderNo nvarchar(50), --订单号 @nAccountIDFro ...
- 阿里云部署Docker(8)----安装和使用redmine
安装redmine对过程进行管理. 须要说明的是:当你在docker images的时候,会说没连接到xxxx的时候,并且会提示用"docker -d".事实上这仅仅是把docke ...
- UVALive 4223 / HDU 2962 spfa + 二分
Trucking Problem Description A certain local trucking company would like to transport some goods on ...
- h5缓存之数据库
/*======================================================= 函数功能:保存日志到本地数据库 ========================== ...