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;
}

最新文章

  1. Java_DOM创建XML
  2. 继续OI
  3. 【转】PHP中require和include路径问题总结
  4. com组件 Ieframe的主页锁定
  5. 在jQuery和JavaScript中,实现转跳
  6. android的进度条使用
  7. C# - Delegate Simple Demo
  8. Orchard中的多语言功能
  9. 转载--http协议学习和总结
  10. Java获取客户端真实IP地址的两种方法
  11. vscode中使用markdown(转)
  12. 把dotx模板的样式应用到当前文档中(不应用dotx的其他东西)
  13. react-router-dom v^4路由、带参路由的配置
  14. settings设置中文admin
  15. RetrieveFavicon 获取任何站点的 favicon
  16. Lua和C++交互 学习记录之二:栈操作
  17. spring 核心思想:AOP 理解
  18. C++之类和对象的特性
  19. Python -- 数据结构实现
  20. bzoj千题计划146:bzoj3295: [Cqoi2011]动态逆序对

热门文章

  1. python课堂知识的几点总结
  2. ubuntu/wireshark --Lua: Error during loading: [string &quot;/usr/share/wireshark/init.lua&quot;]:45问题解决
  3. IOS系统兼容input keyup事件
  4. php获取时间是星期几
  5. 洛谷 1063 dp 区间dp
  6. cogs 2060. 除法表达式
  7. struts.xml中出现extends undefined package struts-default解决的方法
  8. 深刻理解Docker镜像大小
  9. matplotlib 可视化 —— matplotlib.patches
  10. [Codeforces 1051F] The Shortest Statement 解题报告(树+最短路)