HDU 1083

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1083


二分图的最大匹配。。直接用套用匈牙利算法即可。


下面是AC代码:

#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 110;
bool g[3*maxn][3*maxn];
int match[3*maxn];
bool vis[3*maxn];
int p,n;
bool dfs(int cur){
     for(int i=1;i<=n;i++){
          if(g[cur][i]==true&&!vis[i]){
                vis[i]=true;
                int t=match[i];
                if(t==-1||dfs(t)){
                   match[i]=cur;
                   return true;
                }
          }
     }
     return false;
}
int hungary(){
    int res=0;
    for(int i=1;i<=n;i++)  match[i]=-1;

    for(int i=1;i<=p;i++){
        memset(vis,false,sizeof(vis));
        if(dfs(i))  res++;
    }
    return res;
}
int main(){
    int t,num,a;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&p,&n);
        memset(g,false,sizeof(g));
        for(int i=1;i<=p;i++){
            scanf("%d",&num);
            for(int j=1;j<=num;j++){
                scanf("%d",&a);
                g[i][a]=true;
            }
        }
        if(p==hungary()){
           printf("YES\n");
        }
        else{
           printf("NO\n");
        }
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值