QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#527008 | #5504. Flower Garden | nhuang685 | TL | 0ms | 3632kb | C++23 | 4.5kb | 2024-08-22 05:50:48 | 2024-08-22 05:50:48 |
Judging History
answer
/**
* @author n685
* @brief
* @date 2024-08-21 17:05:01
*
*
*/
#include "bits/stdc++.h"
#ifdef LOCAL
#include "dd/debug.h"
#else
#define dbg(...) 42
#define dbg_proj(...) 420
#define dbg_rproj(...) 420420
void nline() {}
void bar() {}
void start_clock() {}
void end_clock() {}
#endif
void tarjan(
int node,
std::vector<int> &clr,
std::vector<int> &dfn,
int n,
const std::vector<std::vector<int>> &adj
) {
static int cnt = 0, num = 0;
static std::vector<int> low(n, -1);
static std::vector<bool> vis(n);
static std::stack<int, std::vector<int>> st;
dfn[node] = cnt++;
low[node] = dfn[node];
st.push(node);
vis[node] = true;
for (int i : adj[node]) {
if (dfn[i] == -1) {
tarjan(i, clr, dfn, n, adj);
}
if (vis[i]) {
low[node] = std::min(low[node], low[i]);
}
}
if (dfn[node] == low[node]) {
while (!st.empty()) {
int v = st.top();
st.pop();
clr[v] = num;
vis[v] = false;
if (node == v) {
break;
}
}
++num;
}
}
std::vector<int> scc(int n, const std::vector<std::vector<int>> &adj) {
std::vector<int> clr(n, -1), dfn(n, -1);
for (int i = 0; i < n; ++i) {
if (dfn[i] == -1) {
tarjan(i, clr, dfn, n, adj);
}
}
return clr;
}
std::optional<std::vector<bool>> type1(
int n,
int thres,
const std::vector<int> &sz,
const std::vector<std::vector<int>> &adj,
const std::vector<std::vector<int>> &radj
) {
std::vector<int> in(n);
std::queue<int> q;
for (int i = 0; i < n; ++i) {
in[i] = static_cast<int>(adj[i].size());
if (in[i] == 0) {
q.push(i);
}
}
std::vector<bool> ans(n);
int sum = 0;
while (!q.empty()) {
int node = q.front();
q.pop();
if (sz[node] >= thres) {
continue;
}
sum += sz[node];
ans[node] = true;
if (sum >= thres) {
break;
}
for (int i : radj[node]) {
if (--in[i] == 0) {
q.push(i);
}
}
}
if (sum >= thres) {
return ans;
}
return std::nullopt;
}
int dfs_type2(
int node,
std::vector<bool> &ans,
const std::vector<int> &sz,
const std::vector<std::vector<int>> &adj
) {
int res = sz[node];
ans[node] = true;
for (int i : adj[node]) {
res += dfs_type2(i, ans, sz, adj);
}
return res;
}
std::optional<std::vector<bool>> type2(
int n,
int thres,
const std::vector<int> &sz,
const std::vector<std::vector<int>> &adj
) {
for (int i = 0; i < n; ++i) {
if (sz[i] >= thres) {
std::vector<bool> ans(n);
if (dfs_type2(i, ans, sz, adj) <= 2 * thres) {
return ans;
}
}
}
return std::nullopt;
}
void solve() {
int n, q;
std::cin >> n >> q;
std::vector<std::vector<int>> adj(3 * n + q);
for (int i = 0; i < q; ++i) {
int a, b, c, d;
std::cin >> a >> b >> c >> d;
--a;
--b;
--c;
--d;
for (int j = a; j <= b; ++j) {
adj[j].push_back(3 * n + i);
}
for (int j = c; j <= d; ++j) {
adj[3 * n + i].push_back(j);
}
}
std::vector<int> clr = scc(3 * n + q, adj);
int m = *std::ranges::max_element(clr) + 1;
std::vector<int> sz(m);
std::vector<std::vector<int>> sadj(m);
std::vector<std::vector<int>> gr(m);
for (int i = 0; i < 3 * n; ++i) {
++sz[clr[i]];
gr[clr[i]].push_back(i);
}
for (int i = 0; i < 3 * n + q; ++i) {
for (int j : adj[i]) {
if (clr[i] != clr[j]) {
sadj[clr[i]].push_back(clr[j]);
}
}
}
for (std::vector<int> &v : sadj) {
std::ranges::sort(v);
v.erase(std::unique(v.begin(), v.end()), v.end());
}
std::vector<std::vector<int>> sradj(m);
for (int i = 0; i < m; ++i) {
for (int j : sadj[i]) {
sradj[j].push_back(i);
}
}
std::optional<std::vector<bool>> opt = type1(m, n, sz, sadj, sradj);
std::vector<bool> ans;
if (opt.has_value()) {
ans = *opt;
} else {
opt = type2(m, n, sz, sadj);
if (opt.has_value()) {
ans = *opt;
} else {
std::cout << "NIE\n";
return;
}
}
std::cout << "TAK\n";
std::string val(3 * n, 'R');
for (int i = 0; i < m; ++i) {
if (ans[i]) {
for (int j : gr[i]) {
val[j] = 'F';
}
}
}
std::cout << val << '\n';
}
int main() {
#ifndef LOCAL
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
#endif
int z;
std::cin >> z;
for (int i = 0; i < z; ++i) {
dbg(i + 1);
solve();
bar();
}
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3632kb
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: -100
Time Limit Exceeded
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...