zoukankan      html  css  js  c++  java
  • poj2239

    匈牙利,二分图匹配

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

    #define maxn 305

    struct Edge
    {
    int v, next;
    } edge[maxn
    * 7 * 12];

    int n, m;
    int head[maxn];
    int xM[maxn], yM[maxn];
    bool chk[maxn];
    int count;

    void addedge(int a, int b)
    {
    edge[count].next
    = head[a];
    edge[count].v
    = b;
    head[a]
    = count;
    count
    ++;
    }

    void input()
    {
    int t;

    for (int i = 0; i < n; i++)
    {
    scanf(
    "%d", &t);
    for (int j = 0; j < t; j++)
    {
    int p, q;
    scanf(
    "%d%d", &p, &q);
    addedge(i, (p
    - 1) * 12 + q);
    }
    }
    }

    bool SearchPath(int u)
    {
    for (int i = head[u]; i != -1; i = edge[i].next)
    {
    int v = edge[i].v;
    if (!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 < n; u++)
    if (xM[u] == -1)
    {
    memset(chk,
    false, sizeof(chk));
    if (SearchPath(u))
    ret
    ++;
    }
    return ret;
    }

    int main()
    {
    //freopen("t.txt", "r", stdin);
    while (scanf("%d", &n) != EOF)
    {
    count
    = 0;
    memset(head,
    -1, sizeof(head));
    m
    = 7 * 12 + 1;
    input();
    printf(
    "%d\n", MaxMatch());
    }
    return 0;
    }

  • 相关阅读:
    【Layui】11 滑块 Slider
    【Layui】10 颜色选择器 ColorPicker
    【Layui】09 动画 Anim
    【Layui】08 时间线 Timeline
    【Layui】07 徽章 Badge
    【Layui】06 面板 Panel
    【Layui】05 进度条 Progress
    【Layui】05 选项卡 Tabs
    【Layui】04 导航 Nav
    【Layui】03 按钮 Button
  • 原文地址:https://www.cnblogs.com/rainydays/p/2062145.html
Copyright © 2011-2022 走看看