QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#373208 | #4513. Slide Parade | 5ab | 35 ✓ | 9285ms | 69912kb | C++20 | 3.8kb | 2024-04-01 09:23:35 | 2024-04-01 09:23:36 |
Judging History
answer
/* name: 4513
* author: 5ab
* created at: 2024-03-29
*/
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define ssz(x) (int((x).size()))
auto chmax = [](auto& x, auto y) { if (x < y) x = y; };
auto chmin = [](auto& x, auto y) { if (y < x) x = y; };
using ll = long long;
const int N = 200, M = 5000, ND = 2 * N + 2, E = 2 * N + M, INF = 1e9;
int hd[ND], des[E * 2], nxt[E * 2], val[E * 2], dis[ND], pre[ND], ec = 0;
bool ok[M], vs[N];
void _add(int s, int t, int v)
{
des[ec] = t, val[ec] = v;
nxt[ec] = hd[s], hd[s] = ec++;
}
void add(int s, int t, int v) {
_add(s, t, v), _add(t, s, 0);
}
int s, t, nd;
queue<int> q;
void bfs(int sp = s)
{
fill(dis, dis + nd, INF);
fill(pre, pre + nd, -1);
q.push(sp), dis[sp] = 0;
while (!q.empty())
{
int cx = q.front(); q.pop();
for (int p = hd[cx], dst; p != -1; p = nxt[p])
{
dst = des[p];
if (val[p] && dis[dst] > dis[cx] + 1)
{
dis[dst] = dis[cx] + 1;
pre[dst] = p;
q.push(dst);
}
}
}
}
vector<int> ng[N];
int cur[N];
vector<int> stk;
void dfs2(int id)
{
vs[id] = 1;
for (int &i = cur[id]; i < ssz(ng[id]); )
dfs2(ng[id][i++]);
stk.push_back(id);
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int cas, n, m;
cin >> cas;
for (int cid = 1; cid <= cas; cid++)
{
cin >> n >> m;
s = n * 2, t = n * 2 + 1, nd = n * 2 + 2;
fill(hd, hd + nd, -1), ec = 0;
for (int i = 0, x, y; i < m; i++)
{
cin >> x >> y, x--, y--;
add(x, y + n, 1);
}
for (int i = 0; i < n; i++)
{
add(s, i, 1);
add(i + n, t, 1);
}
auto doFl = [&](int p) {
// cerr << "fl " << p << endl;
val[p]--, val[p ^ 1]++;
};
auto doBan2 = [&](int p) {
p *= 2;
val[p] = 0, val[p ^ 1] = 0;
};
auto doOk = [&](int p) {
p *= 2;
val[p] = 0, val[p ^ 1] = 1;
};
auto doInv = [&](int p) {
// cerr << "inv " << p << endl;
val[p]++, val[p ^ 1]--;
};
auto doInv2 = [&](int p) { doInv(p * 2); };
int fl = 0;
auto aug = [&]() -> bool
{
bfs();
if (pre[t] == -1)
{
// cerr << "aug failed" << endl;
return 0;
}
for (int x = t; x != s; x = des[pre[x] ^ 1])
doFl(pre[x]);
fl++;
return 1;
};
while (aug());
if (fl != n)
{
cout << "Case #" << cid << ": IMPOSSIBLE\n";
continue;
}
for (int i = 0; i < n; i++)
ng[i].clear();
fill(ok, ok + m, 0);
bool sol = 1;
for (int i = 0; i < m; i++) if (!ok[i])
{
int x = des[i * 2 + 1], y = des[i * 2] - n;
// cerr << x << " " << y << endl;
for (int p = hd[x]; p != -1; p = nxt[p])
if (!val[p] && des[p] >= n)
{
doInv(p), doInv2(m + x * 2), doInv2(m + (des[p] - n) * 2 + 1);
fl--;
}
for (int p = hd[y + n]; p != -1; p = nxt[p])
if (val[p] && des[p] < n)
{
doInv(p ^ 1), doInv2(m + des[p] * 2), doInv2(m + y * 2 + 1);
fl--;
}
doBan2(m + x * 2), doBan2(i), doBan2(m + y * 2 + 1);
fl++;
// cerr << "try aug\n";
while (aug());
doOk(m + x * 2), doOk(i), doOk(m + y * 2 + 1);
if (fl != n)
{
sol = 0;
break;
}
for (int j = 0; j < m; j++)
if (!val[j * 2])
{
ok[j] = 1;
// cerr << des[j * 2 + 1] << " " << des[j * 2] - n << endl;
ng[des[j * 2 + 1]].push_back(des[j * 2] - n);
}
}
cout << "Case #" << cid << ": ";
if (sol)
{
stk.clear();
fill(cur, cur + n, 0);
fill(vs, vs + n, 0);
dfs2(0);
for (int j = 0; j < n; j++)
if (!vs[j])
sol = 0;
if (sol)
{
reverse(all(stk));
// stk.push_back(0);
cout << ssz(stk) << "\n";
for (int x : stk)
cout << x + 1 << " ";
cout << "\n";
continue;
}
}
cout << "IMPOSSIBLE\n";
}
return 0;
}
// started coding at: 03-29 23:13:52
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 11
Accepted
time: 1ms
memory: 3648kb
input:
100 5 8 1 2 1 3 1 4 1 5 2 1 3 1 4 1 5 1 5 10 1 3 1 4 2 3 2 5 3 1 3 4 3 5 4 2 5 1 5 3 5 10 1 4 2 3 2 5 3 1 3 5 4 2 4 3 4 5 5 1 5 2 3 6 1 2 1 3 2 1 2 3 3 1 3 2 5 10 1 2 1 5 2 3 2 4 3 1 4 3 4 5 5 2 5 3 5 4 4 10 1 2 1 3 1 4 2 1 2 3 2 4 3 1 3 4 4 2 4 3 5 10 1 2 1 3 2 1 2 4 3 1 3 5 4 2 4 5 5 3 5 4 5 10 1 ...
output:
Case #1: IMPOSSIBLE Case #2: 16 1 3 4 2 5 1 4 2 3 5 1 4 2 5 3 1 Case #3: 16 1 4 5 2 3 1 4 3 1 4 2 5 2 3 5 1 Case #4: 7 1 2 3 1 3 2 1 Case #5: 16 1 2 3 1 5 4 5 2 4 3 1 2 4 5 3 1 Case #6: 17 1 2 1 3 4 3 4 2 1 4 2 4 2 3 1 3 1 Case #7: 11 1 2 4 5 3 1 3 5 4 2 1 Case #8: 16 1 3 5 1 4 2 4 2 3 5 1 4 3...
result:
ok (100 test cases)
Test #2:
score: 24
Accepted
time: 9285ms
memory: 69912kb
input:
100 199 4980 1 95 1 96 1 105 1 124 1 130 1 131 1 135 1 137 1 139 1 140 1 141 1 147 1 155 1 172 1 174 1 179 1 183 1 188 1 193 1 196 1 197 2 3 2 5 2 13 2 14 2 17 2 20 2 24 2 26 2 30 2 41 2 44 2 45 2 52 2 56 2 67 2 70 2 71 2 74 2 78 2 84 2 85 2 90 2 92 2 93 2 97 2 107 2 111 2 113 2 122 2 124 2 128 2 13...
output:
Case #1: IMPOSSIBLE Case #2: IMPOSSIBLE Case #3: 555401 1 18 29 35 21 27 32 5 168 167 158 175 12 186 181 178 171 13 15 25 26 28 23 17 11 4 166 155 144 150 159 131 109 100 104 129 153 160 8 16 10 14 34 43 54 60 36 20 3 7 135 136 149 154 140 127 133 141 114 112 102 95 81 78 50 51 37 22 40 190 187 6 2 ...
result:
ok (100 test cases)