hdu 1054 Strategic Game

http://acm.hdu.edu.cn/showproblem.php?pid=1054

题意:给一棵树,选择最小的点数,覆盖整个图。

思路:最小点覆盖=最大二分匹配。

View Code
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<cmath>
#include<bitset>
#include<string>
#include<climits>
#include<cstdio>
#include<vector>
#include<utility>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define IN puts("in")
#define OUT puts("out")
#define FR(x) freopen(x,"r",stdin)
#define FW(x) freopen(x,"w",stdout)
#define MSET(x,y) memset(x,y,sizeof(x))
#define ST system("pause")
#define lowbit(x) (x)&(-x)
#define L(x) (x)<<1
#define R(x) ((x)<<1)^1
using namespace std;
const int maxn = 1505;
struct nd{
        int v,next;
}edge[maxn*2];
int head[maxn],vis[maxn],to[maxn];
int ecnt,n;
void add(int u,int v)
{
        edge[ecnt].v = v;
        edge[ecnt].next = head[u];
        head[u] = ecnt++;
}
void readin()
{
        MSET(head,-1);
        ecnt = 0;
        for(int i = 0; i < n; ++ i)
        {
                int k,u,v;
                scanf("%d:(%d)",&u,&k);
                while(k--){
                        scanf("%d",&v);
                        add(u,v); add(v,u);
                }
        }
}
int dfs(int u)
{
        int i,v;
        for(i = head[u]; i != -1; i = edge[i].next)
        {
                v = edge[i].v;
                if(!vis[v]){
                        vis[v] = 1;
                        if(to[v]==-1||dfs(to[v])){
                                to[v] = u;
                                return 1;
                        }
                }
        }return 0;
}
void processing()
{
        int i,k = 0;
        MSET(to,-1);
        for(i = 0; i < n; ++ i)
        {
              MSET(vis,0);
              k += dfs(i);
        }
        printf("%d\n",k/2);
}

int main()
{
        while(scanf("%d",&n)==1)
        {
                readin();
                processing();
        }
        return 0;
}

posted on 2012-07-25 10:36  aigoruan  阅读(148)  评论(0)    收藏  举报

导航