HDU3957 Street Fighter

    比赛的时候知道是最小支配集问题,想到了用DLX,不过没能A掉。

    题意:

    街霸游戏中,有n种角色,每种角色有1~2种人物,比如说Ryu有两种人:Metsu Hadoke和Metsu Shoryuken。Ryu用Metsu Hadoke可以轻易打败Chun-Li,用Metsu Shoryuke可以打败Ken。从n种角色中选出哪些角色在那种人物上,可以击败其余的任何角色的任何人物。求选取的最少人物。

    解:

    显然,行是代表选择,一共最多2*n行,第2*i-1行表示第i种角色选人物1,2*i行表示第i种角色选人物2。列代表约束,显然,每个角色的每种人物必须至少被覆盖一次(重复覆盖),2*n列,其次,每种角色只能选一次。增加n列,代表每种角色只能选一样,这n列必须满足每列不能多于2个1(精确覆盖)。这样,前面2*n列用重复覆盖,后面n列用精确覆盖。结束时只需判断前2*n列重复覆盖即可。

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int INF = 0x7fffffff;
const int MAX = 100;
int L[MAX*MAX],R[MAX*MAX],U[MAX*MAX],D[MAX*MAX];
int C[MAX*MAX];
int cntc[MAX];
int head;

int n,m;
int mx[MAX][MAX];
int sum[MAX];
bool vis[MAX];

struct Model
{
    int k;
    int beat[12][2];
};

struct Node
{
    int m;
    Model model[2];
};
Node node[MAX];

void EXremove(int c)
{
    int i,j;
    L[R[c]] = L[c];
    R[L[c]] = R[c];
    for(i = D[c]; i != c; i = D[i])
    {
        for(j = R[i]; j != i; j = R[j])
        {
            U[D[j]] = U[j];
            D[U[j]] = D[j];
            cntc[C[j]]--;
        }
    }
}

void EXresume(int c)
{
    int i,j;
    for(i = U[c]; i != c; i = U[i])
    {
        for(j = L[i]; j != i; j = L[j])
        {
            U[D[j]] = j;
            D[U[j]] = j;
            cntc[C[j]]++;
        }
    }
    L[R[c]] = c;
    R[L[c]] = c;
}

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

void resume(int c)
{
    int i;
    for(i = U[c]; i != c; i = U[i])
    {
        R[L[i]] = i;
        L[R[i]] = i;
    }
}

int h()
{
    int i,j,c;
    int result = 0;
    memset(vis,0,sizeof(vis));
    for(c = R[head]; c <= n && c != head; c = R[c])
    {
        if(!vis[c])
        {
            result++;
            vis[c] = true;
            for(i = D[c]; i != c; i = D[i])
            {
                for(j = R[i]; j != i; j = R[j])
                    vis[C[j]] = true;
            }
        }
    }
    return result;
}

bool dfs(int deep, int lim)
{
    int i,j;
    if(deep + h() > lim)
        return false;
    if(R[head] > n || R[head] == head)
        return true;
    int Min = INF,c;
    for(i = R[head]; i <= n && i != head; i = R[i])
    {
        if(cntc[i] < Min)
        {
            Min = cntc[i];
            c = i;
        }
    }
    for(i = D[c]; i != c; i = D[i])
    {
        remove(i);
        for(j = R[i]; j != i; j = R[j])
            if(C[j] <= n)
                remove(j);
        for(j = R[i]; j != i; j = R[j])
            if(C[j] > n)
                EXremove(C[j]);
        if(dfs(deep+1,lim))
        {
            //不用重新建图
            for(j = L[i]; j != i; j = L[j])
            if(C[j] > n)
                EXresume(C[j]);
            for(j = L[i]; j != i; j = L[j])
                if(C[j] <= n)
                    resume(j);
            resume(i);
            return true;
        }
        for(j = L[i]; j != i; j = L[j])
            if(C[j] > n)
                EXresume(C[j]);
        for(j = L[i]; j != i; j = L[j])
            if(C[j] <= n)
                resume(j);
        resume(i);
    }
    return false;
}

void build()
{
    int i,j;
    memset(cntc,0,sizeof(cntc));
    head = 0;
    for(i = 0; i < m; i++)
    {
        R[i] = i+1;
        L[i+1] = i;
    }
    R[m] = 0;
    L[0] = m;
    int first,pre,now;
    //列链表
    for(j = 1; j <= m; j++)
    {
        pre = j;
        for(i = 1; i <= n; i++)
        {
            if(mx[i][j])
            {
                cntc[j]++;
                now = i*m+j;
                C[now] = j;
                U[now] = pre;
                D[pre] = now;
                pre = now;
            }
        }
        D[pre] = j;
        U[j] = pre;
    }
    //行链表
    for(i = 1; i <= n; i++)
    {
        pre = first = -1;
        for(j = 1; j <= m; j++)
        {
            if(mx[i][j])
            {
                now = i*m+j;
                if(pre == -1)
                    first = now;
                else
                {
                    R[pre] = now;
                    L[now] = pre;
                }
                pre = now;
            }
        }
        if(first != -1)
        {
            R[pre] = first;
            L[first] = pre;
        }
    }
}

int solve(int h)
{
    int low = 1;
    int high = h;
    int ans = h;
    while(low <= high)
    {
        int mid = (low+high)>>1;
        if(dfs(0,mid))
        {
            ans = mid;
            high = mid - 1;
        }
        else
            low = mid + 1;
    }
    return ans;
}

int main()
{
    int i,j,k;
    int t,T;
    scanf("%d",&T);
    for(t = 1; t <= T; t++)
    {
        scanf("%d",&n);
        m = 0;
        memset(mx,0,sizeof(mx));
        memset(sum,0,sizeof(sum));
        for(i = 1; i <= n; i++)
        {
            sum[i] = m + 1;
            scanf("%d",&node[i].m);
            m += node[i].m;
            for(j = 0; j < node[i].m; j++)
            {
                scanf("%d",&node[i].model[j].k);
                for(k = 0; k < node[i].model[j].k; k++)
                {
                    scanf("%d %d",&node[i].model[j].beat[k][0],&node[i].model[j].beat[k][1]);
                    node[i].model[j].beat[k][0]++;
                }
            }
        }
        sum[n+1] = m+1;
        for(i = 1; i <= n; i++)
        {
            for(j = 0; j < node[i].m; j++)
            {
                for(k = sum[i]; k < sum[i+1]; k++)
                    mx[sum[i]+j][k] = 1;
                for(k = 0; k < node[i].model[j].k; k++)
                {
                    int tmpt1 = node[i].model[j].beat[k][0];
                    int tmpt2 = node[i].model[j].beat[k][1];
                    mx[sum[i]+j][sum[tmpt1]+tmpt2] = 1;
                }
                mx[sum[i]+j][m+i] = 1;
            }
        }
        int tmpt = n;
        n = m;          //行
        m += tmpt;      //列 前面mi重复覆盖,后面n精确覆盖
        build();
        printf("Case %d: %d\n",t,solve(tmpt));
    }
    return 0;
}

 

 

 

你可能感兴趣的:(HDU3957 Street Fighter)