hdu 3498(DLX 重复覆盖)

简单重复覆盖题。。。

whosyourdaddy

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1022    Accepted Submission(s): 502

Problem Description
sevenzero liked Warcraft very much, but he haven't practiced it for several years after being addicted to algorithms. Now, though he is playing with computer, he nearly losed and only his hero Pit Lord left. sevenzero is angry, he decided to cheat to turn defeat into victory. So he entered "whosyourdaddy", that let Pit Lord kill any hostile unit he damages immediately. As all Warcrafters know, Pit Lord masters a skill called Cleaving Attack and he can damage neighbour units of the unit he attacks. Pit Lord can choice a position to attack to avoid killing partial neighbour units sevenzero don't want to kill. Because sevenzero wants to win as soon as possible, he needs to know the minimum attack times to eliminate all the enemys.
 
Input
There are several cases. For each case, first line contains two integer N (2 ≤ N ≤ 55) and M (0 ≤ M ≤ N*N),and N is the number of hostile units. Hostile units are numbered from 1 to N. For the subsequent M lines, each line contains two integers A and B, that means A and B are neighbor. Each unit has no more than 4 neighbor units. The input is terminated by EOF.
 
Output
One line shows the minimum attack times for each case.
 
Sample Input
5 4 1 2 1 3 2 4 4 5 6 4 1 2 1 3 1 4 4 5
 
Sample Output
2 3
 
Author
sevenzero
 
Source
 
Recommend
zhouzeyong
 
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
#define N 5000
#define INF 0x3ffffff

struct node
{
    int to,next;
}edge[N];

int R[N],L[N],U[N],D[N],num[N],col[N],line[N],H[N];
int head;
int n,m;
int g[60][60];
int nn,mm;
int cnt,pre[60];
int id;
int mi;

void add_edge(int x,int y)
{
    edge[cnt].to=y;
    edge[cnt].next=pre[x];
    pre[x]=cnt++;
}

void prepare()
{
    for(int i=0;i<=mm;i++)
    {
        num[i]=0;
        U[i]=i;
        D[i]=i;
        R[i]=i+1;
        L[i+1]=i;
    }
    R[mm]=0;
    L[0]=mm;
    memset(H,-1,sizeof(H));
}

void link(int tn,int tm)
{
    id++;
    num[line[id]=tm ]++;
    col[id]=tn;
    U[D[tm]]=id;
    D[id]=D[tm];
    U[id]=tm;
    D[tm]=id;
    if(H[tn]<0) H[tn]=R[id]=L[id]=id;
    else
    {

        L[R[H[tn]]]=id;
        R[id]=R[ H[tn] ];
        L[id]=H[tn];
        R[ H[tn] ]=id;
    }
}

void build()
{
    id=mm;
    prepare();
    for(int i=1;i<=n;i++)
    {
        link(i,i);
        for(int p=pre[i];p!=-1;p=edge[p].next)
        {
            int v=edge[p].to;
            link(i,v);
        }
    }    
}

int h()
{
    int mark[60];
    memset(mark,0,sizeof(mark));
    int sum=0;
    for(int i=R[head];i!=head;i=R[i])
    {
        if(mark[i]==0)
        {
            sum++;
            for(int j=D[i];j!=i;j=D[j])
                for(int k=R[j];k!=j;k=R[k])
                    mark[ line[k] ]=1;
        }
    }
    return sum;
}

void remove(int s)
{
    for(int i=D[s];i!=s;i=D[i])
    {
        L[R[i]]=L[i];
        R[L[i]]=R[i];
    }
}

void resume(int s)
{
    for(int i=D[s];i!=s;i=D[i])
        L[R[i]]=R[L[i]]=i;
}
void dfs(int s)
{
    if(s+h()>=mi) return ;
    if(R[head]==head)
    {
        mi=s;
        return ;
    }
    int tmi=INF,tu;
    for(int i=R[head];i!=head;i=R[i])
        if(num[i]<tmi)
        {
            tmi=num[i];
            tu=i;
        }
    for(int i=D[tu];i!=tu;i=D[i])
    {
        remove(i);
        for(int j=R[i];j!=i;j=R[j])
            remove(j);
        dfs(s+1);
        for(int j=L[i];j!=i;j=L[j])
            resume(j);
        resume(i);
    }
}

int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        head=cnt=0;
        memset(pre,-1,sizeof(pre));
        for(int i=0;i<m;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            add_edge(x,y);
            add_edge(y,x);
        }
        nn=0; mm=n;
        build();
        mi=INF;
        dfs(0);
        printf("%d\n",mi);
    }
}
原文地址:https://www.cnblogs.com/chenhuan001/p/3007302.html