QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#470853 | #5504. Flower Garden | wcyQwQ | RE | 0ms | 0kb | C++17 | 3.5kb | 2024-07-10 16:42:15 | 2024-07-10 16:42:15 |
answer
#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 10;
vector<int> g[N], G[N], R[N];
int tot, val[N], sz[N], dfn[N], low[N], vis[N], stk[N], id[N], tim, top, cnt, n, m;
inline void add(int u, int v) { g[u].emplace_back(v); }
struct SGT {
int t[N];
void build(int p, int l, int r, int f) {
if (l == r) {
t[p] = l;
vector<int>().swap(g[l]);
return;
}
t[p] = ++tot;
vector<int>().swap(g[tot]);
int m = (l + r) / 2;
build(p * 2, l, m, f);
build(p * 2 + 1, m + 1, r, f);
if (f) ::add(t[p * 2], t[p]), ::add(t[p * 2 + 1], t[p]);
else ::add(t[p], t[p * 2]), ::add(t[p], t[p * 2 + 1]);
}
void add(int p, int l, int r, int ql, int qr, int id, int f) {
if (ql <= l && r <= qr) {
if (f) ::add(t[p], id);
else ::add(id, t[p]);
return;
}
int m = (l + r) / 2;
if (ql <= m) add(p * 2, l, m, ql, qr, id, f);
if (qr > m) add(p * 2 + 1, m + 1, r, ql, qr, id, f);
}
} sgt[2];
void tarjan(int u) {
dfn[u] = low[u] = ++tim;
vis[u] = 1, stk[++top] = u;
for (int v : g[u]) {
if (!dfn[v]) {
tarjan(v);
low[u] = min(low[u], low[v]);
} else if (vis[v]) {
low[u] = min(low[u], dfn[v]);
}
}
if (dfn[u] == low[u]) {
cnt++;
sz[cnt] = 0;
int v;
do {
v = stk[top--];
vis[v] = 0;
id[v] = cnt;
sz[cnt] += val[v];
} while (u != v);
}
}
void print(int x) {
cout << "TAK\n";
for (int i = 1; i <= 3 * n; i++) {
cout << (vis[id[i]] == x ? 'F' : 'R');
}
cout << '\n';
}
bool check(int u) {
fill(vis + 1, vis + cnt + 1, 0);
queue<int> q;
q.emplace(u);
int sum = sz[u];
vis[u] = 1;
while (q.size()) {
int u = q.front();
q.pop();
for (int v : G[u]) if (!vis[v]) {
sum += sz[v];
vis[v] = 1;
q.emplace(v);
}
}
if (sum <= 2 * n) {
print(1);
return 1;
}
fill(vis + 1, vis + cnt + 1, 0);
q.emplace(u);
sum = sz[u];
while (q.size()) {
int u = q.front();
q.pop();
for (int v : R[u]) if (!vis[v]) {
sum += sz[v];
vis[v] = 1;
q.emplace(v);
}
}
if (sum <= 2 * n) {
print(0);
return 1;
}
return 0;
}
void solve() {
cin >> n >> m;
tot = 3 * n;
sgt[0].build(1, 1, 3 * n, 0);
sgt[1].build(1, 1, 3 * n, 1);
while (m--) {
int a, b, c, d;
cin >> a >> b >> c >> d;
int id = ++tot;
sgt[1].add(1, 1, 3 * n, a, b, id, 1);
sgt[0].add(1, 1, 3 * n, c, d, id, 0);
}
fill(val + 1, val + tot + 1, 0);
fill(dfn + 1, dfn + tot + 1, 0);
fill(val + 1, val + 3 * n + 1, 1);
fill(vis + 1, vis + tot + 1, 0);
tim = top = cnt = 0;
for (int i = 1; i <= tot; i++) {
if (!dfn[i]) tarjan(i);
}
int sum = 0;
for (int i = 1; i <= cnt; i++) {
sum += sz[i];
vis[i] = 1;
if (sum >= n && sum <= 2 * n) {
print(1);
return;
}
}
for (int i = 1; i <= cnt; i++) {
vector<int>().swap(G[i]);
vector<int>().swap(R[i]);
}
for (int i = 1; i <= tot; i++) {
for (int j : g[i]) {
if (id[i] != id[j]) {
G[id[i]].emplace_back(id[j]);
R[id[j]].emplace_back(id[i]);
}
}
}
for (int i = 1; i <= cnt; i++) {
if (sz[i] > n && check(i)) return;
}
cout << "NIE\n";
}
int main() {
freopen("1.in", "r", stdin);
cin.tie(0)->sync_with_stdio(0);
int T;
cin >> T;
while (T--) solve();
return 0;
}
详细
Test #1:
score: 0
Runtime Error
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