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

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\n", si);
    for(int i = 0; i < si; ++i) printf("%d%c", a[ans[i]], " \n"[i==si-1]);
    return 0;
}
/*
2 100
3 8 5 2
2 9 3
1 2 10
*/
View Code

 

posted @ 2019-04-14 17:39  Schenker  阅读(190)  评论(0)    收藏  举报