HDU1054 Strategic Game 最小点覆盖

这是一道裸的最小点覆盖。。二分图中,最小点覆盖数=最大匹配数

一开始我用的邻接矩阵,结果TLE了,后来改用了邻接表就过了。。可能因为图比较稀疏吧。

附上AC代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;

const int MAX=1505;
int n,l,r;
struct edge
{
    int to,next;
}e[100*MAX];
int tol,head[MAX];
int link[MAX];
bool vis[MAX];

void init()
{
    tol=0;
    memset(head,-1,sizeof(head));
}
void addedge(int u,int v)
{
    e[tol].to=v;e[tol].next=head[u];
    head[u]=tol++;
}
bool dfs(int u)
{
    for(int i=head[u];i!=-1;i=e[i].next)
    {
        int v=e[i].to;
        if(!vis[v])
        {
            vis[v]=true;
            if(link[v]==-1||dfs(link[v]))
            {
                link[v]=u;
                return true;
            }
        }
    }
    return false;
}
int hungry()
{
    int ans=0;
    memset(link,-1,sizeof(link));
    for(int u=0;u<l;u++)
    {
        memset(vis,false,sizeof(vis));
        if(dfs(u))
            ans++;
    }
    return ans;
}
int main()
{
    int n;
    while(scanf("%d",&n)==1)
    {
        memset(e,0,sizeof(e));
        init();
        int x,num,y;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&x);
            getchar();getchar();
            scanf("%d",&num);
            getchar();getchar();
            while(num--)
            {
                scanf("%d",&y);
                addedge(x,y);
                addedge(y,x);
            }
        }
        l=r=n;
        int ans=hungry();
        printf("%d\n",ans/2);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Cc_Sonia/article/details/81946658