QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#605142#8759. 小班课UESTC_NLNSTL 133ms4968kbC++145.3kb2024-10-02 15:47:432024-10-02 15:47:44

Judging History

你现在查看的是最新测评结果

  • [2024-10-02 15:47:44]
  • 评测
  • 测评结果:TL
  • 用时:133ms
  • 内存:4968kb
  • [2024-10-02 15:47:43]
  • 提交

answer

#include <limits>
#include <queue>
#include <vector>
using i64 = long long;
using ll = long long;

struct MCFGraph {
    struct Edge {
        int v, c;
        ll f;
        Edge(int v, int c, ll f) : v(v), c(c), f(f) {}
    };
    const int n;
    std::vector<Edge> e;
    std::vector<std::vector<int>> g;
    std::vector<i64> h, dis;
    std::vector<int> pre;
    bool dijkstra(int s, int t) {
        dis.assign(n, std::numeric_limits<i64>::max());
        pre.assign(n, -1);
        std::priority_queue<std::pair<i64, int>, std::vector<std::pair<i64, int>>, std::greater<std::pair<i64, int>>> que;
        dis[s] = 0;
        que.emplace(0, s);
        while (!que.empty()) {
            i64 d = que.top().first;
            int u = que.top().second;
            que.pop();
            if (dis[u] < d) continue;
            for (int i : g[u]) {
                int v = e[i].v;
                int c = e[i].c;
                ll f = e[i].f;
                if (c > 0 && dis[v] > d + h[u] - h[v] + f) {
                    dis[v] = d + h[u] - h[v] + f;
                    pre[v] = i;
                    que.emplace(dis[v], v);
                }
            }
        }
        return dis[t] != std::numeric_limits<i64>::max();
    }
    MCFGraph(int n) : n(n), g(n) {}
    int add(int u, int v, int c, ll f) { // 最大流
        g[u].push_back(e.size());
        int t = e.size();
        e.emplace_back(v, c, f);
        g[v].push_back(e.size());
        e.emplace_back(u, 0, -f);
        return t;
    }

    // void addEdge(int u, int v, int c, int f) {
    //     if (f < 0) {
    //         g[u].push_back(e.size());
    //         e.emplace_back(v, 0, f);
    //         g[v].push_back(e.size());
    //         e.emplace_back(u, c, -f);
    //     } else {
    //         g[u].push_back(e.size());
    //         e.emplace_back(v, c, f);
    //         g[v].push_back(e.size());
    //         e.emplace_back(u, 0, -f);
    //     }
    // }
    std::pair<int, i64> flow(int s, int t) {
        int flow = 0;
        i64 cost = 0;
        h.assign(n, 0);
        while (dijkstra(s, t)) {
            for (int i = 0; i < n; ++i) h[i] += dis[i];
            int aug = std::numeric_limits<int>::max();
            for (int i = t; i != s; i = e[pre[i] ^ 1].v) aug = std::min(aug, e[pre[i]].c);
            for (int i = t; i != s; i = e[pre[i] ^ 1].v) {
                e[pre[i]].c -= aug;
                e[pre[i] ^ 1].c += aug;
            }
            flow += aug;
            cost += i64(aug) * h[t];
        }
        return std::make_pair(flow, cost);
    }
};

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f

using namespace std;
const int N = 1505;
vector<int> g[N];
int c[N];
int b[N];
int n, m;

vector<int> get_ans() {
    vector<int> b1(m + 1);
    for (int i = 1; i <= m; ++i) b1[i] = b[i];
    queue<int> q;
    vector<int> ans;
    vector<int> cur(n + 1, 0);
    vector<vector<int>> w(m + 1);

    ans.reserve(n);

    auto get = [&](int i) {
        return g[i][cur[i]];
    };
    for (int i = 1; i <= m; ++i) {
        if (b1[i] == 0) q.push(i);
    }
    for (int i = 1; i <= n; ++i) {
        if (c[i] == 0) continue;
        int w1 = g[i][cur[i]];
        if (w1 == c[i]) {
            // assert(b1[w1] > 0);
            b1[w1]--;
            if (b1[w1] == 0) q.push(w1);
            ans.push_back(i);
        } else {
            w[w1].push_back(i);
        }
    }

    while (q.size()) {
        int f = q.front();
        q.pop();
        for (const auto u : w[f]) {
            while (b1[get(u)] == 0) {
                cur[u]++;
            }
            int w1 = get(u);
            if (w1 == c[u]) {
                // assert(b1[w1] > 0);
                b1[w1]--;
                if (b1[w1] == 0) q.push(w1);
                ans.push_back(u);
            } else {
                w[w1].push_back(u);
            }
        }
        w[f].clear();
    }

    for (int i = 1; i <= n; ++i) {
        if (c[i] == 0) ans.push_back(i);
    }
    return ans;
}

int s, t;

void INIT() {
    s = n + m + 1, t = n + m + 2;
}

void solve() {
    cin >> n >> m;
    INIT();
    vector<vector<int>> as(n + 1, vector<int>(m + 1));
    MCFGraph mcfg(t + 2);
    for (int i = 1; i <= n; ++i) g[i].clear();
    for (int i = 1; i <= m; ++i) cin >> b[i], mcfg.add(n + i, t, b[i], 0);
    for (int i = 1; i <= n; ++i) {
        mcfg.add(s, i, 1, 0);
        int k;
        cin >> k;
        g[i].reserve(k);
        int la = 0;
        for (int j = 1, tmp; j <= k; ++j) {
            cin >> tmp;
            g[i].push_back(tmp);
            // mcfg.add(i, n + tmp, 1, j);
            as[i][tmp] = mcfg.add(i, n + tmp, 1, j);
        }
    }

    memset(c, 0, sizeof(c));
    auto [flow, _] = mcfg.flow(s, t);
    for (int i = 1; i <= n; ++i) {
        for (int x : g[i]) {
            if (mcfg.e[as[i][x]].c == 0) c[i] = x;
        }
    }
    cout << flow << '\n';
    vector<int> ans = get_ans();
    for (auto u : ans) {
        cout << u << " ";
    }
    cout << '\n';
    // puts("");
}

int main() {
    cin.tie(0), cout.tie(0), ios::sync_with_stdio(0);

    int t;
    cin >> t;
    while (t--) solve();
}
/*
1
5 4
1 1 1 1

1 1
3 3 1 2
1 1
2 1 3
4 1 2 3 4
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3916kb

input:

3
5 5
1 1 1 1 1
4 1 3 2 4
1 5
4 3 4 2 1
2 3 5
1 1
5 3
1 2 2
2 1 2
2 1 2
2 1 3
2 1 3
2 1 3
5 5
1 1 1 1 1
2 1 2
2 5 4
2 3 2
2 4 3
2 5 1

output:

5
2 4 5 3 1 
5
3 1 2 4 5 
5
2 3 4 5 1 

result:

ok Correct!

Test #2:

score: 0
Accepted
time: 1ms
memory: 3612kb

input:

250
2 1
2
1 1
1 1
1 1
1
0
2 2
1 1
1 1
2 2 1
2 2
0 2
2 1 2
1 2
1 1
1
1 1
1 2
1 0
0
1 2
1 0
0
2 1
2
1 1
0
1 2
1 0
0
2 1
2
1 1
1 1
1 1
1
1 1
1 2
1 0
1 2
2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4 3
0
5 5
1 0 1 1 2
0
1 4
1 3
1 3
0
5 5
1 2 1 1 0
1 ...

output:

3
2 4 3 1 5 
1
1 2 3 4 5 
2
1 5 2 3 4 
3
1 3 5 2 4 
2
1 3 2 4 5 
4
2 5 4 1 3 
3
1 4 3 2 5 
2
2 3 1 4 5 
1
1 2 3 4 5 
4
1 2 3 4 5 
2
2 3 1 4 5 
2
1 4 2 3 5 
3
2 3 5 1 4 
3
3 4 1 2 5 
3
1 2 4 3 5 
3
1 3 2 4 5 
2
1 3 2 4 5 
3
1 4 5 2 3 
1
1 2 3 4 5 
3
3 5 2 1 4 
1
2 1 3 4 5 
2
2 3 1 4 5 
2
1 4 2 3 5 
2...

result:

ok Correct!

Test #6:

score: 0
Accepted
time: 5ms
memory: 3752kb

input:

10
45 47
3 0 2 0 1 1 1 0 2 0 1 0 0 3 0 0 0 4 0 1 0 0 1 2 1 1 1 0 1 1 1 0 0 0 0 1 0 0 0 1 2 4 1 2 1 2 3
7 1 37 21 3 13 43 22
0
10 23 46 22 40 12 19 47 27 16 42
4 29 19 45 35
10 6 26 2 43 41 7 9 16 42 44
5 39 40 34 46 14
3 34 3 38
8 10 5 38 23 19 37 9 34
0
5 31 29 15 13 35
3 40 4 28
1 7
6 29 12 9 35 2...

output:

33
1 11 12 14 15 19 29 37 38 39 42 31 16 8 30 17 40 43 7 21 6 35 5 10 25 3 44 36 4 13 24 18 34 2 9 20 22 23 26 27 28 32 33 41 45 
39
10 12 14 15 16 20 30 32 43 44 25 45 2 28 29 36 3 35 19 7 17 40 31 33 38 24 18 9 21 42 11 34 5 26 1 41 6 23 39 4 8 13 22 27 37 
36
3 4 10 20 28 29 31 32 33 46 47 16 36 ...

result:

ok Correct!

Test #7:

score: 0
Accepted
time: 133ms
memory: 4968kb

input:

1
499 497
1 2 0 2 0 1 0 0 0 2 1 2 0 3 1 2 0 0 0 1 0 1 0 2 1 0 1 0 1 1 1 2 0 1 0 1 0 2 2 3 1 1 2 1 0 0 1 0 2 3 0 1 0 0 2 0 1 2 1 0 0 1 2 0 0 2 0 2 0 1 0 1 0 0 1 0 0 1 1 1 1 1 0 0 0 1 2 3 0 0 0 4 2 2 1 2 2 0 1 0 1 0 2 0 1 0 2 0 0 1 1 1 3 2 0 2 2 2 0 1 1 1 1 1 0 1 0 1 1 1 1 1 2 0 0 1 0 2 1 2 1 2 1 0 1 ...

output:

482
3 10 17 18 22 30 31 33 34 35 38 40 41 55 56 59 60 63 69 70 73 80 81 82 86 89 91 95 96 104 106 108 110 111 113 118 119 120 124 125 126 134 138 139 142 144 149 150 153 157 162 167 171 172 174 179 182 187 190 199 204 208 214 215 217 224 227 228 231 232 234 237 239 241 242 243 244 245 250 252 253 26...

result:

ok Correct!

Test #8:

score: -100
Time Limit Exceeded

input:

1
498 499
0 1 1 0 1 0 1 0 0 0 0 2 0 3 1 2 4 0 1 0 1 1 0 0 0 1 1 0 0 2 2 0 1 1 1 0 4 1 1 2 1 0 0 1 2 0 1 2 1 0 1 2 0 2 1 2 2 0 2 2 0 1 0 2 0 0 3 0 1 1 1 1 0 1 0 1 0 0 1 1 0 1 1 0 0 1 0 1 2 1 1 0 1 0 1 0 0 0 1 1 2 0 1 0 2 1 1 2 2 0 0 0 0 2 0 2 1 0 1 0 2 0 1 3 1 1 1 0 1 3 0 1 0 1 0 0 1 3 2 3 2 1 1 0 2 ...

output:

498
1 2 3 5 7 8 15 16 19 26 28 35 36 37 39 45 46 47 50 52 53 56 57 66 68 69 72 73 78 79 82 83 85 88 96 98 108 110 111 112 113 114 116 117 123 124 126 127 129 133 134 135 136 137 140 142 146 147 150 155 157 160 161 170 174 176 177 180 184 185 188 189 191 198 199 200 204 207 209 211 214 215 216 221 22...

result: