QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#527012 | #5504. Flower Garden | nhuang685 | WA | 0ms | 3568kb | C++23 | 5.8kb | 2024-08-22 06:44:15 | 2024-08-22 06:44:15 |
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 build(
int node,
int l,
int r,
int &sz,
std::vector<int> &iin,
std::vector<int> &iout,
std::vector<std::vector<int>> &adj
) {
if (l == r) {
iin[node] = l;
iout[node] = l;
return;
}
iin[node] = sz++;
iout[node] = sz++;
int mid = (l + r) / 2;
build(2 * node, l, mid, sz, iin, iout, adj);
build(2 * node + 1, mid + 1, r, sz, iin, iout, adj);
adj[iout[node]].push_back(iout[2 * node]);
adj[iout[node]].push_back(iout[2 * node + 1]);
adj[iin[2 * node]].push_back(iin[node]);
adj[iin[2 * node + 1]].push_back(iin[node]);
}
enum class Dir : int8_t { IN, OUT };
void unite(
int a,
int b,
int v,
Dir dir,
int node,
int l,
int r,
std::vector<std::vector<int>> &adj,
const std::vector<int> &iin,
const std::vector<int> &iout
) {
if (a <= l && r <= b) {
if (dir == Dir::OUT) {
adj[iout[node]].push_back(v);
} else {
adj[v].push_back(iin[node]);
}
return;
}
int mid = (l + r) / 2;
if (a <= mid) {
unite(a, b, v, dir, 2 * node, l, mid, adj, iin, iout);
}
if (b > mid) {
unite(a, b, v, dir, 2 * node + 1, mid + 1, r, adj, iin, iout);
}
}
void solve() {
int n, q;
std::cin >> n >> q;
std::vector<int> iin(6 * n), iout(6 * n);
int cnt = 3 * n;
std::vector<std::vector<int>> adj(9 * n + q);
build(1, 0, 3 * n - 1, cnt, iin, iout, adj);
for (int i = 0; i < q; ++i) {
int a, b, c, d;
std::cin >> a >> b >> c >> d;
--a;
--b;
--c;
--d;
unite(a, b, cnt + i, Dir::OUT, 1, 0, 3 * n - 1, adj, iin, iout);
unite(c, d, cnt + i, Dir::IN, 1, 0, 3 * n - 1, adj, iin, iout);
}
std::vector<int> clr = scc(cnt + 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 < cnt + 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();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3568kb
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 RFR NIE
result:
wrong answer odpowiedz nie spelnia wymogow!