zoukankan      html  css  js  c++  java
  • 二分图匹配

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <set>
    #include <queue>
    #include <map>
    #include <sstream>
    #include <cstdio>
    #include <cstring>
    #include <numeric>
    #include <cmath>
    #include <iomanip>
    #include <deque>
    #include <bitset>
    #include <cassert>
    //#include <unordered_set>
    //#include <unordered_map>
    #define ll              long long
    #define pii             pair<int, int>
    #define rep(i,a,b)      for(int  i=a;i<=b;i++)
    #define dec(i,a,b)      for(int  i=a;i>=b;i--)
    #define forn(i, n)      for(int i = 0; i < int(n); i++)
    using namespace std;
    int dir[4][2] = { { 1,0 },{ 0,1 } ,{ 0,-1 },{ -1,0 } };
    const long long INF = 0x7f7f7f7f7f7f7f7f;
    const int inf = 0x3f3f3f3f;
    const double pi = acos(-1.0);
    const double eps = 1e-6;
    const int mod = 1e9 + 7;
    
    inline ll read()
    {
        ll x = 0; bool f = true; char c = getchar();
        while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
        while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
        return f ? x : -x;
    }
    inline ll gcd(ll m, ll n)
    {
        return n == 0 ? m : gcd(n, m % n);
    }
    void exgcd(ll A, ll B, ll& x, ll& y)
    {
        if (B) exgcd(B, A % B, y, x), y -= A / B * x; else x = 1, y = 0;
    }
    inline int qpow(int x, ll n) {
        int r = 1;
        while (n > 0) {
            if (n & 1) r = 1ll * r * x % mod;
            n >>= 1; x = 1ll * x * x % mod;
        }
        return r;
    }
    inline int inv(int x) {
        return qpow(x, mod - 2);
    }
    ll lcm(ll a, ll b)
    {
        return a * b / gcd(a, b);
    }
    /**********************************************************/
    const int N = 2e5 + 5;
    struct augment_path {
        vector<vector<int> > g;
        vector<int> pa;  // 匹配
        vector<int> pb;
        vector<int> vis;  // 访问
        int n, m;         // 顶点和边的数量
        int dfn;          // 时间戳记
        int res;          // 匹配数
    
        augment_path(int _n, int _m) : n(_n+1), m(_m+1) {
            assert(0 <= n && 0 <= m);
            pa = vector<int>(n, -1);
            pb = vector<int>(n, -1);
            vis = vector<int>(n);
            g.resize(n);
            res = 0;
            dfn = 0;
        }
    
        void add(int from, int to) {
            //assert(0 <= from && from < n && 0 <= to && to < m);
            g[from].push_back(to);
        }
    
        bool dfs(int v) {
            vis[v] = dfn;
            for (int u : g[v]) {
                if (pb[u] == -1) {
                    pb[u] = v;
                    pa[v] = u;
                    return true;
                }
            }
            for (int u : g[v]) {
                if (vis[pb[u]] != dfn && dfs(pb[u])) {
                    pa[v] = u;
                    pb[u] = v;
                    return true;
                }
            }
            return false;
        }
    
        int solve() {
            while (true) {
                dfn++;
                int cnt = 0;
                for (int i = 1; i < n; i++) {
                    if (pa[i] == -1 && dfs(i)) {
                        cnt++;
                    }
                }
                if (cnt == 0) {
                    break;
                }
                res += cnt;
            }
            return res;
        }
    };
    
    int main()
    {
        int nl, nr, m;
        cin >> nl >> nr >> m;
        augment_path s(nl+nr,m);
        rep(i, 1, m)
        {
            int u, v;
            cin >> u >> v;
            v += nl;
            s.add(u, v);
            s.add(v, u);
        }
        int res = s.solve();
        cout << res/2 << endl;
        return 0;
    }
  • 相关阅读:
    emulating ionic really slow even on genymotion just using the “tabs” example
    Build Your First Mobile App With Ionic 2 & Angular 2
    Build Your First Mobile App With Ionic 2 & Angular 2
    Build Your First Mobile App With Ionic 2 & Angular 2
    Build Your First Mobile App With Ionic 2 & Angular 2
    Build Your First Mobile App With Ionic 2 & Angular 2
    Build Your First Mobile App With Ionic 2 & Angular 2
    Build Your First Mobile App With Ionic 2 & Angular 2
    (OK) using-VScode_cordova_ionic_taco-cli_Genymotion
    华华华
  • 原文地址:https://www.cnblogs.com/dealer/p/13585482.html
Copyright © 2011-2022 走看看