bzoj 1741: [Usaco2005 nov]Asteroids 穿越小行星群【最大点覆盖】

二分图最大点覆盖模型,因为对于一个点(x,y)显然只要选x或者y就好了,于是连边,跑最大匹配=最大点覆盖(不会证)

#include<iostream>
#include<cstdio>
using namespace std;
const int N=10005,inf=1e9;
int n,m,h[N],cnt,con,lk[N],v[N],ti;
struct qwe
{
    int ne,to;
}e[100005];
int read()
{
    int r=0,f=1;
    char p=getchar();
    while(p>'9'||p<'0')
    {
        if(p=='-')
            f=-1;
        p=getchar();
    }
    while(p>='0'&&p<='9')
    {
        r=r*10+p-48;
        p=getchar();
    }
    return r*f;
}
void add(int u,int v)
{
    cnt++;
    e[cnt].ne=h[u];
    e[cnt].to=v;
    h[u]=cnt;
}
bool dfs(int u)
{
    for(int i=h[u];i;i=e[i].ne)
        if(v[e[i].to]!=ti)
        {
            v[e[i].to]=ti;
            if(!lk[e[i].to]||dfs(lk[e[i].to]))
            {
                lk[e[i].to]=u;
                return 1;
            }
        }
    return 0;
}
int main()
{
    n=read(),m=read();
    for(int i=1;i<=m;i++)
    {
        int x=read(),y=read();
        add(x,y+n);
    }
    for(int i=1;i<=n;i++)
    {
        ti++;
        if(dfs(i))
            con++;
    }
    printf("%d\n",con);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/lokiii/p/9010703.html