zoukankan      html  css  js  c++  java
  • 【hihocoder 1122】二分图二•二分图最大匹配之匈牙利算法

    Link:https://hihocoder.com/problemset/problem/1122

    Description

    Solution

    二分图匹配,匈牙利算法模板题;
    这里我先把染成0的放在一个vector里面,然后再进行匈牙利算法.

    NumberOf WA


    Reviw


    Code

    #include <bits/stdc++.h>
    using namespace std;
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define LL long long
    #define rep1(i,a,b) for (int i = a;i <= b;i++)
    #define rep2(i,a,b) for (int i = a;i >= b;i--)
    #define mp make_pair
    #define pb push_back
    #define fi first
    #define se second
    #define ms(x,y) memset(x,y,sizeof x)
    #define ri(x) scanf("%d",&x)
    #define rl(x) scanf("%lld",&x)
    #define rs(x) scanf("%s",x+1)
    #define oi(x) printf("%d",x)
    #define ol(x) printf("%lld",x)
    #define oc putchar(' ')
    #define os(x) printf(x)
    #define all(x) x.begin(),x.end()
    #define Open() freopen("F:\rush.txt","r",stdin)
    #define Close() ios::sync_with_stdio(0)
    
    typedef pair<int,int> pii;
    typedef pair<LL,LL> pll;
    
    const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
    const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
    const double pi = acos(-1.0);
    const int N = 1000;
    
    vector <int> g[N+100],v[2];
    int pre[N+10],Try[N+10],color[N+10];
    int n,m;
    
    bool hungary(int x){
        int len = g[x].size();
        for (int i = 0;i <= len-1;i++){
            int y = g[x][i];
            if (!Try[y]){
                Try[y] = 1;
                if ( pre[y] == -1 || hungary(pre[y])){
                    pre[y] = x;
                    return 1;
                }
            }
        }
        return 0;
    }
    
    void dfs(int x,int c){
        color[x] = c;v[c].pb(x);
        int len = g[x].size();
        rep1(i,0,len-1){
            int y = g[x][i];
            if (color[y]==-1) dfs(y,1-c);
        }
    }
    
    int main(){
        //Open();
        //Close();
        ri(n),ri(m);
        rep1(i,1,m){
            int x,y;
            ri(x),ri(y);
            g[x].pb(y),g[y].pb(x);
        }
    
        ms(color,255);
        rep1(i,1,n)
            if (color[i] == -1)
                dfs(i,0);
    
        ms(pre,255);
        int ans = 0,len = v[0].size();
        rep1(i,0,len-1){
            ms(Try,0);
            if (hungary(v[0][i])) ans++;
        }
    
        oi(ans);puts("");
        return 0;
    }
    
  • 相关阅读:
    BZOJ
    Codeforces
    GYM
    UOJ
    Java集合之Queue
    【HIHOCODER 1478】 水陆距离(BFS)
    Java集合之Stack
    Java集合之Vector
    Java多线程入门Ⅱ
    【HIHOCODER 1604】股票价格II(堆)
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626127.html
Copyright © 2011-2022 走看看