hustoj 1017 - Exact cover dancing link

1017 - Exact cover

Time Limit: 15s Memory Limit: 128MB

Special Judge Submissions: 5851 Solved: 3092

DESCRIPTION
There is an N*M matrix with only 0s and 1s, (1 <= N,M <= 1000). An exact cover is a selection of rows such that every column has a 1 in exactly one of the selected rows. Try to find out the selected rows.
INPUT
There are multiply test cases. First line: two integers N, M; The following N lines: Every line first comes an integer C(1 <= C <= 100), represents the number of 1s in this row, then comes C integers: the index of the columns whose value is 1 in this row.
OUTPUT
First output the number of rows in the selection, then output the index of the selected rows. If there are multiply selections, you should just output any of them. If there are no selection, just output "NO".
SAMPLE INPUT
6 7
3 1 4 7
2 1 4
3 4 5 7
3 3 5 6
4 2 3 6 7
2 2 7
SAMPLE OUTPUT
3 2 4 6
HINT
SOURCE
dupeng
dancing link裸題,就不細說了。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<string>
#include<queue>
using namespace std;
#ifdef WIN32
#define LL "%I64d"
#else
#define LL "%lld"
#endif
#define MAXN 1100
#define MAXV MAXN*2
#define MAXE MAXV*2
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
#define MAXD 100000
typedef long long qword;
inline int nextInt()
{
        char ch;
        int x=0;
        bool flag=false;
        do
                ch=(char)getchar(),flag=(ch==‘-‘)?true:flag;
        while(ch<‘0‘||ch>‘9‘);
        do x=x*10+ch-‘0‘;
        while (ch=(char)getchar(),ch<=‘9‘ && ch>=‘0‘);
        return x*(flag?-1:1);
}

int n,m;
struct DLX_t
{
        static const int maxd=1000000;
        static const int maxn=1001;
        static const int maxm=1001;
        int L[maxd],R[maxd],U[maxd],D[maxd];
        int rw[maxd];
        int head;
        int topd;
        int chd[maxm];
        int col[maxd];
        int tt[maxm];
        int n,m;
        vector<int> res;
        void init(int nn,int mm)
        {
                n=nn;m=mm;
                topd=0;
                memset(L,0,sizeof(L));
                memset(R,0,sizeof(R));
                memset(D,0,sizeof(D));
                memset(U,0,sizeof(U));
                memset(tt,0,sizeof(tt));
                res.clear();
                head=++topd;
                L[head]=R[head]=head;
                for (int i=1;i<=m;i++)
                {
                        chd[i]=++topd;
                        col[chd[i]]=i;
                        rw[chd[i]]=0;
                        R[chd[i]]=head;
                        L[chd[i]]=L[head];
                        R[chd[i]]=head;
                        L[R[chd[i]]]=chd[i];
                        R[L[chd[i]]]=chd[i];
                        U[chd[i]]=D[chd[i]]=chd[i];
                }
        }
        void Add_row(int r,const vector<int> &vec)
        {
                int i;
                int nowh;
                int now;
                for (i=0;i<(int)vec.size();i++)
                {
                        now=++topd;
                        rw[now]=r;
                        col[now]=vec[i];
                        tt[vec[i]]++;
                        U[now]=U[chd[vec[i]]];
                        D[now]=chd[vec[i]];
                        D[U[now]]=now;
                        U[D[now]]=now;
                }
                L[U[chd[vec[0]]]]=R[U[chd[vec[0]]]]=U[chd[vec[0]]];
                nowh=U[chd[vec[0]]];
                for (i=1;i<(int)vec.size();i++)
                {
                        R[U[chd[vec[i]]]]=nowh;
                        L[U[chd[vec[i]]]]=L[nowh];
                        L[R[U[chd[vec[i]]]]]=U[chd[vec[i]]];
                        R[L[U[chd[vec[i]]]]]=U[chd[vec[i]]];
                }
        }
        void Finish()
        {
                vector<int> res2=res;
                sort(res2.begin(),res2.end());
                printf("%d",(int)res2.size());
                for (int i=0;i<(int)res2.size();i++)
                        printf(" %d",res2[i]);
                printf("\n");
        }
        void cover(int c)
        {
                int i,j;
                R[L[chd[c]]]=R[chd[c]];
                L[R[chd[c]]]=L[chd[c]];
                for (i=D[chd[c]];i!=chd[c];i=D[i])
                {
                        for (j=R[i];j!=i;j=R[j])
                        {
                                tt[col[j]]--;
                                U[D[j]]=U[j];
                                D[U[j]]=D[j];
                        }
                }
        }
        void resume(int c)
        {
                int i,j;
                R[L[chd[c]]]=chd[c];
                L[R[chd[c]]]=chd[c];
                for (i=D[chd[c]];i!=chd[c];i=D[i])
                {
                        for (j=R[i];j!=i;j=R[j])
                        {
                                tt[col[j]]++;
                                U[D[j]]=j;
                                D[U[j]]=j;
                        }
                }
        }
        bool dfs()
        {
                int now=head;
                if (L[now]==now)
                {
                        Finish();
                        return true;
                }
                int bst=INF,bi=-1;
                int i,j;
                for (i=R[head];i!=head;i=R[i])
                {
                        if (tt[col[i]]<bst)
                        {
                                bst=tt[i];
                                bi=i;
                        }
                }
                cover(col[bi]);
                for (i=D[bi];i!=bi;i=D[i])
                {
                        res.push_back(rw[i]);
                        for (j=R[i];j!=i;j=R[j])
                                cover(col[j]);
                        if (dfs())return true;
                        res.pop_back();
                        for (j=R[i];j!=i;j=R[j])
                                resume(col[j]);
                }
                resume(col[bi]);
                return false;
        }
}DLX;
vector<int> vec;
int main()
{
        freopen("input.txt","r",stdin);
        //freopen("output.txt","w",stdout);
        int i,j,k;
        int x,y,z;
        while (~scanf("%d%d",&n,&m))
        {
                DLX.init(n,m);
                for (i=1;i<=n;i++)
                {
                        scanf("%d",&y);
                        vec.clear();
                        for (j=1;j<=y;j++)
                        {
                                scanf("%d",&x);
                                vec.push_back(x);
                        }
                        sort(vec.begin(),vec.end());
                        DLX.Add_row(i,vec);
                }
                if (!DLX.dfs())
                        printf("NO\n");
        }
        return 0;
}

时间: 2024-10-31 22:15:50

hustoj 1017 - Exact cover dancing link的相关文章

HUST 1017 Exact cover (Dancing links)

1017 - Exact cover 时间限制:15秒 内存限制:128兆 自定评测 6110 次提交 3226 次通过 题目描述 There is an N*M matrix with only 0s and 1s, (1 <= N,M <= 1000). An exact cover is a selection of rows such that every column has a 1 in exactly one of the selected rows. Try to find o

Dancing Link --- 模板题 HUST 1017 - Exact cover

1017 - Exact cover Problem's Link:   http://acm.hust.edu.cn/problem/show/1017 Mean: 略 analyse: A Time complexity: O(n) Source code:  #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vecto

HUST 1017 - Exact cover (Dancing Links 模板题)

1017 - Exact cover 时间限制:15秒 内存限制:128兆 自定评测 5584 次提交 2975 次通过 题目描述 There is an N*M matrix with only 0s and 1s, (1 <= N,M <= 1000). An exact cover is a selection of rows such that every column has a 1 in exactly one of the selected rows. Try to find o

搜索(DLX):HOJ 1017 - Exact cover

1017 - Exact cover Time Limit: 15s Memory Limit: 128MB Special Judge Submissions: 6751 Solved: 3519 Description There is an N*M matrix with only 0s and 1s, (1 <= N,M <= 1000). An exact cover is a selection of rows such that every column has a 1 in e

(简单) 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 a selection of rows such that every column has a 1 in exactly one of the selected rows. Try to find out the selected rows. DLX精确覆盖的模板题...... 代码如下: //HUST 1

HUST 1017 Exact cover DLX

题意:一个n×m行的矩阵,每个格子可能是 0 或者1  现在让你选择 几列  使得 每一列 有且只有一个1.(精确覆盖模板题) 解题思路: dancing links 模板 关于dancing links  献上几篇必看论文 :http://par.buaa.edu.cn/acm-icpc/filepool/r/35/ 板子用的kuangbin的板子(还是适牛的..) 解题思路: 1 // File Name: hust1017.cpp 2 // Author: darkdream 3 // C

[DLX] hust 1017 Exact cover

题意: 给你N个包,要拿到M个东西(编号1~M每个只能有一个) 然后每个包里有k个东西,每个东西都有编号. 思路: 舞蹈连模板题 代码: #include"stdio.h" #include"algorithm" #include"string.h" #include"iostream" #include"queue" #include"map" #include"vector

Dancing Link专题

1.hust 1017 Exact cover (Dancing Links 模板题) 题意:n*m的单位矩阵.现在要选一些行,使得这些行的集合中每列只出现一个1. 思路:裸的精确覆盖问题.刷一遍模板. 1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 //精确覆盖问题的定义:给定一个由0-1组成的矩阵,是否能找到一个行的集合,使得集合中每一列都恰好包含一个1 5 const int M

dancing link模板

1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 #include<cmath> 6 #include<iomanip> 7 using namespace std; 8 9 const int n=729,m=324; 10 bool mx[2000][2000];//数独转化过来的01矩阵 11 int map[1