QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#511423 | #5504. Flower Garden | qL | WA | 2813ms | 217592kb | C++14 | 4.4kb | 2024-08-09 21:00:05 | 2024-08-09 21:00:05 |
Judging History
answer
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <vector>
using i32 = int;
using i64 = long long;
constexpr i32 N = 1E5;
i32 n, m, s;
std::vector<i32> G[N * 9];
struct {
i32 id1, id2;
} tr[N * 4];
i32 id, in[N + 1], out[N + 1], val[N * 9];
void build(i32 p, i32 l, i32 r) noexcept {
tr[p].id1 = ++id, tr[p].id2 = ++id;
if (l == r) return in[l] = tr[p].id1, val[out[r] = tr[p].id2] = 1, void(G[in[l]].emplace_back(out[r]));
i32 mid = (l + r) >> 1;
build(p << 1, l, mid), build(p << 1 | 1, mid + 1, r);
G[tr[p].id1].emplace_back(tr[p << 1].id1), G[tr[p].id1].emplace_back(tr[p << 1 | 1].id1);
G[tr[p << 1].id2].emplace_back(tr[p].id2), G[tr[p << 1 | 1].id2].emplace_back(tr[p].id2);
}
template <typename Linker>
void link(i32 p, i32 l, i32 r, i32 L, i32 R, Linker&& linker) noexcept {
if (L <= l && r <= R) return linker(tr[p].id1, tr[p].id2);
i32 mid = (l + r) >> 1;
if (L <= mid) link(p << 1, l, mid, L, R, linker);
if (R > mid) link(p << 1 | 1, mid + 1, r, L, R, linker);
}
i32 dfn[N * 9], low[N * 9], cur;
i32 sta[N * 9], *top;
bool ins[N * 9];
i32 scc[N * 9], siz[N * 9], col;
void dfs(i32 u) noexcept {
dfn[u] = low[u] = ++cur, ins[*++top = u] = true;
for (i32 v : G[u])
if (!dfn[v])
dfs(v), low[u] = std::min(low[u], low[v]);
else if (ins[v])
low[u] = std::min(low[u], dfn[v]);
if (dfn[u] == low[u]) {
siz[++col] = 0;
do siz[scc[*top] = col] += val[*top], ins[*top] = false;
while (*top-- != u);
}
}
std::vector<i32> H[N * 9], R[N * 9];
i32 deg[N * 9], seq[N * 9], *hd, *tl;
char ans[N * 9];
template <std::vector<i32>* T>
i32 dfs(i32 u, std::vector<i32>& vec) noexcept {
i32 sum = siz[u];
for (i32 v : T[u]) sum += dfs<T>(v, vec);
return vec.emplace_back(u), sum;
}
signed main() noexcept {
i32 Test;
for (scanf("%d", &Test); Test; --Test) {
for (i32 i = 1; i <= id; ++i) G[i].clear();
__builtin_memset(dfn + 1, 0, sizeof(i32) * id);
__builtin_memset(low + 1, 0, sizeof(i32) * id);
__builtin_memset(val + 1, 0, sizeof(i32) * id);
for (i32 i = 1; i <= col; ++i) H[i].clear(), R[i].clear();
scanf("%d%d", &n, &m), s = n, id = 0, build(1, 1, n *= 3);
for (i32 a, b, c, d; m; --m)
scanf("%d%d%d%d", &a, &b, &c, &d), ++id,
link(1, 1, n, a, b, [](i32 in, i32 out) noexcept { G[out].emplace_back(id); }),
link(1, 1, n, c, d, [](i32 in, i32 out) noexcept { G[id].emplace_back(in); });
cur = 0, top = sta, col = 0;
for (i32 i = 1; i <= id; ++i)
if (!dfn[i]) dfs(i);
for (i32 i = 1; i <= id; ++i)
for (i32 j : G[i])
if (scc[i] != scc[j]) H[scc[i]].emplace_back(scc[j]), R[scc[j]].emplace_back(scc[i]);
for (i32 i = 1; i <= col; ++i)
std::sort(H[i].begin(), H[i].end()), H[i].erase(std::unique(H[i].begin(), H[i].end()), H[i].end()),
std::sort(R[i].begin(), R[i].end()), R[i].erase(std::unique(R[i].begin(), R[i].end()), R[i].end());
for (i32 i = 1; i <= col; ++i)
for (i32 j : H[i]) ++deg[j];
hd = tl = seq + 1;
for (i32 i = 1; i <= col; ++i)
if (!deg[i]) *tl++ = i;
for (i32 u; hd != tl;)
for (i32 v : H[u = *hd++])
if (!--deg[v]) *tl++ = v;
// for (i32 i = 1; i <= n; ++i) fprintf(stderr, "%d %d\n", scc[i], val[i]);
// for (i32 i = 1; i <= col; ++i) fprintf(stderr, "%d ", siz[i]);
// fprintf(stderr, "\n");
// for (i32 i = 1; i <= col; ++i) fprintf(stderr, "%d ", seq[i]);
// fprintf(stderr, "\n");
// fprintf(stderr, "\n");
bool flag = false;
for (i32 t = 1; t <= col && !flag; ++t)
if (siz[t] >= s) {
std::vector<i32> pv, sv;
i32 pre = dfs<R>(t, pv), suf = dfs<H>(t, sv);
if (pre <= s * 2) {
__builtin_memset(ans + 1, 'F', sizeof(char) * col);
for (i32 it : pv) ans[it] = 'R';
puts("TAK");
for (i32 i = 1; i <= n; ++i) putchar(ans[scc[out[i]]]);
puts(""), flag = true;
} else if (suf <= s * 2) {
__builtin_memset(ans + 1, 'R', sizeof(char) * col);
for (i32 it : sv) ans[it] = 'F';
puts("TAK");
for (i32 i = 1; i <= n; ++i) putchar(ans[scc[out[i]]]);
puts(""), flag = true;
}
}
if (!flag) {
i32 sum = 0;
__builtin_memset(ans + 1, 'F', sizeof(char) * col);
for (i32 i = 1; i <= col; ++i)
if (ans[seq[i]] = 'R', (sum += siz[seq[i]]) > s) break;
if (sum >= 2 * s)
puts("NIE");
else {
puts("TAK");
for (i32 i = 1; i <= n; ++i) putchar(ans[scc[out[i]]]);
puts("");
}
}
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 7ms
memory: 87796kb
input:
2 1 3 1 1 2 2 1 2 3 3 1 1 3 3 1 3 1 1 2 2 2 2 3 3 3 3 1 1
output:
TAK RRF NIE
result:
ok good!
Test #2:
score: 0
Accepted
time: 1976ms
memory: 155092kb
input:
10 33333 100000 28701 40192 93418 95143 95902 97908 78378 78461 36823 44196 22268 23996 23977 24786 33315 48829 83965 90411 4923 8445 20235 21177 32543 47454 29598 35414 72477 73049 2014 12632 42163 46466 64305 65518 98825 99552 32331 41625 92772 96224 26500 54122 76990 77126 18249 20335 31165 36080...
output:
NIE NIE NIE NIE NIE NIE NIE NIE NIE NIE
result:
ok good!
Test #3:
score: 0
Accepted
time: 2813ms
memory: 217592kb
input:
10 33333 100000 15207 33614 66276 66276 97173 97173 67589 73960 19673 36626 65207 65207 89825 98169 27079 27079 56067 56966 7560 7560 18170 35477 18752 18752 32621 36748 34460 34460 61595 61700 14117 14117 32395 36710 9064 9064 13172 13172 1728 4640 40462 41878 47171 47171 76965 82414 5767 5767 9225...
output:
TAK FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF...
result:
ok good!
Test #4:
score: 0
Accepted
time: 2613ms
memory: 199620kb
input:
10 33333 100000 2646 2646 6430 6446 82226 82231 15128 15132 877 877 85831 88474 51389 79196 37573 37573 38030 38030 14248 14280 63032 68489 81074 81074 46468 46468 7403 7487 19864 20058 97979 97979 71640 71833 8558 8558 12121 12434 82481 82481 32901 32901 1899 2162 65312 77886 75969 75974 87983 8798...
output:
TAK FFFFFFFFFFFFFFFFFFFFFFFFFFFFFRRFFFFRFFRFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF...
result:
ok good!
Test #5:
score: -100
Wrong Answer
time: 513ms
memory: 81496kb
input:
87005 1 3 1 1 2 2 1 2 3 3 1 1 3 3 1 3 1 1 2 2 2 2 3 3 3 3 1 1 1 1 1 3 2 3 1 2 1 1 2 2 2 2 1 1 1 2 1 3 1 1 1 1 1 3 4 20 3 5 6 12 4 5 7 11 1 1 2 2 3 4 7 12 3 5 10 10 3 5 8 8 4 4 9 11 4 4 7 7 1 1 9 10 3 4 6 9 3 5 11 12 3 3 7 9 3 5 2 2 4 5 2 2 1 1 7 11 1 1 10 10 3 5 7 8 4 4 2 2 1 1 2 2 4 5 8 10 4 12 11 ...
output:
TAK RRF NIE TAK RFF TAK RRF NIE TAK RFRRRRFFFFFF TAK RFRFFFRFRFFR NIE TAK RFFRRRRFFFFFFFR TAK FRRRRRFFFFFF TAK FFRRRRFFRFFF TAK FFFFFRFRFFFRRRR TAK RRRRFRFFFFFF TAK RRRRRRFFFFFFFFF NIE TAK RFFFRRRFRFFF NIE TAK RRFFFRRFF NIE TAK FFFFFRRRR TAK RRRRRFFFFFFF TAK FRRRFRRRRRRFFFF TAK RRRRFRFFFFFF NIE TAK ...
result:
wrong answer zla odpowiedz!