HUST 1017 Exact cover dance links
学习:请看 www.cnblogs.com/jh818012/p/3252154.html
模板题,上代码
#include<cstdio>
#include<cstring>
#include<queue>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
typedef long long LL;
const int N=;
const LL mod=1e9+;
int n,m,sz;
int u[N],l[N],r[N],d[N];
int col[N],row[N];
int h[],s[];
void init()
{
for(int i=;i<=m;++i)
{
s[i]=;
u[i]=d[i]=i;
l[i]=i-;
r[i]=i+;
}
r[m]=;l[]=m;
sz=m;
for(int i=;i<=n;++i)
h[i]=-;
}
void link(int x,int y)
{
++sz;
++s[y],col[sz]=y,row[sz]=x;
u[sz]=u[y],d[u[y]]=sz;
d[sz]=y,u[y]=sz;
if(h[x]==-)h[x]=l[sz]=r[sz]=sz;
{
l[sz]=l[h[x]];
r[l[h[x]]]=sz;
r[sz]=h[x];
l[h[x]]=sz;
}
}
void del(int y)
{
l[r[y]]=l[y];
r[l[y]]=r[y];
for(int i=d[y];i!=y;i=d[i])
{
for(int j=r[i];j!=i;j=r[j])
{
u[d[j]]=u[j];
d[u[j]]=d[j];
--s[col[j]];
}
}
}
void resume(int y)
{
for(int i=u[y];i!=y;i=u[i])
{
for(int j=l[i];j!=i;j=l[j])
{
d[u[j]]=u[d[j]]=j;
++s[col[j]];
}
}
r[l[y]]=l[r[y]]=y;
}
int ans[],tmp;
bool dance(int pos)
{
if(!r[])
{
tmp=pos;
return ;
}
int t=r[];
for(int i=r[];i!=;i=r[i])
if(s[i]<s[t])t=i;
del(t);
for(int i=d[t];i!=t;i=d[i])
{
ans[pos]=row[i];
for(int j=r[i];j!=i;j=r[j])
del(col[j]);
if(dance(pos+))return ;
for(int j=l[i];j!=i;j=l[j])
resume(col[j]);
}
resume(t);
return ;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
init();
for(int i=;i<=n;++i)
{
int x,y;
scanf("%d",&x);
while(x--)
{
scanf("%d",&y);
link(i,y);
}
}
if(dance())
{
printf("%d",tmp);
for(int i=;i<tmp;++i)
printf(" %d",ans[i]);
printf("\n");
}
else printf("NO\n");
}
return ;
}
HUST 1017 Exact cover dance links的更多相关文章
- HUST 1017 Exact cover (Dancing links)
1017 - Exact cover 时间限制:15秒 内存限制:128兆 自定评测 6110 次提交 3226 次通过 题目描述 There is an N*M matrix with only 0 ...
- [ACM] HUST 1017 Exact cover (Dancing Links,DLX模板题)
DESCRIPTION There is an N*M matrix with only 0s and 1s, (1 <= N,M <= 1000). An exact cover is ...
- HUST 1017 - Exact cover (Dancing Links 模板题)
1017 - Exact cover 时间限制:15秒 内存限制:128兆 自定评测 5584 次提交 2975 次通过 题目描述 There is an N*M matrix with only 0 ...
- Dancing Link --- 模板题 HUST 1017 - Exact cover
1017 - Exact cover Problem's Link: http://acm.hust.edu.cn/problem/show/1017 Mean: 给定一个由0-1组成的矩阵,是否 ...
- (简单) HUST 1017 Exact cover , DLX+精确覆盖。
Description There is an N*M matrix with only 0s and 1s, (1 <= N,M <= 1000). An exact cover is ...
- [HUST 1017] Exact cover
Exact cover Time Limit: 15s Memory Limit: 128MB Special Judge Submissions: 6012 Solved: 3185 DESCRIP ...
- HUST 1017 Exact cover(DLX精确覆盖)
Description There is an N*M matrix with only 0s and 1s, (1 <= N,M <= 1000). An exact cover is ...
- [DLX] hust 1017 Exact cover
题意: 给你N个包,要拿到M个东西(编号1~M每一个仅仅能有一个) 然后每一个包里有k个东西,每一个东西都有编号. 思路: 舞蹈连模板题 代码: #include"stdio.h" ...
- 搜索(DLX):HOJ 1017 - Exact cover
1017 - Exact cover Time Limit: 15s Memory Limit: 128MB Special Judge Submissions: 6751 Solved: 3519 ...
随机推荐
- csharp excel interop programming
string fileName = "c:\\a.xlsx"; var application = new Application(); application.Visible = ...
- How to open MS word document from the SharePoint 2010 using Microsoft.Office.Interop.dll
or this you must change the identity of word component inC:\windows\System32\comexp.mscto be interac ...
- 1052: [HAOI2007]覆盖问题 - BZOJ
Description 某人在山上种了N棵小树苗.冬天来了,温度急速下降,小树苗脆弱得不堪一击,于是树主人想用一些塑料薄膜把这些小树遮盖起来,经过一番长久的思考,他决定用3个L*L的正方形塑料薄膜将小 ...
- tomcat集群时统计session与在线人数
tomcat集群时,原来通过HttpSessionListener实现类监听session的创建和销毁来统计在线人数的方法不再有效,因为不是每个人登陆都会在同一个tomcat服务器上,而在另一台tom ...
- linux 添加用户
1.下面就开始来说怎么添加新用户 我们以添加一个用户名为jorcen的新用户为例来说明,执行下面的命令: # useradd jorcen 2.那么就新用户就添加完成,但是没有任何信息,新用户添加完成 ...
- c#带参数和返回值的函数 开启线程调用的方法
public delegate string DgTest(); private void btn_District_Click(object sender, EventArgs e) { //实例化 ...
- The 11th Zhejiang Provincial Collegiate Programming Contest->Problem A:A - Pokemon Master
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3776 题意:比较两组数据的总和大小. #include <iostr ...
- Android 图片旋转(使用Matrix.setRotate方法)
imageView2 = (ImageView) findViewById(R.id.img2); Bitmap bitmap = BitmapFactory.decodeResource(getRe ...
- 使用eclipse远程调试Tomcat的方法
tomcat是一种非常常见的java web应用服务器,有时候服务器可能并不是部署在本地,而是部署在远程其他的机器上,我们用eclispe该如何进行debug调试呢? 1. 在eclispe中新建we ...
- Android 使用split函数进行多个空格分割
在项目中经常会遇到按字符分割字符串的情况,可以使用String对象的split函数进行分割. 先看实际情况: String str = "关键词1 关键词2 关键词3"; Stri ...