QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#427697#8759. 小班课fairyqq28WA 1ms4024kbC++143.6kb2024-06-01 15:03:092024-06-01 15:03:09

Judging History

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

  • [2024-06-01 15:03:09]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:4024kb
  • [2024-06-01 15:03:09]
  • 提交

answer

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
#include<cassert>
#define rep(i, a, b) for(int i = (a); i <= (b); i++)
using namespace std;
const int N = 510, M = N+N+N*N;

namespace FLOW{
    int dis[N], s, t, cur[N], h[N], cnt;
    bool v[N];
    struct edge {int to, nxt, val, cst;} e[M<<1];
    queue<int> q;
    bool spfa(){
        // printf("enter spfa.\n");
        rep(i, 1, t) dis[i] = 0x3f3f3f3f, v[i] = 0;
        q.push(s); dis[s] = 0, v[s] = 1;
        while(!q.empty()){
            int x = q.front(); q.pop(); v[x] = 0;
            // printf("spfa: %d %d\n", x, dis[x]);
            cur[x] = h[x];
            for(int y = h[x]; y; y = e[y].nxt){
                int z = e[y].to, w = e[y].val, d = e[y].cst;
                // printf("spfa: %d->%d (%d %d)\n", x, z, w, d);
                if(w && dis[x] + d < dis[z]){
                    dis[z] = dis[x] + d;
                    if(!v[z]) q.push(z), v[z] = 1;
                }
            }
        }
        // printf("end spfa. (ret = %d)\n", (dis[t] < 0x3f3f3f3f));
        // rep(i, 1, t) printf("%d ", dis[i]);
        // puts("");
        return dis[t] < 0x3f3f3f3f;
    }
    int dfs(int x, int flow){
        // printf("dfs: %d %d\n", x, flow);
        if(x == t) return flow;
        v[x] = 1; int ret = 0;
        for(int &y = cur[x]; y && flow; y = e[y].nxt){
            int z = e[y].to, w = e[y].val, d = e[y].cst;
            if(!v[z] && (dis[z] == dis[x]+d) && w){
                int tmp = dfs(z, min(flow, w));
                if(tmp){
                    ret += tmp, flow -= tmp;
                    e[y].val -= tmp, e[y ^ 1].val += tmp;
                }
                else dis[z] = 0x3f3f3f3f;
            }
        }
        v[x] = 0; return ret;
    }
    int dinic(){
        int ret = 0;
        while(spfa()) ret += dfs(s, 0x3f3f3f3f);
        return ret;
    }
    void init(int S, int T){
        rep(i, 1, t) h[i] = 0;
        cnt = 1, s = S, t = T;
    }
    void add_edge(int x, int y, int w, int d){
        // printf("add_edge: %d %d %d %d\n", x, y, w, d);
        e[++cnt] = edge{y, h[x], w, d}; h[x] = cnt;
        e[++cnt] = edge{x, h[y], 0, -d}; h[y] = cnt;
    }
}

int n, m, cnt, ans[N];
vector<int> a[N], e[N];
bool v[N];

void solve_ans(int x){
    if(v[x]) return;
    v[x] = 1;
    for(int &y : e[x]) for(int &i : a[y]){
        if(i == x) break;
        solve_ans(i);
    }
    for(int &y : e[x]) ans[++cnt] = y;
}

void solve(){
    scanf("%d%d", &n, &m);
    FLOW::init(n+m+1, n+m+2);
    rep(i, 1, n) FLOW::add_edge(FLOW::s, i, 1, 0);
    rep(i, 1, m){
        int x; scanf("%d", &x);
        FLOW::add_edge(i+n, FLOW::t, x, 0);
    }
    rep(i, 1, n){
        a[i].clear();
        int k; scanf("%d", &k);
        rep(j, 1, k){
            int x; scanf("%d", &x);
            a[i].push_back(x);
            FLOW::add_edge(i, x+n, 1, j);
        }
    }
    printf("%d\n", FLOW::dinic());
    rep(i, 1, m) e[i].clear(), v[i] = 0;
    rep(i, 1, n){
        bool flag = 0;
        for(int y = FLOW::h[i]; y; y = FLOW::e[y].nxt)
            if(FLOW::e[y].to > n) if(!FLOW::e[y].val){
                e[FLOW::e[y].to-n].push_back(i);
                flag = 1; break;
            }
        if(!flag) e[m+1].push_back(i);
    }
    cnt = 0;
    // printf("main: ");
    // rep(i, 1, m+1) printf("%d ", (int)e[i].size());
    // puts("");
    rep(i, 1, m+1) solve_ans(i);
    // assert(cnt == n);
    rep(i, 1, cnt) printf("%d ", ans[i]);
    puts("");
}

int main(){
    int T; scanf("%d", &T); while(T--) solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 4024kb

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

result:

ok Correct!

Test #2:

score: -100
Wrong Answer
time: 1ms
memory: 3792kb

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

2
1 2 
2
1 2 
1
1 
0

0

1
1 
0

2
1 2 
1
1 
0

1
1 
0

0

0

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

1
1 
1
1 
0

1
1 
2
2 1 
0

0

1
1 
2
1 2 
0

0

0

0

2
1 2 
1
1 
1
1 
0

0

0

1
1 
1
1 
0

2
1 2 
2
1 2 
1
2 
1
1 
0

0

2
1 2 
1
1 
1
1 
0

0

1
1 
2
1 2 
0

2
2 1 
0

2
2 ...

result:

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