QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#416189 | #4513. Slide Parade | Whicy | 11 | 0ms | 3892kb | C++20 | 4.1kb | 2024-05-21 17:04:29 | 2024-05-21 17:04:29 |
Judging History
answer
#include <cstdio>
#include <string>
#include <vector>
namespace iobuff {
const int LEN = 1000000;
char in[LEN + 5], out[LEN + 5];
char *pin = in, *pout = out, *ed = in, *eout = out + LEN;
inline char gc(void) {
return pin == ed && (ed = (pin = in) + fread(in, 1, LEN, stdin), ed == in) ? EOF : *pin++;
}
inline void pc(char c) {
pout == eout && (fwrite(out, 1, LEN, stdout), pout = out);
(*pout++) = c;
}
inline void ps(const std::string &s) {
for (auto c : s)
pc(c);
}
inline void flush() { fwrite(out, 1, pout - out, stdout), pout = out; }
template <typename T>
inline void scan(T &x) {
static int f;
static char c;
c = gc(), f = 1, x = 0;
while (c < '0' || c > '9')
f = (c == '-' ? -1 : 1), c = gc();
while (c >= '0' && c <= '9')
x = 10 * x + c - '0', c = gc();
x *= f;
}
template <typename T>
inline void putint(T x, char div) {
static char s[100];
static int top;
top = 0;
x < 0 ? pc('-'), x = -x : 0;
while (x)
s[top++] = x % 10, x /= 10;
!top ? pc('0'), 0 : 0;
while (top--)
pc(s[top] + '0');
pc(div);
}
}
using namespace iobuff;
int main() {
int T;
scan(T);
for (int TC = 1; TC <= T; TC++) {
ps("Case #"), putint(TC, ':'), pc(' ');
int n, m;
scan(n), scan(m);
std::vector<std::vector<int>> ed(n), rved(n);
for (int i = 0; i < m; i++) {
int u, v;
scan(u), scan(v);
ed[--u].push_back(--v);
rved[v].push_back(u);
}
std::vector<bool> con1(n), con2(n);
bool imp = 0;
int lst;
auto scc1 = [&](const auto &scc1, int u) -> void {
con1[u] = 1;
for (auto v : ed[u])
if (!con1[v])
scc1(scc1, v);
lst = u;
};
scc1(scc1, 0);
for (int i = 0; i < n; i++) {
if (!con1[i]) {
imp = 1;
break;
}
}
auto scc2 = [&](const auto &scc2, int u) -> void {
con2[u] = 1;
for (auto v : rved[u])
if (!con2[v])
scc2(scc2, v);
};
scc2(scc2, lst);
for (int i = 0; i < n; i++) {
if (!con2[i]) {
imp = 1;
break;
}
}
if (imp) {
ps("IMPOSSIBLE\n");
continue;
}
std::vector<int> lnk(n, -1), vis(n, 0);
auto dfs = [&](const auto &dfs, const int &u, std::vector<bool> &vis) -> bool {
for (int v : ed[u]) {
if (vis[v])
continue;
vis[v] = 1;
if (!~lnk[v] || dfs(dfs, lnk[v], vis)) {
lnk[v] = u;
return true;
}
}
return false;
};
int cnt = 0;
for (int i = 0; i < n; i++) {
std::vector<bool> vis(n, 0);
if (dfs(dfs, i, vis))
cnt++;
}
if (cnt < n) {
ps("IMPOSSIBLE\n");
continue;
}
// begin
std::vector<std::vector<int>> nwed(n);
for (int u = 0; u < n; u++) {
if (imp)
break;
for (auto v : ed[u]) {
if (lnk[v] == u) {
for (int i = 0; i < n; i++) {
nwed[lnk[i]].push_back(i);
}
continue;
}
for (int i = 0; i < n; i++)
if (lnk[i] == u)
lnk[i] = -1;
int tmp = lnk[v];
lnk[v] = u;
std::vector<bool> vis(n, 0);
vis[v] = 1;
if (!dfs(dfs, tmp, vis)) {
imp = 1;
break;
}
for (int i = 0; i < n; i++) {
nwed[lnk[i]].push_back(i);
}
}
}
if (imp) {
ps("IMPOSSIBLE\n");
continue;
}
// end
putint( n * m + 1 , '\n');
std::vector<int> ans(n*m);
int cans=0;
auto fnd = [&](const auto &fnd, const int &u) -> void {
for (auto &x : nwed[u]) {
int v = x;
if (!~v)
continue;
x = -1;
fnd(fnd, v);
ans[cans++]=u;
}
};
fnd(fnd, 0);
int st = 0;
for (; ans[st] != 0; st++)
;
for (int i = st; ~i; i--) {
putint(ans[i]+1, ' ');
}
for (int i = n * m - 1; i > st; i--) {
putint(ans[i]+1, ' ');
}
ps("1\n");
}
flush();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 11
Accepted
time: 0ms
memory: 3892kb
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: 51 1 4 2 5 3 1 3 4 2 5 1 4 2 5 3 1 4 2 3 5 1 4 2 5 3 1 4 2 5 3 1 3 4 2 5 1 4 2 3 5 1 4 2 3 5 1 4 2 3 5 1 Case #3: 51 1 4 3 1 4 2 3 5 1 4 2 3 5 1 4 3 1 4 3 1 4 2 5 2 5 2 3 5 1 4 2 3 5 1 4 3 1 4 5 2 5 2 3 1 4 2 3 5 2 5 1 Case #4: 19 1 3 2 1 2 3 1 3 2 1 3 2 1 2 3 1 2 3 1 Ca...
result:
ok (100 test cases)
Test #2:
score: 0
Time Limit Exceeded
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: 1000001 1 67 142 99 115 89 49 143 192 181 156 174 34 170 198 177 78 2 148 15 111 90 113 26 155 125 38 28 140 42 51 4 57 110 88 172 53 8 169 22 43 195 25 104 126 146 87 3 91 135 139 158 185 165 197 86 194 47 55 178 166 47 59 29 154 45 101 171 82 24 66 ...