QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#511379 | #5504. Flower Garden | qL | WA | 59ms | 380352kb | C++14 | 3.8kb | 2024-08-09 20:11:40 | 2024-08-09 20:11:41 |
Judging History
answer
#include <algorithm>
#include <cstdio>
#include <vector>
using i32 = int;
using i64 = long long;
constexpr i32 N = 1E6;
i32 n, m, s;
std::vector<i32> G[N * 8];
struct {
i32 id1, id2;
} tr[N * 4];
i32 id, in[N + 1], out[N + 1], val[N * 8];
void build(i32 p, i32 l, i32 r) noexcept {
val[tr[p].id1 = ++id] = 0, val[tr[p].id2 = ++id] = 0;
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 * 8], low[N * 8], cur;
i32 sta[N * 8], *top;
bool ins[N * 8];
i32 scc[N * 8], siz[N * 8], 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 * 8];
i32 deg[N * 8], seq[N * 8], rnk[N * 8];
i32 que[N * 8], *hd, *tl;
char ans[N + 1];
signed main() noexcept {
i32 Test;
for (scanf("%d", &Test); Test; --Test) {
for (i32 i = 1; i <= id; ++i) G[id].clear();
__builtin_memset(dfn + 1, 0, sizeof(i32) * id);
__builtin_memset(low + 1, 0, sizeof(i32) * id);
for (i32 i = 1; i <= col; ++i) H[id].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]);
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());
for (i32 i = 1; i <= col; ++i)
for (i32 j : H[i]) ++deg[j];
hd = tl = que, cur = 0;
for (i32 i = 1; i <= col; ++i)
if (!deg[i]) *tl++ = i;
for (i32 u; hd != tl;)
for (i32 v : H[rnk[u = *hd++] = ++cur, seq[cur] = u])
if (!--deg[v]) *tl++ = v;
if (*std::max_element(siz + 1, siz + col + 1) >= s) {
i32 t = std::max_element(siz + 1, siz + col + 1) - siz;
i32 pre = 0, suf = 0;
for (i32 i = 1; seq[i] != t; ++i) pre += siz[seq[i]];
suf = n - siz[t] - pre;
if (pre + siz[t] <= s * 2) {
__builtin_memset(ans + 1, 'R', sizeof(char) * col);
for (i32 i = 1; seq[i] != t; ++i) ans[seq[i]] = 'F';
ans[t] = 'F', puts("TAK");
for (i32 i = 1; i <= n; ++i) putchar(ans[scc[out[i]]]);
puts("");
} else if (suf + siz[t] <= s * 2) {
__builtin_memset(ans + 1, 'F', sizeof(char) * col);
for (i32 i = n; seq[i] != t; --i) ans[seq[i]] = 'R';
ans[t] = 'R', puts("TAK");
for (i32 i = 1; i <= n; ++i) putchar(ans[scc[out[i]]]);
puts("");
} else
puts("NIE");
} else {
i32 sum = 0;
__builtin_memset(ans + 1, 'R', sizeof(char) * col);
for (i32 i = 1; (sum += siz[seq[i]]) <= s; ++i) ans[seq[i]] = 'F';
puts("TAK");
for (i32 i = 1; i <= n; ++i) putchar(ans[scc[out[i]]]);
puts("");
}
}
return 0;
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 59ms
memory: 380352kb
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 FFR NIE
result:
wrong answer odpowiedz nie spelnia wymogow!