题解 P4382 【[八省联考2018]劈配】

· · 题解

暴力即正解

首先对于每个选手连边 $(S, P_i, 1)$ ,每个导师连边 $(Q_i, T, B_i)

第一问比较显然,就是枚举每个选手被第几志愿录取,然后这个选手向对应的导师连边,看能否跑出流量即可

第二问大家都二分,然而我们可以暴力,每当跑完一个选手之后,我们对于每个导师暴力连边 (S, Q_i, 1) ,再跑网络流,如果能跑出流量,那么说明第 i 个导师的队伍没有满员,然后再把这条边删掉

那么第二问的答案就相当于是在它的所有前 s_i 志愿中找出一个最晚满员的导师,满员的时间就是就是这个人最少需要到达的名次

这样子时间复杂度是 : O(n^2m^2),我们只需要在第一问的时候,如果发现选手的志愿的队伍已经满员就不加边,这样就可以跑过去了

然而我们发现 C\leq 10,也就是每一轮最多加 10 条边,如果一个选手没有被第 i 志愿录取,那么之后第 i 志愿的边也不可能跑出流量,所以我们可以把这些边删掉,于是时间复杂度 : O(n^2mC)

#include <cctype>
#include <cstdio>
#include <cstring>
#include <algorithm>

#define DEBUG(args...) fprintf(stderr, args)

typedef long long LL;

#define FOR(i, l, r) for(int i = (l), i##_end = (r); i <= i##_end; ++i)
#define REP(i, l, r) for(int i = (l), i##_end = (r); i <  i##_end; ++i)
#define DFR(i, l, r) for(int i = (l), i##_end = (r); i >= i##_end; --i)
#define DRP(i, l, r) for(int i = (l), i##_end = (r); i >  i##_end; --i)

template<class T>T Min(const T &a, const T &b) {return a < b ? a : b;}
template<class T>T Max(const T &a, const T &b) {return a > b ? a : b;}
template<class T>bool Chkmin(T &a, const T &b) {return a > b ? a = b, 1 : 0;}
template<class T>bool Chkmax(T &a, const T &b) {return a < b ? a = b, 1 : 0;}

class fast_input {
private:
    static const int SIZE = 1 << 15 | 1;
    char buf[SIZE], *front, *back;

    void Next(char &c) {
        if(front == back) back = (front = buf) + fread(buf, 1, SIZE, stdin);
        c = front == back ? (char)EOF : *front++;
    }

public :
    template<class T>void operator () (T &x) {
        char c, f = 1;
        for(Next(c); !isdigit(c); Next(c)) if(c == '-') f = -1;
        for(x = 0; isdigit(c); Next(c)) x = x * 10 + c - '0';
        x *= f;
    }
    void operator () (char &c, char l = 'a', char r = 'z') {
        for(Next(c); c > r || c < l; Next(c)) ;
    }
}input;

const int SN = 200 + 47;
const int SP = SN << 1 | 1;
const int SE = SN * SN * 4 + 1;
const int S = SP - 1;
const int T = SP - 2;
const int INF = 0x3f3f3f3f;

int cur[SP], head[SP], nxt[SE], to[SE], flow[SE], ce = 1;
int que[SP], front, back, deep[SP], pre[SP];
int a[SN][SN], b[SN], n, m, s[SN];
int ans1[SN], ans2[SN];
int team_full[SN], team_test_edge[SN];

void Clear();
void Add(int, int, int);
bool BFS();
void Doit();

int main() {

#ifdef Cai
    freopen("s.in", "r", stdin);
#endif

    int x, y, z, cases;

    input(cases), input(x);

    while(cases--) {
        Clear();
        input(n), input(m);
        FOR(i, 1, m) input(b[i]);
        FOR(i, 1, n) FOR(j, 1, m) input(a[i][j]);
        FOR(i, 1, n) input(s[i]);

        FOR(i, 1, m) Add(i + n, T, b[i]);
        FOR(i, 1, m) team_test_edge[i] = ce + 1, Add(S, i + n, 0);

        FOR(i, 1, n) {
            Add(S, i, 1), ans1[i] = m + 1;
            FOR(j, 1, m) {
                x = ce;
                memcpy(cur, head, sizeof head);
                FOR(k, 1, n) if(a[i][k] == j && !team_full[k]) Add(i, k + n, 1);
                if(BFS()) {ans1[i] = j; Doit(); break;}
                memcpy(head, cur, sizeof head);
            }
            FOR(j, 1, m) if(!team_full[j]) {
                flow[team_test_edge[j]] = 1;
                if(!BFS()) team_full[j] = i;
                flow[team_test_edge[j]] = 0;
            }
        }

        FOR(i, 1, n) if(!team_full[i]) team_full[i] = n + 1;

        FOR(i, 1, n) {
            x = 0;
            FOR(j, 1, m) if(a[i][j] && a[i][j] <= s[i]) Chkmax(x, team_full[j]);
            if(!x) ans2[i] = i;
            else if(x >= i) ans2[i] = 0;
            else ans2[i] = i - x;
        }

        FOR(i, 1, n) printf("%d ", ans1[i]); putchar(10);
        FOR(i, 1, n) printf("%d ", ans2[i]); putchar(10);
    }

    return 0;

}

void Clear() {
    memset(head, 0, sizeof head), ce = 1;
    memset(team_full, 0, sizeof team_full);
}

void Add(int x, int y, int z) {
    nxt[++ce] = head[x], head[x] = ce, to[ce] = y, flow[ce] = z;
    nxt[++ce] = head[y], head[y] = ce, to[ce] = x, flow[ce] = 0;
}

bool BFS() {
    memset(deep, 0, sizeof deep), deep[S] = 1;
    que[0] = S, front = 0, back = 1;
    while(front < back)
        for(int now, i = head[now = que[front++]]; i; i = nxt[i])
            if(!deep[to[i]] && flow[i]) {
                deep[to[i]] = deep[now] + 1;
                pre[to[i]] = i;
                if(to[i] == T) return true;
                que[back++] = to[i];
            }
    return false;
}

void Doit() {
    for(int i = T; i != S; i = to[pre[i] ^ 1])
        --flow[pre[i]], ++flow[pre[i] ^ 1];
}

/*
g++11 -o s s.cpp -O2; for((i = 1; i <= 10; ++i)) do cp $i.in s.in; time ./s > s.out; diff $i.ans s.out -w > s.res; echo $i : $?; done;
*/