Luogu[POI2005]KOS-Dicing

题面


二分后用网络流判定
S->人,流量为二分的mid
人->比赛,流量为1
比赛->T,流量为1
输出方案只要判断a就可以了


# include <bits/stdc++.h>
# define IL inline
# define RG register
# define Fill(a, b) memset(a, b, sizeof(a))
# define Copy(a, b) memcpy(a, b, sizeof(a))
# define ID(a, b) n * (a - 1) + b
using namespace std;
typedef long long ll;
const int _(2e4 + 10), __(2e5 + 10), INF(2147483647);

IL ll Read(){
    RG char c = getchar(); RG ll x = 0, z = 1;
    for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
    for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    return x * z;
}

int n, m, a[_], w[__], fst[_], nxt[__], to[__], cnt, S, T, lev[_], cur[_], max_flow, tmp1[_], tmp2[__], tcnt, ans[_];
queue <int> Q;

IL void Add(RG int u, RG int v, RG int f){
    w[cnt] = f; to[cnt] = v; nxt[cnt] = fst[u]; fst[u] = cnt++;
    w[cnt] = 0; to[cnt] = u; nxt[cnt] = fst[v]; fst[v] = cnt++;
}

IL int Dfs(RG int u, RG int maxf){
    if(u == T) return maxf;
    RG int ret = 0;
    for(RG int &e = cur[u]; e != -1; e = nxt[e]){
        if(lev[to[e]] != lev[u] + 1 || !w[e]) continue;
        RG int f = Dfs(to[e], min(w[e], maxf - ret));
        ret += f; w[e ^ 1] += f; w[e] -= f;
        if(ret == maxf) break;
    }
    if(!ret) lev[u] = 0;
    return ret;
}

IL bool Bfs(){
    Fill(lev, 0); lev[S] = 1; Q.push(S);
    while(!Q.empty()){
        RG int u = Q.front(); Q.pop();
        for(RG int e = fst[u]; e != -1; e = nxt[e]){
            if(lev[to[e]] || !w[e]) continue;
            lev[to[e]] = lev[u] + 1;
            Q.push(to[e]);
        }
    }
    return lev[T];
}

IL void Getans(){
    for(RG int i = 1; i <= m; i++)
        for(RG int e = fst[i + n]; e != -1; e = nxt[e])
            if(to[e] && to[e] <= n && w[e]){
                if(to[e] == a[i]) ans[i] = 1;
                else ans[i] = 0;
            }
}

IL bool Check(RG int x){
    Copy(fst, tmp1); Copy(w, tmp2); cnt = tcnt;
    for(RG int i = 1; i <= n; i++) Add(S, i, x);
    for(max_flow = 0; Bfs(); ) Copy(cur, fst), max_flow += Dfs(S, INF);
    return max_flow == m;
}

int main(RG int argc, RG char* argv[]){
    Fill(fst, -1); n = Read(); m = Read(); T = n + m + 1;
    for(RG int i = 1, b; i <= m; i++){
        a[i] = Read(), b = Read();
        Add(a[i], i + n, 1); Add(b, i + n, 1); Add(i + n, T, 1);
    }
    Copy(tmp1, fst); Copy(tmp2, w); tcnt = cnt;
    RG int l = 1, r = m, num = 0;
    while(l <= r){
        RG int mid = (l + r) >> 1;
        if(Check(mid)) r = mid - 1, num = mid, Getans();
        else l = mid + 1;
    }
    printf("%d
", num);
    for(RG int i = 1; i <= m; i++) printf("%d
", ans[i]);
    return 0;
}
原文地址:https://www.cnblogs.com/cjoieryl/p/8206346.html