QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#570130 | #9319. Bull Farm | UESTC_OldEastWest | WA | 90ms | 3856kb | C++17 | 5.5kb | 2024-09-17 14:06:42 | 2024-09-17 14:06:44 |
Judging History
answer
#include <bits/stdc++.h>
const int N = 2048;
void charming() {
int n, l, q; std::cin >> n >> l >> q;
std::vector move(l, std::vector<int> (n));
std::function<int(std::string)> decoder = [&](std::string s) {
int x = s[0] - '0', y = s[1] - '0';
return x * 50 + y;
};
for (int i = 0; i < l; ++i) {
std::string s; std::cin >> s;
for (int j = 0; j < s.size(); j += 2) {
int x = decoder(s.substr(j, 2));
move[i][j / 2] = x, --move[i][j / 2];
}
}
std::vector<int> ans(q);
std::vector qry(l + 1, std::vector<std::array<int, 3> > ());
for (int i = 0, a, b, c; i < q; ++i) {
std::string word; std::cin >> word;
a = decoder(word.substr(0, 2)) - 1;
b = decoder(word.substr(2, 2)) - 1;
c = decoder(word.substr(4, 2)) - 1;
if (a == b) ans[i] = 1;
else if (c >= 0) qry[c].emplace_back((std::array<int, 3>) {a, b, i});
}
std::vector<int> pre(n);
std::iota(pre.begin(), pre.end(), 0);
std::vector G(n, std::vector<int> ());
std::function<int(int)> find = [&](int x) {
if (x == pre[x]) return x;
else return pre[x] = find(pre[x]);
};
std::function<void(int, int&, std::vector<int>&, std::vector<int>&, std::vector<int>&, std::vector<int>&)> DFS =
[&](int u, int &tot, std::vector<int> &dfn, std::vector<int> &low, std::vector<int> &stk, std::vector<int> &pre) {
dfn[u] = low[u] = ++tot;
stk.emplace_back(u);
for (int v : G[u]) {
if (!dfn[v]) {
DFS(v, tot, dfn, low, stk, pre);
low[u] = std::min(low[u], low[v]);
}
else low[u] = std::min(low[u], dfn[v]);
}
if (low[u] >= dfn[u]) {
// std::cout << "Scc: " << u << '\n';
// for (auto x : stk) std::cout << x << ' ';
// std::cout << '\n';
while (stk.back() != u) {
assert(!stk.empty());
pre[stk.back()] = u;
stk.pop_back();
}
pre[u] = u;
stk.pop_back();
}
};
std::function<std::vector<int>(std::vector<int>&)> Tarjan = [&](std::vector<int> &pre) {
// std::cout << "Before tarjan.\n";
// for (int u = 0; u < n; ++u) {
// for (int v : G[u]) std::cout << u << ' ' << v << '\n';
// }
std::vector<int> new_pre(n);
std::vector<int> dfn(n), low(n);
int tot = 0;
for (int i = 0; i < n; ++i) if (!dfn[i] && i == find(i)) {
std::vector<int> stk;
DFS(i, tot, dfn, low, stk, new_pre);
}
return new_pre;
};
std::function<std::vector<std::vector<int> > ()> Shrink = [&]() {
std::vector nG(n, std::vector<int> ());
for (int u = 0, fu, fv; u < n; ++u) if (u == find(u)) {
fu = find(u);
for (int v : G[u]) {
fv = find(v);
if (fu != fv) nG[fu].emplace_back(fv);
}
}
return nG;
};
std::function<void(std::vector<int>&, std::vector<std::array<int, 3> >&)> Solve
= [&](std::vector<int> &ans, std::vector<std::array<int, 3> > &qry) {
if (!qry.size()) return;
std::vector<std::bitset<N> > f(n);
std::vector<int> du(n);
std::vector revG(n, std::vector<int>());
for (int u = 0; u < n; ++u) if (u == find(u)) {
f[u][u] = 1;
for (int v : G[u]) ++du[u], revG[v].emplace_back(u);
}
std::vector<int> q;
int head = 0, tail = 0;
for (int u = 0; u < n; ++u) if (u == find(u) && !du[u]) {
q.emplace_back(u), ++tail;
}
while (head < tail) {
int u = q[head++];
// std::cout << u << '\n';
for (int v : revG[u]) {
--du[v];
f[v] |= f[u];
if (!du[v]) q.emplace_back(v), ++tail;
}
}
for (auto [u, v, q_idx] : qry) {
int fu = find(u), fv = find(v);
if (f[fu].test(fv)) ans[q_idx] = 1;
else ans[q_idx] = 0;
}
// std::cout << "Check\n";
// for (int u = 0; u < n; ++u) {
// for (int v : G[u]) {
// std::cout << u << ' ' << v << '\n';
// }
// }
// for (int u = 0; u < n; ++u) if (u == find(u)) {
// std::cout << u << ": ";
// for (int v = 0; v < n; ++v) std::cout << f[u][v];
// std::cout << '\n';
// }
};
for (int i = 0; i < l; ++i) {
std::vector<int> vis(n);
std::unordered_set<int> s;
for (int j = 1; j < n; ++j) assert(move[i][j] < n), ++vis[move[i][j]];
for (int j = 0; j < n; ++j) if (!vis[j]) s.insert(j);
for (int u = 0, fu, fv; u < n; ++u) {
fu = find(u);
if (int(s.size()) == 1) {
int v = *s.begin();
fv = find(v);
if (fu != fv) G[fu].emplace_back(fv); // std::cout << "Add edge " << u << ' ' << v << '\n';
}
int nxt0 = move[i][u];
++vis[nxt0];
if (vis[nxt0] == 1) assert(s.count(nxt0)), s.erase(nxt0);
if (u < n - 1) {
int nxt1 = move[i][u + 1];
--vis[nxt1];
if (!vis[nxt1]) s.insert(nxt1);
}
}
pre = Tarjan(pre);
// std::cout << "Before Shrink\n";
// for (int u = 0; u < n; ++u) std::cout << find(u) << " \n"[u == n - 1];
G = Shrink();
// std::cout << "After Shrink \n";
// for (int u = 0; u < n; ++u) {
// std::cout << u << ' ' << find(u) << '\n';
// if (u == find(u)) {
// for (int v : G[u]) {
// std::cout << "- " << u << ' ' << v << '\n';
// }
// }
// }
Solve(ans, qry[i]);
}
for (int i = 0; i < q; ++i) std::cout << ans[i];
std::cout << '\n';
}
signed main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
int t; std::cin >> t;
while (t--) charming();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3660kb
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: 3856kb
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: 90ms
memory: 3732kb
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:
011110001101011111111111111111111101111111110111111110111110111011010111111111111111111100011111111110111111110111111111111111111111111111111111101111111111110001110101111110111111111111111011101111111111111111111111111111111111111111011011110100111110111011110111111100111111101110111111111101111110...
result:
wrong answer 1st lines differ - expected: '011110001101101111111111111111...1111111111111111101111111111111', found: '011110001101011111111111111111...1111111111111111101111111011111'