zoukankan      html  css  js  c++  java
  • poj1469

    二分图匹配,匈牙利算法

    View Code
    #include <iostream>
    #include
    <cstdio>
    #include
    <cstdlib>
    #include
    <cstring>
    using namespace std;

    #define maxn 505

    int uN, vN;
    bool g[maxn][maxn];
    int xM[maxn], yM[maxn];
    bool chk[maxn];
    int n, p;

    void input()
    {
    int a, m;
    memset(g,
    0, sizeof(g));
    for (int i = 0; i < p; i++)
    {
    scanf(
    "%d", &m);
    for (int j = 0; j < m; j++)
    {
    scanf(
    "%d", &a);
    a
    --;
    g[i][a]
    = true;
    }
    }
    }

    bool SearchPath(int u)
    {
    int v;
    for (v = 0; v < vN; v++)
    if (g[u][v] && !chk[v])
    {
    chk[v]
    = true;
    if (yM[v] == -1 || SearchPath(yM[v]))
    {
    yM[v]
    = u;
    xM[u]
    = v;
    return true;
    }
    }
    return false;
    }

    int MaxMatch()
    {
    int u, ret = 0;
    memset(xM,
    -1, sizeof(xM));
    memset(yM,
    -1, sizeof(yM));
    for (u = 0; u < uN; u++)
    if (xM[u] == -1)
    {
    memset(chk,
    false, sizeof(chk));
    if (SearchPath(u))
    ret
    ++;
    }
    return ret;
    }

    int main()
    {
    //freopen("t.txt", "r", stdin);
    int t;
    scanf(
    "%d", &t);
    while (t--)
    {
    scanf(
    "%d%d", &p, &n);
    input();
    uN
    = p;
    vN
    = n;
    int ans = MaxMatch();
    if (ans == p)
    printf(
    "YES\n");
    else
    printf(
    "NO\n");
    }
    return 0;
    }

  • 相关阅读:
    Luogu P4727 [HNOI2009]图的同构记数
    ARC 101 E
    JSOI2019 Round2 游记
    JSOI2019 Round1(十二省联考)游记
    Technocup 2019
    Codeforces Round #533 (Div. 2)比赛总结
    学习链接
    2018.12.29-2018.1.9安师大附中集训
    关于考试
    NOIP2018提高组 游记
  • 原文地址:https://www.cnblogs.com/rainydays/p/2085460.html
Copyright © 2011-2022 走看看