zoukankan      html  css  js  c++  java
  • HD1083 二分图,匈牙利算法

    #define _CRT_SECURE_NO_WARNINGS
    #include<iostream>
    #include<stdio.h>
    #include<stdlib.h>
    using namespace std;
    #define N 500
    int n1, n2, ans;
    int result[N];
    bool state[N];
    bool map[N][N];
    
    bool find(int x)
    {
        for (int i = 1; i <= n2; ++i){
            if (map[x][i] && !state[i]){
                state[i] = true;
                if (result[i] == 0 || find(result[i])){
                    result[i] = x;
                    return true;
                }
            }
        }
        return false;
    }
    int hungary()
    {
        for (int i = 1; i <= n1; ++i){
            memset(state, 0, sizeof(state));
            if (find(i)) ++ans;
        }
        return ans;
    }
    int main()
    {
        int t, p, n;
        int a, b;
        scanf("%d", &t);
        while (t--){
            memset(map, 0, sizeof(map));
            memset(result, 0, sizeof(result));
            ans = 0;
            scanf("%d %d", &p, &n);
            for (int i = 1; i <= p; ++i){
                scanf("%d", &a);
                for (int j = 1; j <= a; ++j){
                    scanf("%d", &b);
                    map[i][b] = true;
                }
            }
            n1 = p; n2 = n;
            //cout << hungary() << endl;
            if (p == hungary()) cout << "YES" << endl;
            else cout << "NO" << endl;
        }
    }
  • 相关阅读:
    二分专题
    数据结构-图
    Linux文件基本属性(以ls -l输出为例解释)
    shell脚本版素数筛
    Linux whereis,which
    Linux外网代理配置
    Linux三剑客
    Elasticsearch集群搭建(Linux)
    测试之路
    我的另一半
  • 原文地址:https://www.cnblogs.com/jokoz/p/4772698.html
Copyright © 2011-2022 走看看