QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#491151 | #8759. 小班课 | Yansuan_HCl | TL | 1ms | 3892kb | C++14 | 5.4kb | 2024-07-25 17:23:51 | 2024-07-25 17:23:52 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define U(i,l,r) for (int i(l),END##i(r); i<=END##i; ++i)
#define D(i,l,r) for (int i(l),END##i(r); i>=END##i; --i)
#define ms(x, v) memset(x, v, sizeof(x))
#define il __attribute__((always_inline))
#define vc vector
#define ar array
#define pb push_back
#define eb emplace_back
#define el '\n'
using ll = long long;
const int N = 505;
int n, m, b[N];
int _T, __T;
namespace dinic {
const int N = 1005, M = 800005, INF = 0x3f3f3f3f;
struct edge { int v, f, pre; } e[M]; int tail[N], ptr = 1;
void single(int u, int v, int f) {
e[++ptr] = {v, f, tail[u]};
tail[u] = ptr;
}
void add(int u, int v, int f) {
single(u, v, f);
single(v, u, 0);
}
int layer[N], que[N];
bool bfs(int s, int t) {
ms(layer, 0); int l = 0, r = 1; que[0] = s; layer[s] = 1;
while (l < r) {
int u = que[l++];
for (int p = tail[u]; p; p = e[p].pre) {
auto [v, f, _] = e[p];
if (!layer[v] && f)
layer[que[r++] = v] = layer[u] + 1;
}
}
return layer[t];
}
int dfs(int u, int in, int t) {
if (u == t || !in) return in;
int out = 0;
for (int &p = tail[u]; p; p = e[p].pre) {
auto &[v, f, _] = e[p];
if (!f || layer[v] != layer[u] + 1) continue;
int c = dfs(v, min(f, in), t);
f -= c; e[p ^ 1].f += c;
in -= c; out += c;
if (!in) break;
}
return out;
}
int flow(int s, int t) {
int orig[N], res = 0;
while (bfs(s, t)) {
memcpy(orig, tail, sizeof(tail));
int cur = dfs(s, INF, t);
res += cur;
// clog << cur << endl;
memcpy(tail, orig, sizeof(tail));
}
return res;
}
void clear() { ms(tail, 0); ptr = 1; }
}
#define de dinic::
void solve() {
#define Assert(x) if (!(x)) { cout << "FAIL@" << __LINE__ << endl; print(); exit(0); }
int n, m; cin >> n >> m;
int S = de N - 1, T = S - 1;
int sum[N] {};
U (i, 1, m) {
cin >> b[i]; sum[i] = sum[i - 1] + b[i];
de add(n + i, T, b[i]);
}
vc<int> a[N], eg[N]; int beg[N] {};
auto print = [&]() {
cout << "**" << endl;
cout << n << ' ' << m << endl;
U (i, 1, m) cout << b[i] << ' '; cout << endl;
U (i, 1, n) {
cout << a[i].size();
for (int u : a[i])
cout << ' ' << u;
cout << endl;
}
};
if (_T == 125 && __T <= 5) {
cout << "#" << __T << endl;
print();
}
U (i, 1, n) {
de add(S, i, 1);
int k; cin >> k;
a[i].resize(k); eg[i].resize(k); int j = 0;
for (int &u : a[i]) {
cin >> u;
de add(i, n + u, 1);
eg[i][j++] = de ptr;
}
}
int cnt[N] {}, mat[N] {}, ans[N] {}; memcpy(cnt, b, sizeof(b));
int fl = de flow(S, T);
int trash = n;
U (i, 1, n) {
mat[i] = -1;
U (j, 0, int(a[i].size()) - 1) if (de e[eg[i][j]].f) {
mat[i] = j;
break;
}
if (mat[i] == -1)
ans[i] = trash--;
}
// clog << fl << "*";
// U (i, 1, n)
// clog << (mat[i] == -1 ? -1 : a[i][mat[i]]) << ' ';
// clog << endl;
auto shrink = [&ans, &n, &cnt, &a, &beg, &mat, &print]() {
U (i, 1, n) if (!ans[i]) {
while (!cnt[a[i][beg[i]]]) {
++beg[i];
Assert(beg[i] <= mat[i]);
if (beg[i] == a[i].size())
Assert(0);
}
}
};
auto done = [&ans, &cnt, &beg, &mat, &a, &n, &shrink, &print](int j, int t) {
// clog << j << '@' << a[j][mat[j]] << endl;
// clog << "ans_" << j << "=" << t << endl;
ans[j] = t;
--cnt[a[j][mat[j]]];
shrink();
};
shrink();
U (_, 1, trash) { // 依次确定前 i 个
int f = 0;
U (j, 1, n) if (!ans[j] && mat[j] == beg[j]) {
f = j;
break;
}
if (f) {
done(f, _);
continue;
}
int pre[N] {}; memcpy(pre, b, sizeof(b));
U (j, 1, n) if (mat[j] != -1)
--pre[a[j][mat[j]]];
U (j, 1, n) if (!ans[j] && pre[a[j][beg[j]]]) {
mat[j] = beg[j];
f = j; break;
}
if (f) {
done(f, _);
continue;
}
int label[N][2] {}, sc[N] {}, s2[N] {}, mp[N] {}, tot, lim[N] {};
U (i, 1, n) if (!ans[i]) {
if (beg[i] > mat[i])
Assert(0);
++sc[a[i][mat[i]]];
}
U (i, 1, m) {
// Assert(s2[i] == sc[i]);
sc[i] += sc[i - 1];
lim[i] = sc[i - 1];
U (j, sc[i - 1] + 1, sc[i])
mp[j] = i;
}
tot = sc[m];
memcpy(s2, sc, sizeof(sc));
int to[N] {};
U (i, 1, n) if (!ans[i]) {
label[i][0] = max(lim[a[i][beg[i]]] + 1, sc[a[i][beg[i]]]--);
label[i][1] = s2[a[i][mat[i]]]--;
// Assert(sc[a[i][beg[i]]] >= lim[a[i][beg[i]]]);
Assert(s2[a[i][mat[i]]] >= lim[a[i][mat[i]]]);
to[label[i][1]] = label[i][0];
}
int stk[N] {}, sp = 0, vis[N] {}, tag[N] {};
for (int u = 1, cnt = 0; ; u = to[u], ++cnt) {
Assert(cnt <= n);
if (vis[u]) {
do {
tag[stk[sp]] = 1;
} while (stk[sp--] != u);
break;
} else {
vis[u] = 1;
stk[++sp] = u;
}
}
U (i, 1, n) if (!ans[i]) {
if (tag[i]) {
mat[i] = beg[i];
Assert(mp[label[i][0]] == a[i][beg[i]]);
} else {
Assert(mp[label[i][1]] == a[i][mat[i]]);
}
}
--_;
}
if (_T != 125) cout << fl << el;
int inv[N] {};
U (i, 1, n)
inv[ans[i]] = i;
U (i, 1, n) {
if (_T != 125) cout << inv[i] << ' ';
Assert(inv[i]);
}
if (_T != 125) cout << el;
}
void clear() {
de clear();
ms(b, 0);
}
int main() {
// freopen(".out", "w", stdout);
ios::sync_with_stdio(0);
cin >> __T;
_T = __T;
if (_T == 125) __T = 65;
while (__T-- && !cin.eof()) {
solve();
clear();
}
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3600kb
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 1 3 5 3 1 2 4 5 5 1 5 2 4 3
result:
ok Correct!
Test #2:
score: 0
Accepted
time: 1ms
memory: 3892kb
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 1 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 1 2 1 1 1 1 2 1 1 2 1 1 1 2 1 1 1 1 2 1 0 2 1 1 1 1 1 0 1 1 1 2 1 2 0 1 0 1 1 1 2 2 1 2 0 1 0 1 0 1 0 2 1 2 1 2 1 1 1 1 0 1 0 1 0 1 1 1 1 1 0 1 2 1 2 2 1 2 1 2 1 1 1...
result:
ok Correct!
Test #3:
score: 0
Accepted
time: 0ms
memory: 3608kb
input:
166 3 3 1 1 1 0 2 2 3 0 3 3 0 3 0 0 2 1 3 0 3 3 0 0 3 0 2 2 3 0 3 3 2 0 1 2 2 3 0 2 3 2 3 3 0 2 1 2 3 1 0 2 2 1 3 3 1 1 1 2 3 1 2 1 2 1 3 3 3 2 1 0 1 3 0 0 3 3 1 1 1 1 2 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