QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#329597#5504. Flower Gardenddl_VS_pigeonWA 2627ms120360kbC++204.4kb2024-02-16 22:30:242024-02-16 22:30:24

Judging History

你现在查看的是最新测评结果

  • [2024-02-16 22:30:24]
  • 评测
  • 测评结果:WA
  • 用时:2627ms
  • 内存:120360kb
  • [2024-02-16 22:30:24]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
using ll = long long;

struct Graph {
	const int n;
	vector<vector<int>> e;
	vector<int> dfn, low, col, ins;
	stack<int> stk;
	int idc, scc;
	Graph(int _n) : n(_n), e(n), dfn(n), low(n), col(n), ins(n), idc(0), scc(0) {}
	void addedge(int a, int b) {
		e[a].emplace_back(b);
	}
	void tarjan(int v) {
		low[v] = dfn[v] = ++idc;
		stk.push(v), ins[v] = true;
		for (auto u : e[v]) {
			if (!dfn[u]) {
				tarjan(u);
				low[v] = min(low[v], low[u]);
			} else if (ins[u]) {
				low[v] = min(low[v], dfn[u]);
			}
		}
		if (low[v] == dfn[v]) {
			for (++scc; stk.top() != v; stk.pop()) {
				ins[stk.top()] = false, col[stk.top()] = scc;
			}
			ins[v] = false, col[v] = scc, stk.pop();
		}
	}
	bool solve(int num) {
		for (int i = 1; i <= num; i++) {
			if (!dfn[i]) {
				tarjan(i);
			}
		}
		vector<vector<int>> pts(scc + 1);
		for (int i = 1; i <= num; i++) {
			pts[col[i]].emplace_back(i);
		}
		vector<vector<int>> to(scc + 1), fr(scc + 1);
		for (int v = 0; v < n; v++) {
			for (auto u : e[v]) {
				if (col[u] && col[v] && col[u] != col[v]) {
					to[col[v]].emplace_back(col[u]);
					fr[col[u]].emplace_back(col[v]);
				}
			}
		}
		int sufsum = 0;
		vector<char> ans(num + 1, 'R');
		bool tag = false;
		for (int i = scc; i >= 1; i--) {
			if ((int)pts[i].size() >= num / 3) {
				vector<int> vis(scc + 1);
				auto init = [&]() {
					fill(vis.begin(), vis.end(), false);
					return true;
				};
				auto count = [&](auto &&self, int v, vector<vector<int>> &e) -> int {
					int res = 0;
					if (!vis[v]) {
						vis[v] = true, res = pts[v].size();
						for (auto u : e[v]) {
							res += self(self, u, e);
						}
					}
					return res;
				};
				if (init() && count(count, i, to) <= num / 3 * 2) {
					for (int i = 1; i <= scc; i++) {
						if (vis[i]) {
							for (auto id : pts[i]) {
								ans[id] = 'F';
							}
						}
					}
				} else if (init() && count(count, i, fr) <= num / 3 * 2) {
					fill(ans.begin(), ans.end(), 'F');
					for (int i = 1; i <= scc; i++) {
						if (vis[i]) {
							for (auto id : pts[i]) {
								ans[id] = 'R';
							}
						}
					}
				} else {
					return false;
				}
				tag = true;
				break;
			}
			sufsum += pts[i].size();
			if (sufsum >= num / 3 && sufsum <= num / 3 * 2) {
				for (int j = i; j <= scc; j++) {
					for (auto id : pts[j]) {
						ans[id] = 'F';
					}
				}
				tag = true;
				break;
			}
		}
		assert(tag);
		cout << "TAK\n";
		for (int i = 1; i <= num; i++) {
			cout << ans[i];
		}
		cout << '\n';
		return true;
	}
};

struct Segt {
	const int n, m, diff;
	Graph g;
	Segt(int _n, int _m) : n(_n), m(_m), diff(n + m - 1), g((n * 2) * 4 + n + m + 1) {
		build(1, 1, n);
	}
	void addin(int al, int ar, int p) {
		addin(al, ar, p, 1, 1, n);
	}
	void addout(int al, int ar, int p) {
		addout(al, ar, p, 1, 1, n);
	}
	#define mid ((l + r) >> 1)
	#define ls (v << 1)
	#define rs (v << 1 | 1)
	int in(int v) {
		return (v << 1) + diff;
	}
	int out(int v) {
		return (v << 1 | 1) + diff;
	}
	void build(int v, int l, int r) {
		if (l == r) {
			g.addedge(in(v), l);
			g.addedge(l, out(v));
		} else {
			build(ls, l, mid);
			build(rs, mid + 1, r);
			g.addedge(in(v), in(ls)), g.addedge(in(v), in(rs));
			g.addedge(out(ls), out(v)), g.addedge(out(rs), out(v));
		}
	}
	void addout(int al, int ar, int p, int v, int l, int r) {
		if (ar < l || al > r) {
			return;
		}
		if (al <= l && ar >= r) {
			g.addedge(out(v), p);
		} else {
			addout(al, ar, p, ls, l, mid);
			addout(al, ar, p, rs, mid + 1, r);
		}
	}
	void addin(int al, int ar, int p, int v, int l, int r) {
		if (ar < l || al > r) {
			return;
		}
		if (al <= l && ar >= r) {
			g.addedge(p, in(v));
		} else {
			addin(al, ar, p, ls, l, mid);
			addin(al, ar, p, rs, mid + 1, r);
		}
	}
	#undef rs
	#undef ls
	#undef mid
	bool solve() {
		return g.solve(n);
	}
};

bool solve() {
	int n, m;
	cin >> n >> m;
	Segt tr(n * 3, m);
	for (int i = 1; i <= m; i++) {
		int a, b, c, d;
		cin >> a >> b >> c >> d;
		tr.addout(a, b, n * 3 + i);
		tr.addin(c, d, n * 3 + i);
	}
	return tr.solve();
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr), cout.tie(nullptr);
	int T;
	cin >> T;
	while (T--) {
		if (!solve()) {
			cout << "NIE\n";
		}
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3624kb

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
RFF
NIE

result:

ok good!

Test #2:

score: 0
Accepted
time: 2229ms
memory: 94452kb

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: -100
Wrong Answer
time: 2627ms
memory: 120360kb

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
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR...

result:

wrong answer odpowiedz nie spelnia wymogow!