POJ 1463 ||HDU 1054 Strategic game(二分图最小顶点覆盖)

思路:直接跑一个KM,最后答案/2

#include<map>
#include<queue>
#include<cmath>
#include<iostream>
#include<cstdio>
#include<stack>
#include<cstring>
#include<algorithm>
#define LL __int64
#define inf 0x3f3f3f3f
const double PI=acos(-1.0);
using namespace std;

int cro[20000],head[20000],cnt;
bool vis[20000];

struct node{
    int to,next;
}q[200000];
void bu(int k,int t){
    q[cnt].to=t;
    q[cnt].next=head[k];
    head[k]=cnt++;

    q[cnt].to=k;
    q[cnt].next=head[t];
    head[t]=cnt++;
}
bool dfs(int u){
    for(int i=head[u];~i;i=q[i].next){
        int v=q[i].to;
        if(vis[v]) continue;
        vis[v]=true;
        if(cro[v]==-1||dfs(cro[v])){
            cro[v]=u;
            return true;
        }
    }
    return false;
}
int main(){
    int i,j,n,m,k,t;
    while(~scanf("%d",&n)){
        memset(head,-1,sizeof(head));
        cnt=0;
        for(i=0;i<n;++i){
            scanf("%d:(%d)",&k,&m);
            while(m--){
                scanf("%d",&t);
                bu(k,t);
            }
        }
        int ans=0;
        memset(cro,-1,sizeof(cro));
        for(i=0;i<n;++i){
            memset(vis,false,sizeof(vis));
            if(dfs(i)) ans++;
        }
        printf("%d\n",ans/2);
    }
    return 0;
}
//2
//1:(1) 0
//0:(1) 1

你可能感兴趣的:(二分图)