【hust1017】【DLX】Exact cover

12 篇文章 0 订阅
3 篇文章 0 订阅

1017 - Exact cover

Time Limit: 15s Memory Limit: 128MB
Special Judge Submissions: 7122 Solved: 3668

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

SOURCE

dupeng

舞蹈链裸题,没什么好多说的,就是基本的舞蹈链算法
然后注意有多组数据
直接放代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<stack>
#define INF 2100000000
#define ll long long
#define clr(x)  memset(x,0,sizeof(x));
#define M 100000

using namespace std;

struct link2
{
    link2 *up,*down,*right,*left,*col;
    int row,count,lie;
}*head,*c[M],*lik[M],dizhi[M];

int T,ans[M],sta,n,Ans,m;

void remove(link2 *C)
{
    C->left->right=C->right;
    C->right->left=C->left;

    link2 *i=C->down;
    while(i!=C)
    {
        link2 *j=i->right;
        while(j!=i)
        {
            j->up->down=j->down;
            j->down->up=j->up;
            j=j->right;
            j->col->count--;
        }
        i=i->down;
    }
}

void resume(link2 *C)
{
    C->left->right=C;
    C->right->left=C;

    link2 *i=C->down;
    while(i!=C)
    {
        link2 *j=i->right;
        while(j!=i)
        {
            j->up->down=j;
            j->down->up=j;
            j=j->right;
            j->col->count++;
        }
        i=i->down;
    }
}

int t;

void addline(int *index,int row,int x)
{
    for(int i=1;i<=x;i++)
    {
        t++;
        lik[t]=&dizhi[t+T];
        if(i==1)
        {
            lik[t]->left=lik[t];
            lik[t]->right=lik[t];
        }
        else
        {
            lik[t]->left=lik[t-1];
            lik[t]->right=lik[t-1]->right;
            lik[t-1]->right->left=lik[t];
            lik[t-1]->right=lik[t];
        }
        int j=index[i];
        c[j]->count++;
        lik[t]->up=c[j]->up;
        c[j]->up->down=lik[t];
        lik[t]->down=c[j];
        c[j]->up=lik[t];
        lik[t]->col=c[j];
        lik[t]->row=row;
        lik[t]->lie=j;
    }
}

bool dance(int x)
{
    if(head->right==head)
    {
        sort(ans+1,ans+sta+1);
        printf("%d ",sta);
        for(int i=1;i<=sta;i++)
            printf("%d ",ans[i]);
        return 1;
    }
    link2 *i=head->right->right;
    link2 *temp=head->right;
    while(i!=head)
    {
        if(i->count<temp->count)
            temp=i;
        i=i->right;
    }
    if(temp->down==temp)return 0;
    remove(temp);
    i=temp->down;
    while(i!=temp)
    {
        link2 *j=i->right;
        ans[++sta]=i->row;
        while(j!=i)
        {
            remove(j->col);
            j=j->right;
        }
        if(dance(x+1))return 1;
        j=i->left;
        while(j!=i)
        {
            resume(j->col);
            j=j->left;
        }
        ans[sta]=0;
        sta--;
        i=i->down;
    }
    resume(temp);
    return 0;
}

int q;

int main()
{
    freopen("in.txt","r",stdin);
    int b=0;
    while(cin>>n)
    {
        cin>>q;
        clr(dizhi);
        t=0;T=0;
        clr(ans);
        sta=0;
        m=q;
        head=&dizhi[T++];
        for(int i=1;i<=m;i++)c[i]=&dizhi[T++];
        head->right=c[1];
        head->left=c[m];
        for(int i=1;i<=m;i++)
        {
            c[i]->left=i==1?head:c[i-1];
            c[i]->right=i==m?head:c[i+1];
            c[i]->up=c[i]->down=c[i];
            c[i]->count=0;
            c[i]->lie=i;
        }
        for(int i=1;i<=n;i++)
        {
            int index[1000];
            clr(index);
            scanf("%d",&m);
            for(int j=1;j<=m;j++)
                scanf("%d",&index[j]);
            addline(index,i,m);
        }
        if(b)cout<<'\n';
        b=1;
        if(dance(0));
        else cout<<"NO";

    }

}

大概就是这个样子,如果有什么问题,或错误,请在评论区提出,谢谢。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值