zoukankan      html  css  js  c++  java
  • hdu1054(最小顶点覆盖)

    传送门:Strategic Game

    题意:用尽量少的顶点来覆盖所有的边。

    分析:最小顶点覆盖裸题,最小顶点覆盖=最大匹配数(双向图)/2.

    #include <cstdio>
    #include <cstring>
    #include <string>
    #include <cmath>
    #include <iostream>
    #include <algorithm>
    #include <queue>
    #include <cstdlib>
    #include <stack>
    #include <vector>
    #include <set>
    #include <map>
    #define LL long long
    #define mod 100000000
    #define inf 0x3f3f3f3f
    #define eps 1e-6
    #define N 1510
    #define FILL(a,b) (memset(a,b,sizeof(a)))
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define PII pair<int,int>
    using namespace std;
    int match[N],vis[N],n,m;
    vector<int>g[N];
    int dfs(int u)
    {
        for(int i=0,sz=g[u].size();i<sz;i++)
        {
            int v=g[u][i];
            if(!vis[v])
            {
                vis[v]=1;
                if(match[v]==-1||dfs(match[v]))
                {
                    match[v]=u;
                    return 1;
                }
            }
        }
        return 0;
    }
    int hungary()
    {
        FILL(match,-1);
        int ans=0;
        for(int i=0;i<n;i++)
        {
            FILL(vis,0);
            if(dfs(i))ans++;
        }
        return ans;
    }
    
    int main()
    {
        while(scanf("%d",&n)>0)
        {
            for(int i=0;i<=n;i++)
                g[i].clear();
            for(int i=1;i<=n;i++)
            {
                int u,v,num;
                scanf("%d:(%d)",&u,&num);
                while(num--)
                {
                    scanf("%d",&v);
                    g[u].push_back(v);
                    g[v].push_back(u);
                }
            }
            int res=hungary();
            printf("%d
    ",res/2);
        }
    }
    View Code
  • 相关阅读:
    @雅礼集训01/13
    @hdu
    @bzoj
    @hdu
    @bzoj
    @雅礼集训01/10
    @codeforces
    @spoj
    @bzoj
    @bzoj
  • 原文地址:https://www.cnblogs.com/lienus/p/4287058.html
Copyright © 2011-2022 走看看