QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#566408 | #9319. Bull Farm | lllei | WA | 53ms | 3968kb | C++20 | 6.1kb | 2024-09-16 00:02:59 | 2024-09-16 00:02:59 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
int n, l, q;
int calc(char a, char b) {
return (a - 48) * 50 + (b - 48);
}
using B = bitset<2001>;
int main() {
cin.tie(0);
ios::sync_with_stdio(0);
int T;
cin >> T;
while (T--) {
cin >> n >> l >> q;
vector<int> du(n + 1);
vector<int> pre(n + 1);
vector<string> s(l + 1);
vector<vector<array<int, 3>>> c(l + 1);
for (int i = 1; i <= l; i++) {
cin >> s[i];
}
for (int i = 1; i <= q; i++) {
string t;
cin >> t;
c[calc(t[4], t[5])].push_back({calc(t[0], t[1]), calc(t[2], t[3]), i});
}
vector<int> ans(q + 1);
vector<vector<int>> e(n + 1);
vector<int> id(n + 1);
iota(id.begin(), id.end(), 0);
vector<int> nxt(n + 1);
for (auto [x, y, id] : c[0]) {
if (x == y) {
ans[id] = 1;
} else {
ans[id] = 0;
}
}
for (int i = 1; i <= l; i++) {
for (int j = 1; j <= n; j++) {
du[j] = 0;
pre[j] = 0;
nxt[j] = 0;
}
for (int j = 0; j < n; j++) {
int v = j + 1, u = calc(s[i][j * 2], s[i][j * 2 + 1]);
du[u]++;
pre[u] = v;
nxt[v] = u;
}
int cnt = 0;
for (int j = 1; j <= n; j++) {
if (du[j] == 0) {
cnt++;
}
}
vector<bool> vis(n + 1);
vector<vector<int>> ee(n + 1);
vector<int> dfn(n + 1), low(n + 1);
int cntt = 0;
vector<int> stk(n + 1);
int top = 0;
int tot = 0;
vector<int> idd(n + 1);
auto tarjan = [&](auto &&self, int u) -> void {
dfn[u] = low[u] = ++cntt;
vis[u] = true;
stk[++top] = u;
for (int v : e[u]) {
if (!dfn[v]) {
self(self, v);
low[u] =min(low[u], low[v]);
}
if (vis[v]) {
low[u] = min(low[u], dfn[v]);
}
}
if (low[u] == dfn[u]) {
tot++;
int x;
do {
x = stk[top];
vis[x] = false;
top--;
idd[x] = tot;
} while (x != u);
}
};
if (cnt == 0) {
for (int i = 1; i <= n; ++i) {
int t = pre[i];
if (id[t] != id[i]) {
e[id[t]].push_back(id[i]);
}
}
} else if (cnt == 1) {
int u = -1, v = -1;
for (int i = 1; i <= n; ++i) {
if (du[i] == 0) {
u = i;
} else if (du[i] == 2) {
v = i;
}
}
int x = v;
while (nxt[v] != x) {
v = nxt[v];
}
if (id[v] != id[u]) {
e[id[u]].push_back(id[v]);
}
int t1 = u;
while (nxt[t1] != x) {
t1 = nxt[t1];
}
if (id[t1] != id[u]) {
e[id[u]].push_back(id[t1]);
}
} else {
}
for (int i = 1; i <= n; ++i) {
if (!dfn[id[i]]) {
tarjan(tarjan, id[i]);
}
}
vis.assign(n + 1, 0);
for (int i = 1; i <= n; ++i) {
if (i != id[i]) {
continue;
}
int u = i;
for (int v : e[i]) {
int t1 = idd[u];
int t2 = idd[v];
if (t1 != t2 && !vis[t2]) {
ee[t1].push_back(t2);
vis[t2] = true;
}
}
for (int v : e[i]) {
int t2 = idd[v];
vis[t2] = false;
}
}
e = move(ee);
for (int i = 1; i <= n; ++i) {
id[i] = idd[id[i]];
assert(id[i] != 0);
}
vector<B> b(n + 1);
vector<int> dd(n + 1);
for (int i = 1; i <= n; ++i) {
if (id[i] == i) {
b[i][i] = true;
for (int v : e[i]) {
dd[v]++;
}
}
}
queue<int> q;
for (int i = 1; i <= n; ++i) {
if (id[i] == i && dd[i] == 0) {
q.push(i);
}
}
while (q.size()) {
int u = q.front();
q.pop();
for (int v : e[u]) {
b[v] |= b[u];
dd[v]--;
if (dd[v] == 0) {
q.push(v);
}
}
}
for (auto [u, v, idd] : c[i]) {
u = id[u];
v = id[v];
if (b[u][v]) {
ans[idd] = 1;
} else {
ans[idd] = 0;
}
}
}
for (int i = 1; i <= q; ++i) {
cout << ans[i];
}
cout << '\n';
}
}
/*
1
6 2 4
030603010601
010203060504
030202
060402
050602
060401
1
3 3 2
020202
030301
030201
020303
010202
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3852kb
input:
2 5 2 4 0305040201 0404040404 030300 020500 050102 020501 6 2 4 030603010601 010203060504 030202 060402 050602 060401
output:
1011 0100
result:
ok 2 lines
Test #2:
score: 0
Accepted
time: 0ms
memory: 3880kb
input:
1 3 3 6 020202 030301 030201 020102 030203 010201 010303 020303 010202
output:
010101
result:
ok single line: '010101'
Test #3:
score: -100
Wrong Answer
time: 53ms
memory: 3968kb
input:
200 10 10 5000 01060:04020305080709 0103070:060204050908 09070503080401060:02 050308010204090:0607 03010502040607080:09 03080109020504060:07 06050:09040302080107 07080305010409060:02 030809010:0204060507 0:060908070201050304 060700 090:03 09080: 070405 010703 0:0100 080601 030600 070206 0:0:09 08040...
output:
011110001101101111111111111111111101111111110111011110110110111011010111111111111111111101111111111110111111110111111111111101111111111110111111111111111111110001100111111111111111111111111011101111111111111111111111111111111111111111011011110100111110111111110111111100111111101110111111111101111110...
result:
wrong answer 1st lines differ - expected: '011110001101101111111111111111...1111111111111111101111111111111', found: '011110001101101111111111111111...1111111111111111101111111111111'