QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#470271#5504. Flower GardenYansuan_HClWA 0ms26196kbC++144.4kb2024-07-10 11:41:012024-07-10 11:41:01

Judging History

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

  • [2024-07-10 11:41:01]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:26196kb
  • [2024-07-10 11:41:01]
  • 提交

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[N];
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(!siz[c]);
		Assert(scc[c].empty());
		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])
		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 >= n && 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();
		exit(0);
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 26196kb

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

result:

wrong output format Unexpected end of file - token expected