zoukankan      html  css  js  c++  java
  • HDU

    二分图匹配模板题

    #include <bits/stdc++.h>
    #define FOPI freopen("in.txt", "r", stdin);
    #define FOPO freopen("out.txt", "w", stdout);
    using namespace std;
    typedef long long LL;
    const int maxn = 1500 + 1000;
    int n, k, x, y;
    int link[maxn], vis[maxn];
    vector<int> v[maxn];
    
    void build(int x, int y)
    {
        v[x].push_back(y), v[y].push_back(x);
    }
    
    bool dfs(int k)
    {
        int sz = v[k].size();
        for (int i = 0; i < sz; i++)
        {
            if (!vis[v[k][i]])
            {
                vis[v[k][i]] = 1;
                if (link[v[k][i]] == -1 || dfs(link[v[k][i]]))
                {
                    link[v[k][i]] = k;
                    return true;
                }
            }
        }
        return false;
    }
    
    int hungary()
    {
        int u;
        int res = 0;
        memset(link, -1, sizeof(link));
        for (int i = 0; i < n; i++)
        {
            memset(vis, 0, sizeof(vis));
            if (dfs(i)) res++;
        }
        return res;
    }
    
    
    int main()
    {
        while(~scanf("%d", &n))
        {
            for (int i = 0; i < n; i++) v[i].clear();
            for (int i = 1; i <= n; i++)
            {
                scanf("%d:(%d)", &x, &k);
                for (int j = 1; j <= k; j++)
                    scanf("%d", &y), build(x, y), build(y, x);
            }
            printf("%d
    ", hungary()/2);
        }
    }
  • 相关阅读:
    第一课 进阶高手的大门
    Codeforces Round #363 (Div. 2) C. Vacations
    HDU 5718 Oracle
    A
    Fibonacci数的后9位
    UESTC 982质因子分解
    UESTC149 解救小Q
    UESTC93 King's Sanctuary
    HDU 4857 逃生
    L1-006. 连续因子
  • 原文地址:https://www.cnblogs.com/ruthank/p/10604211.html
Copyright © 2011-2022 走看看