• 模板汇总 ——— 匈牙利算法


    const int N = 500;
    const int M = 1e5;
    int head[N], to[M], nt[M], link[M], tot;
    int vis[N];
    int Vcnt;
    int n, m;
    void add(int u, int v){
        to[tot] = v;
        nt[tot] = head[u];
        head[u] = tot++;
    
        to[tot] = u;
        nt[tot] = head[v];
        head[v] = tot++;
    }
    bool hun(int u){
        for(int i = head[u]; ~i; i = nt[i]){
            if(vis[to[i]] != Vcnt){
                vis[to[i]] = Vcnt;
                if(-1 == link[to[i]] ||  hun(link[to[i]])){
                    link[u] = to[i];
                    link[to[i]]=u;
                    return 1;
                }
            }
        }
        return 0;
    }
    int solve(int n){
        for(int i = 1; i <= n; ++i){
            if(link[i] == -1){
                ++Vcnt;
                if(!hun(i))
                    return false;
            }
        }
        return true;
    }
    void init(){
        memset(head, -1, sizeof head);
        memset(link, -1, sizeof link);
        memset(vis, 0, sizeof vis);
        Vcnt = 0;
        tot = 0;
    }
    View Code

     模板

    P

    #include<cstdio>
    #include<algorithm>
    #include<vector>
    #include<queue>
    #include<map>
    #include<iostream>
    #include<cstring>
    using namespace std;
    #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
    #define LL long long
    #define ULL unsigned LL
    #define fi first
    #define se second
    #define pb push_back
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define lch(x) tr[x].son[0]
    #define rch(x) tr[x].son[1]
    #define max3(a,b,c) max(a,max(b,c))
    #define min3(a,b,c) min(a,min(b,c))
    typedef pair<int,int> pll;
    const int inf = 0x3f3f3f3f;
    const int _inf = 0xc0c0c0c0;
    const LL INF = 0x3f3f3f3f3f3f3f3f;
    const LL _INF = 0xc0c0c0c0c0c0c0c0;
    const LL mod =  (int)1e9+7;
    const int N = 500;
    const int M = 1e5;
    int head[N], to[M], nt[M], link[M], tot;
    int vis[N];
    int Vcnt;
    int n, m;
    void add(int u, int v){
        to[tot] = v;
        nt[tot] = head[u];
        head[u] = tot++;
    }
    bool hun(int u){
        for(int i = head[u]; ~i; i = nt[i]){
            if(vis[to[i]] != Vcnt){
                vis[to[i]] = Vcnt;
                if(-1 == link[to[i]] ||  hun(link[to[i]])){
                    link[u] = to[i];
                    link[to[i]]=u;
                    return 1;
                }
            }
        }
        return 0;
    }
    int solve(int n){
        for(int i = 1; i <= n; ++i){
            if(link[i] == -1){
                ++Vcnt;
                if(!hun(i))
                    return false;
            }
        }
        return true;
    }
    void init(){
        memset(head, -1, sizeof head);
        memset(link, -1, sizeof link);
        memset(vis, 0, sizeof vis);
        Vcnt = 0;
        tot = 0;
    }
    int main(){
        int T;
        scanf("%d", &T);
        while(T--){
            scanf("%d%d", &n, &m);
            init();
            for(int i = 1; i <= n; ++i){
                int t, v;
                scanf("%d", &t);
                while(t--){
                    scanf("%d", &v);
                    add(i, v+n);
                    add(v+n, i);
                }
            }
            if(solve(n)) puts("YES");
            else puts("NO");
        }
        return 0;
    }
    View Code

    找最大独立集

    #include<bits/stdc++.h>
    #define fi first
    #define se second
    #define lson l,mid,o<<1
    #define rson mid+1,r,o<<1|1
    #define pb push_back
    #define fio ios::sync_with_stdio(false);cin.tie(0)
    using namespace std;
    typedef long long LL;
    typedef unsigned long long uLL;
    typedef pair<int, int> P;
    typedef pair<int, P> PI;
    const LL INF = 0x3f3f3f3f;
    const LL mod = 998244353;
    const bool debug = true;
    
    const int N = 5100;
    const int M = 1e6;
    vector<int>vc[N];
    int a[N];
    int head[N], to[M], nt[M], link[M], tot;
    int vis[N];
    int Vcnt;
    int n, m;
    void add(int u, int v){
        to[tot] = v;
        nt[tot] = head[u];
        head[u] = tot++;
    
        to[tot] = u;
        nt[tot] = head[v];
        head[v] = tot++;
    }
    bool hun(int u){
        for(int i = head[u]; ~i; i = nt[i]){
            if(vis[to[i]] != Vcnt){
                vis[to[i]] = Vcnt;
                if(-1 == link[to[i]] ||  hun(link[to[i]])){
                    link[u] = to[i];
                    link[to[i]]=u;
                    return 1;
                }
            }
        }
        return 0;
    }
    int solve(int n){
        int gg = 0;
        for(int i = 1; i <= n; ++i){
            if(link[i] == -1){
                ++Vcnt;
                if(hun(i)){
                    ++gg;
                }
    
            }
        }
        return true;
    }
    vector<int> ans;
    int ok[N];
    void dfs(int u, int col){
        ok[u] = col;
        for(int i = head[u]; ~i; i = nt[i]){
            int v = to[i];
            if(ok[v]) continue;
            dfs(v, 3-col);
        }
    }
    void init(){
        memset(vis, 0, sizeof vis);
        memset(link, -1, sizeof link);
        memset(head, -1, sizeof head);
        Vcnt = 0;
        tot = 0;
    }
    int fuck[N];
    queue<int> q;
    int main()
    {
        int n;
        scanf("%d", &n);
        init();
        for(int i = 1; i <= n; ++i) scanf("%d", &a[i]);
        for(int i = 1; i <= n; ++i){
            for(int j = i+1; j <= n; ++j){
                int v = a[i] ^ a[j];
                if(v == (v & (-v))){
                    add(i, j);
                }
            }
        }
        solve(n);
        for(int i = 1; i <= n; ++i){
            if(!ok[i]) dfs(i, 1);
            if(link[i] == -1){
                q.push(i);
                fuck[i] = 1;
            }
        }
        while(!q.empty()){
            int now = q.front();
            q.pop();
            ans.pb(now);
            for(int i = head[now]; ~i; i = nt[i]){
                int v = to[i];
                if(fuck[v]) continue;
                fuck[v] = fuck[link[v]] = 1;
                q.push(link[v]);
            }
        }
        for(int i = 1; i <= n; ++i){
            if(link[i] == -1 || fuck[i]) continue;
            if(ok[i] == 1) ans.pb(i);
        }
        int si = ans.size();
        printf("%d
    ", si);
        for(int i = 0; i < si; ++i) printf("%d%c", a[ans[i]], " 
    "[i==si-1]);
        return 0;
    }
    /*
    2 100
    3 8 5 2
    2 9 3
    1 2 10
    */
    View Code
  • 相关阅读:
    论:CMMI项目监督和控制方法(PMC)
    开发silverlight下的xps浏览器,支持xps printer输出格式
    论:CMMI风险管理方法(RSKM)
    论:CMMI项目集成管理(IPM)
    Visual studio 2010 sp1中文版正式版无法安装Silverlight5_Tools rc1 的解决办法
    混乱的Comcast
    我的幸福呢
    Windows Phone 7芒果更新
    WP7升级
    来个GPS数字统计
  • 原文地址:https://www.cnblogs.com/MingSD/p/10706065.html
Copyright © 2020-2023  润新知