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;
        }
    }
  • 相关阅读:
    过滤textarea
    vue引用jquery
    vue_ajax插件Axios
    VeeValidate
    mongodb
    WEBGL实现--three.js笔记整理
    My SQLworkbench问题总结
    vue遇到的问题
    MYSQL使用笔记
    vue笔记
  • 原文地址:https://www.cnblogs.com/jokoz/p/4772698.html
Copyright © 2011-2022 走看看