QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#470288#5504. Flower GardenYansuan_HClAC ✓2586ms116492kbC++144.5kb2024-07-10 11:45:392024-07-10 11:45:40

Judging History

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

  • [2024-07-10 11:45:40]
  • 评测
  • 测评结果:AC
  • 用时:2586ms
  • 内存:116492kb
  • [2024-07-10 11:45:39]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define U(i,l,r) for (int i(l),END##i(r); i<=END##i; ++i)
#define D(i,l,r) for (int i(l),END##i(r); i>=END##i; --i)
#define ms(x, v) memset(x, v, sizeof(x))
#define il __attribute__((always_inline))
#define vc vector
#define ar array
#define pb push_back
#define eb emplace_back
#define el '\n'
using ll = long long;

const int N = 33335, M = 100005, V = N * 3 * 3 + M;
int n, Q, m; vc<int> g[V];
int ptr;

#define Assert(x) if (!(x)) { cout << __LINE__ << endl; exit(0); }
struct segt {
	#define mid ((l + r) >> 1)
	#define ls (p << 1)
	#define rs (ls | 1)
	#define ARG int p = 1, int l = 1, int r = m
	vc<int> leaf; int bias; bool flag;
	vc<ar<int, 2>> ch; vc<int> pt;
	
	int build(ARG) {
		int &u = pt[p]; pt[p] = ++ptr;
		if (l == r) {
			--ptr;
			pt[p] = leaf[l] = bias + l;
			return pt[p];
		}
		ch[p][0] = build(ls, l, mid);
		ch[p][1] = build(rs, mid + 1, r);
		if (flag) {
			g[ch[p][0]].pb(u);
			g[ch[p][1]].pb(u);
		} else {
			g[u].resize(2);
			g[u][0] = ch[p][0];
			g[u][1] = ch[p][1];
		}
		return u;
	}
	
	segt(bool _f, int _b) : bias(_b), flag(_f) {
		leaf = vc<int>(m + 1);
		ch = vc<ar<int, 2>>(m * 4 + 1);
		pt = vc<int>(m * 4 + 1);
		build();
	}
	void connect(int b, int e, int u, ARG) {
		if (b <= l && e >= r) {
			if (flag) g[pt[p]].pb(u);
			else g[u].pb(pt[p]);
			return;
		}
		if (b <= mid) connect(b, e, u, ls, l, mid);
		if (e > mid) connect(b, e, u, rs, mid + 1, r);
	}
};

int dfn[V], low[V], ins[V], stk[V], sp, col[V], dfp, cp, siz[V];
vc<int> scc[V];
void tarjan(int u) {
	dfn[u] = low[u] = ++dfp;
	ins[u] = 1; stk[++sp] = u;
//	clog << "!" << u << endl;
	Assert(g[2].size());
	for (int v : g[u])
		if (!dfn[v]) {
			tarjan(v);
			low[u] = min(low[u], low[v]);
		} else if (ins[v]) {
			low[u] = min(low[u], dfn[v]);
		}
	if (dfn[u] == low[u]) {
		int c = ++cp;
		Assert(scc[c].empty());
		Assert(!siz[c]);
		do {
			col[stk[sp]] = c;
			scc[c].pb(stk[sp]);
			siz[c] += (stk[sp] <= m);
			ins[stk[sp]] = 0;
		} while (stk[sp--] != u);
	}
}

bool cons() {
	int c1 = 0; vc<int> vis(m + 1);
	U (i, 1, cp) {
		c1 += siz[i]; int test = 0;
		for (int v : scc[i]) if (v <= m) {
			vis[v] = 1;
			++test;
		}
		Assert(test == siz[i]);
		if (c1 >= n)
			break;
	}
	if (c1 > n * 2) return 0;
	cout << "TAK\n";
	U (i, 1, m)
		cout << ("RF"[vis[i]]);
	cout << el;
	return 1;
}
void large() {
	vc<vc<int>> h(cp + 1);
	U (u, 1, ptr) for (int v : g[u]) if (col[u] != col[v])
		h[col[u]].pb(col[v]);
	U (i, 1, cp) if (siz[i] > n) {
		vc<int> vip(m + 1), vic(cp + 1);
		queue<int> q; vic[i] = 1; q.push(i);
		int c1 = 0;
		while (q.size()) {
			int x = q.front(); q.pop();
			for (int u : scc[x]) if (u <= m)
				vip[u] = 1, ++c1;
			for (int y : h[x]) if (!vic[y]) {
				vic[y] = 1;
				q.push(y);
			}
		}
		if (c1 <= n * 2) {
			cout << "TAK" << el;
			int test = 0;
			U (u, 1, m) {
				cout << ("RF"[vip[u]]);
				test += vip[u];
			}
			Assert(test >= n && test <= n * 2);
			cout << el;
			return;
		}
	}
	h = vc<vc<int>>(cp + 1);
	vc<int> in(cp + 1), vip(m + 1);
	U (u, 1, ptr) for (int v : g[u]) if (col[u] != col[v]) {
		h[col[v]].pb(col[u]);
		++in[col[u]];
	}
	queue<int> q;
	U (i, 1, cp) if (!in[i] && siz[i] <= n)
		q.push(i);
	int c1 = 0;
	while (q.size()) {
		int x = q.front(); q.pop();
		for (int u : scc[x]) if (u <= m)
			vip[u] = 1, ++c1;
		if (c1 >= n) break;
		for (int y : h[x]) if (!(--in[y]) && siz[y] <= n)
			q.push(y);
	}
	if (c1 >= n) {
		cout << "TAK" << el;
		int test = 0;
		U (u, 1, m) {
			cout << ("RF"[vip[u]]);
			test += vip[u];
		}
		Assert(test == c1);
		Assert(test >= n);
		Assert(test <= n * 2);
		cout << el;
		return;
	}
	
	cout << "NIE" << el;
}

void solve() {
	cin >> n >> Q;
	m = ptr = n * 3;
	segt si1(0, 0), so1(1, 0);
//	clog << "fin" << endl;
	
	U (i, 1, Q) {
		int a, b, c, d, p; cin >> a >> b >> c >> d; p = ++ptr;
		so1.connect(a, b, p);
		si1.connect(c, d, p);
	}
	Assert(g[2].size());
	U (i, 1, ptr) if (!dfn[i])
		tarjan(i);
	
	bool f = cons();
	if (!f)
		large();
}

void clear() {
	U (i, 1, ptr) g[i].clear();
	U (i, 1, dfp) 
		dfn[i] = low[i] = ins[i] = stk[i] = col[i] = 0;
	U (i, 1, cp) scc[i].clear(), siz[i] = 0;
	dfp = cp = sp = 0;
}

int main() {
//	freopen("arrebol.in", "r", stdin);
//	freopen(".out", "w", stdout);
//	ios::sync_with_stdio(0);
	int t; cin >> t;
	while (t--) {
		solve();
		clear();
		cout << flush << endl;
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 0
Accepted
time: 2586ms
memory: 116492kb

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: 0
Accepted
time: 1995ms
memory: 99132kb

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

result:

ok good!

Test #4:

score: 0
Accepted
time: 1993ms
memory: 98684kb

input:

10
33333 100000
2646 2646 6430 6446
82226 82231 15128 15132
877 877 85831 88474
51389 79196 37573 37573
38030 38030 14248 14280
63032 68489 81074 81074
46468 46468 7403 7487
19864 20058 97979 97979
71640 71833 8558 8558
12121 12434 82481 82481
32901 32901 1899 2162
65312 77886 75969 75974
87983 8798...

output:

TAK
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFRRFFFFRFFRRFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF...

result:

ok good!

Test #5:

score: 0
Accepted
time: 659ms
memory: 27724kb

input:

87005
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
1 1
1 3 2 3
1 2
1 1 2 2
2 2 1 1
1 2
1 3 1 1
1 1 1 3
4 20
3 5 6 12
4 5 7 11
1 1 2 2
3 4 7 12
3 5 10 10
3 5 8 8
4 4 9 11
4 4 7 7
1 1 9 10
3 4 6 9
3 5 11 12
3 3 7 9
3 5 2 2
4 5 2 2
1 1 7 11
1 1 10 10
3 5 7 8
4 4 2 2
1 1 2 2
4 5 8 10
4 12
11 ...

output:

TAK
RRF

NIE

TAK
RFF

TAK
FFR

NIE

TAK
RFRRRRFRFFRR

TAK
FFRRFFRRRRRR

NIE

TAK
FFFFRRRRRFRRRRR

TAK
FRRRRRRRFFFR

TAK
FFFRRRRRRRRF

TAK
FFFFFRRRRRRRRRR

TAK
FFRRRRRRRRFF

TAK
FFRRFFFRRRRRRRR

NIE

TAK
FFFFRRRRRRRR

NIE

TAK
FRFFFRRFF

NIE

TAK
FFFRRRRRR

TAK
RRRRRRFFFRRF

TAK
RRFFRFRFFFFFRFF

TAK...

result:

ok good!

Test #6:

score: 0
Accepted
time: 1786ms
memory: 102708kb

input:

10
33333 99999
60859 99999 1 60858
78724 99999 1 78723
42986 99999 1 42985
35108 99999 1 35107
63158 99999 1 63157
86977 99999 1 86976
13576 99999 1 13575
48277 99999 1 48276
89102 99999 1 89101
92657 99999 1 92656
25093 99999 1 25092
2353 99999 1 2352
81748 99999 1 81747
51216 99999 1 51215
75815 9...

output:

NIE

TAK
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR...

result:

ok good!