QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#604968#8759. 小班课UESTC_NLNS#WA 6ms16972kbC++144.1kb2024-10-02 14:50:252024-10-02 14:50:25

Judging History

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

  • [2024-10-02 14:50:25]
  • 评测
  • 测评结果:WA
  • 用时:6ms
  • 内存:16972kb
  • [2024-10-02 14:50:25]
  • 提交

answer

#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);
    vector<vector<int>> w(m + 1);

    ans.reserve(n);

    auto get = [&](int i) {
        return g[i][cur[i]];
    };

    for (int i = 1; i <= n; ++i) {
        if (c[i] == -1) continue;
        int w1 = g[i][cur[i]];
        if (w1 == c[i]) {
            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]) {
                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] == -1) ans.push_back(i);
    }
    return ans;
}
int s, t, cnt = 1, num, ans, flow;
int head[600 * 600], nx[N * N], l[N * N], to[N * N], w[N * N], d[600 * 600], pre[600 * 600], as[N][N];
bool v[600 * 600];
void add(int x, int y, int z, int k) {
    //    cerr<<x<<"->"<<y<<" "<<z<<" "<<k<<"\n";
    nx[++cnt] = head[x];
    to[cnt] = y;
    l[cnt] = z;
    w[cnt] = k;
    head[x] = cnt;
    nx[++cnt] = head[y];
    to[cnt] = x;
    l[cnt] = 0;
    w[cnt] = -k;
    head[y] = cnt;
}
bool spfa() {
    queue<int> q;
    for (int i = 0; i <= num; ++i) d[i] = INF, v[i] = pre[i] = 0;
    q.push(s);
    d[s] = 0;
    v[s] = 1;
    int x;
    while (!q.empty()) {
        x = q.front();
        q.pop();
        v[x] = 0;
        for (int i = head[x]; i; i = nx[i])
            if (l[i] && d[to[i]] > d[x] + w[i]) {
                d[to[i]] = d[x] + w[i];
                pre[to[i]] = i;
                if (!v[to[i]]) v[to[i]] = 1, q.push(to[i]);
            }
    }
    //    cerr<<"d"<<d[t]<<"\n";
    return d[t] < INF;
}
void ek() {
    int x = t, delta = INF, sum = 0;
    while (x != s) {
        delta = min(delta, l[pre[x]]);
        sum += w[pre[x]];
        x = to[pre[x] ^ 1];
    }
    x = t;
    while (x != s) {
        l[pre[x]] -= delta;
        l[pre[x] ^ 1] += delta;
        x = to[pre[x] ^ 1];
    }
    flow += delta;
    ans += sum * delta;
}

void Clear() {
    cnt = 1, ans = 0, flow = 0;
    memset(head, 0, sizeof(head));
    s = n + m + 1, t = n + m + 2;
    num = t;
}

void solve() {
    cin >> n >> m;
    Clear();
    for (int i = 1; i <= n; ++i) g[i].clear();
    for (int i = 1; i <= m; ++i) cin >> b[i], add(n + i, t, b[i], 0);
    for (int i = 1; i <= n; ++i) {
        add(s, i, 1, 0);
        int k;
        cin >> k;
        g[i].reserve(k);
        int la = 0;
        for (int j = 0, tmp; j < k; ++j) {
            cin >> tmp;
            g[i].push_back(tmp);
            if (!la)
                add(i, ++num, 1, 0), as[i][tmp] = cnt, add(num, n + tmp, 1, 0);
            else
                add(num, num + 1, 1, 1), as[i][tmp] = cnt, add(num + 1, n + tmp, 1, 0), ++num;
            la = tmp;
        }
    }
    while (spfa()) ek();
    // cerr << flow << "\n";
    memset(c, -1, sizeof(c));
    for (int i = 1; i <= n; ++i) {
        for (int x : g[i]) {
            //            cerr<<l[as[i][x]]<<" ";
            if (l[as[i][x]]) c[i] = x;
            //            }
        }
        //        if(!b[i])b[i]=g[i][g[i].size()-1];
        // cout << b[i] << " ";
    }
    vector<int> ans = get_ans();
    cout << flow << "\n";

    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: 0ms
memory: 16972kb

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

result:

ok Correct!

Test #2:

score: -100
Wrong Answer
time: 6ms
memory: 15496kb

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 2
2 0
1 1
1 2
1 1
1
0
1 1
1
0
1 2
0 1
1 1
2 2
1 1
1 1
2 1 2
2 2
1 1
2 2 1
2 2 1
1 2
0 1
1 2
2 1
2
1 1
0
2 2
2 0
1 1
1 2
1 1
1
1 1
2 1
2
0
1 1
1 1
1
...

output:

2
1 2 
0
1 
2
1 2 
2
2 
1
1 
0
1 
0
1 
1
1 2 
0
1 
2
1 2 
1
1 
0
1 
1
1 2 
0
1 
0
1 
0
1 
2
1 2 
2
2 1 
1
1 
1
1 2 
1
1 2 
1
1 
1
2 1 
1
1 
1
1 
0
1 2 
1
1 
1
1 
0
1 
1
1 
2
1 2 
0
1 
0
1 
1
1 2 
2
2 
0
1 
0
1 
0
1 
0
1 2 
2
1 2 
1
1 
1
1 
0
1 
0
1 
0
1 
1

1
1 
0
1 
2
1 2 
2
1 2 
1
1 
1
1 
0
1 
0
1...

result:

wrong answer Integer 0 violates the range [1, 1]