QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#604830 | #8759. 小班课 | frs | WA | 0ms | 3616kb | C++14 | 2.6kb | 2024-10-02 14:07:34 | 2024-10-02 14:07:36 |
Judging History
answer
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
#include <valarray>
#include <cmath>
#include <numeric>
#include <random>
#include <set>
#include <queue>
using namespace std;
#define ll long long
const int maxN = 5e2+10, mod = 998244353, inf = 1e9+10;
int n, m, head[maxN*2], cnt, b[maxN], st, en, dep[maxN*2], cha[maxN], seq[maxN];
queue<int> q;
struct edge{
int from, to, w, nxt;
}ed[maxN*maxN*2];
void add_edge(int x, int y, int w){
cnt++;
ed[cnt].from = x;
ed[cnt].to = y;
ed[cnt].w = w;
ed[cnt].nxt = head[x];
head[x] = cnt;
}
bool bfs(){
fill(dep, dep+n+m+5, 0);
q.push(st);
dep[st] = 1;
while (!q.empty()){
int s = q.front();
q.pop();
for (int i = head[s]; i ; i=ed[i].nxt) {
if (dep[ed[i].to] || ed[i].w <= 0) continue;
dep[ed[i].to] = dep[s]+1;
q.push(ed[i].to);
}
}
return dep[en];
}
int dinic(int now, int w, int last){
if (now == en || w <= 0) return w;
int su = 0, f;
for (int& i = cha[now]; i ; i=ed[i].nxt) {
if (dep[ed[i].to] == dep[now]+1 && (f = dinic(ed[i].to, min(w, ed[i].w), now))){
w -= f;
su += f;
if (last >= 1 && last <= n && now >= n+1 && now <= n+m && ed[i].to >= 1 && ed[i].to <= n) swap(seq[last], seq[ed[i].to]);
ed[i].w -= f;
ed[i^1].w += f;
if (!w) return su;
}
}
return su;
}
void solve(){
cin >> n >> m;
st = 0, en = n+m+1, cnt = 1;
iota(seq+1, seq+n+3, 1);
fill(head, head+n+m+3, 0);
for (int i = 1; i <= m; ++i) cin >> b[i];
for (int i = n; i >= 1; --i) {
add_edge(st, i, 1);
add_edge(i, st, 0);
}
int k, a;
for (int i = 1; i <= n; ++i) {
cin >> k;
vector<int> v;
for (int j = 1; j <= k; ++j) {
cin >> a;
v.push_back(a);
}
reverse(v.begin(), v.end());
for (auto j:v) {
add_edge(i, j+n, inf);
add_edge(j+n, i, 0);
}
}
for (int i = n+1; i <= n+m; ++i) {
add_edge(i, en, b[i-n]);
add_edge(en, i, 0);
}
int sum = 0;
while (bfs()){
memcpy(cha, head, (n+m+5)*sizeof(int));
sum += dinic(st, inf, 0);
}
cout << sum << "\n";
for (int i = 1; i <= n; ++i) cout << seq[i] << " \n"[i == n];
}
int main(){
ios::sync_with_stdio(false);
cin.tie();
int t;
cin >> t;
while (t--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3616kb
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 2 4 3 1 5 5 2 3 4 1 5 5 2 3 4 1
result:
wrong answer wrong rearrangement 4 5