QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#584301 | #9319. Bull Farm | supepapupu | ML | 387ms | 439720kb | C++20 | 5.8kb | 2024-09-23 11:44:53 | 2024-09-23 11:44:53 |
Judging History
answer
#include <bits/stdc++.h>
#define x first
#define y second
#define el '\n'
#define debug(x) cerr << #x << ": " << x << endl
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int N = 3e5 + 10, INF = 0x3f3f3f3f, mod = 998244353;
struct DSU {
vector<int> fa;
DSU(int n = 0) {
fa.resize(n + 1);
iota(fa.begin() + 1, fa.end(), 1);
}
int find(int x) {
return fa[x] == x ? x : fa[x] = find(fa[x]);
}
};
struct Bitset {
int len;
vector<ll> w;
Bitset(int n = 0) {
len = (n + 64) / 64;
w.resize(len);
}
void set(int x) {
w[x / 64] |= 1ll << x % 64;
}
int get(int x) {
return w[x / 64] >> x % 64 & 1;
}
};
void solve() {
int n, l, q; cin >> n >> l >> q;
vector<vector<Bitset>> bts;
vector<vector<int>> g(n + 1), ids;
vector<int> dfn, low, stk, ins, id(n + 1);
for (int j = 1; j <= n; ++j) id[j] = j;
vector<vector<int>> scc;
int ts, top, scc_cnt = n;
function<void(int)> tarjan = [&](int u) {
dfn[u] = low[u] = ++ts;
stk[++top] = u, ins[u] = 1;
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]) {
++scc_cnt;
int y;
do {
y = stk[top--];
ins[y] = 0;
id[y] = scc_cnt;
scc[scc_cnt].emplace_back(y);
} while (y != u);
}
};
vector<pii> edges;
for (int i = 0; i < l; ++i) {
string s; cin >> s;
vector<int> a(n + 1), deg(n + 1);
int t = 0;
for (int j = 0, k = 1; j < n * 2; j += 2, ++k) {
a[k] = (s[j] - '0') * 50 + s[j + 1] - '0';
++deg[a[k]];
t += deg[a[k]] > 1;
}
if (t >= 2) {
if (!i) {
vector<Bitset> Bt;
Bitset bt(scc_cnt + 1);
for (int j = 0; j <= scc_cnt; ++j) {
Bt.emplace_back(bt);
if (j) Bt[j].set(j);
}
ids.emplace_back(id);
bts.emplace_back(Bt);
continue;
}
ids.emplace_back(ids.back());
bts.emplace_back(bts.back());
continue;
}
g = vector<vector<int>>(n + 1);
if (!t) {
for (int j = 1; j <= n; ++j) {
if (id[j] != id[a[j]])
g[id[j]].emplace_back(id[a[j]]);
// debug(j);
// debug(a[j]);
// debug(id[j]);
// debug(id[a[j]]);
}
} else {
int x;
for (int j = 1; j <= n; ++j) {
if (!deg[j]) {
x = j;
break;
}
}
int y = x;
for (;;) {
int p = a[y];
if (deg[p] > 1) break;
y = p;
}
if (id[y] != id[x]) edges.emplace_back(id[y], id[x]);
y = a[y];
int z = y;
do {
int p = a[z];
if (p == y) break;
// g[id[z]].emplace_back(id[p]);
z = p;
} while (1);
y = z;
// g[id[x]].emplace_back(id[y]);
if (id[y] != id[x]) edges.emplace_back(id[y], id[x]);
// debug(i);
// debug(x), debug(y);
// debug(id[x]), debug(id[y]);
}
for (auto[x, y]: edges) g[x].emplace_back(y);
// continue;
dfn = vector<int>(n + 1), low = vector<int>(n + 1), stk = vector<int>(n + 1), ins = vector<int>(n + 1), id = vector<int>(n + 1);
scc = vector<vector<int>>(n + 1);
int sc = scc_cnt;
ts = top = scc_cnt = 0;
for (int j = 1; j <= sc; ++j) {
if (!dfn[j]) tarjan(j);
}
vector<Bitset> Bt;
Bitset bt(scc_cnt + 1);
for (int j = 0; j <= scc_cnt; ++j) Bt.emplace_back(bt);
// for (int j = scc_cnt; j; --j) {
// debug(i);
for (int j = 1; j <= scc_cnt; ++j) {
Bt[j].set(j);
// debug(j);
set<int> S;
for (int u: scc[j]) {
// debug(u);
// for (int o = 1; o <= sc; ++o) {
// if (i) {
// if (bts[i - 1][u].get(o)) Bt[id[u]].set(id[o]);
// }
// }
for (int v: g[u]) {
// debug(v);
// debug(id[v]);
if (S.count(id[v])) continue;
if (id[v] != j) {
S.emplace(id[v]);
for (int o = 0; o < bt.len; ++o) Bt[j].w[o] |= Bt[id[v]].w[o];
}
}
}
}
// if (i) debug(Bt[2].get(1));
// for (int j = 1; j <= sc; ++j) cout << id[j] << " \n"[j == sc];
vector<int> nid = id;
for (int j = 1; j <= n; ++j) {
if (i) {
// debug(ids[i - 1][j]);
nid[j] = id[ids[i - 1][j]];
}
}
vector<pii> ee;
for (auto[x, y]: edges) {
x = id[x], y = id[y];
if (x != y) ee.emplace_back(x, y);
}
edges = move(ee);
// debug(i);
id = move(nid);
// for (int j = 1; j <= n; ++j) cout << id[j] << " \n"[j == n];
ids.emplace_back(id);
bts.emplace_back(Bt);
}
while (q--) {
string s; cin >> s;
vector<int> v(3);
for (int j = 0, k = 0; k < 3; j += 2, ++k) {
v[k] = (s[j] - '0') * 50 + s[j + 1] - '0';
}
int a = v[0], b = v[1], c = v[2] - 1;
if (c == -1) {
if (a == b) cout << '1';
else cout << '0';
continue;
}
// debug(a), debug(b);
// debug(c);
// debug(ids.size());
// debug(a), debug(b), debug(c);
// debug(ids[c][a]), debug(ids[c][b]);
// continue;
if (bts[c][ids[c][a]].get(ids[c][b])) cout << '1';
else cout << '0';
}
cout << el;
}
// void solve() {
// int n, l, q; cin >> n >> l >> q;
// vector<DSU> dsu(l);
// vector<vector<vector<int>>> g(l);
// for (int i = 0; i < l; ++i) {
// if (i) dsu[i] = dsu[i - 1];
// string s; cin >> s;
// vector<int> a(n + 1), deg(n + 1);
// int t = 0;
// for (int j = 0, k = 1; j < n * 2; j += 2, ++k) {
// a[k] = (s[j] - '0') * 50 + s[j + 1] - '0';
// ++deg[a[k]];
// t += deg[a[k]] > 1;
// }
// if (t >= 2) continue;
// if (!t) {
// vector<bool> st(n + 1);
// for (int j = 1; j <= n; ++j) {
// if (st[j]) continue;
// int x = j;
// vector<int> v;
// do {
// int u = dsu[i].find(x), v = dsu[i].find(j);
// dsu[i].fa[u] = v;
// st[x] = 1;
// x = a[x];
// } while (x != j);
// }
// }
// }
// }
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int tcase = 1;
cin >> tcase;
while (tcase--) solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3544kb
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: 3536kb
input:
1 3 3 6 020202 030301 030201 020102 030203 010201 010303 020303 010202
output:
010101
result:
ok single line: '010101'
Test #3:
score: 0
Accepted
time: 46ms
memory: 3776kb
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:
ok 200 lines
Test #4:
score: 0
Accepted
time: 52ms
memory: 3920kb
input:
1 2000 1 1000000 M=:]A@8UAY7W2JJ4KEHIA[HSCQ1ENSC`JXR;F3PJ:_@41P9Z=9HR8P<<:DUXRR9^WOQFL?NZP6S@=J0^WE32=6;\U0?88]Q_RNPUMT6YU<4<S]H?:7OCQYOT4YAV1^764ENWSDBED>M7A:BI>KSIR48JQ9B=N\5T3N4A2aF0@>3TI81<G7;YE>W`NMP<:IT4CI3D0=GZC3I\CLQJQBA9BDIS9SAM55KaVA<Z@D=>:Y?CQHUQ5U3a6UVI8OKX9_FAF^7=5M85;<0;8YPAM<7Z7PP7A=N...
output:
000101000101100010001000000010010110000001000001001100000000010000100001000000101100000000010000001000000001110000010110100000111100100000001000000000011001010001000001001000100000000100011001100110010000010000101100000011111000000010000101010010000000010101000001010111100000100000000000000101000100...
result:
ok single line: '000101000101100010001000000010...0101000101000000000010101001000'
Test #5:
score: 0
Accepted
time: 168ms
memory: 19608kb
input:
1 2000 2000 1000000 FVAaH7GRPO;_11Da5J18@3SMG==\G8E8S^6:M4L0JH>MN5IXT>2<7WJ3U8LVJS=;;3F13>0D0>VOIIU@EPHG6ABL6;K?T1PXDERLG07]5C9^GDKG<SBMIW;`4W8P3=469TIPKH0O34523_J5C2MJ17D25Z@=K8H@M>WK<CMK7EO]BPD7B6AW741J5YIHIa1:ERSG>L3N2^F3<4F`DLE@2AA5=8GZK6:192FB736[WMV7:^DA2C:<LK040VJBM3M]WXU50`407TR_?PLF@5VL7OSL...
output:
111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111...
result:
ok single line: '111111111111111111111111111111...1111111111111111111111111111111'
Test #6:
score: 0
Accepted
time: 368ms
memory: 439720kb
input:
1 2000 2000 1000000 0102030405060708090:0;0<0=0>0?0@0A0B0C0D0E0F0G0H0I0J0K0L0M0N0O0P0Q0R0S0T0U0V0W0X0Y0Z0[0\0]0^0_0`0a101112131415161718191:1;1<1=1>1?1@1A1B1C1D1E1F1G1H1I1J1K1L1M1N1O1P1Q1R1S1T1U1V1W1X1Y1Z1[1\1]1^1_1`1a202122232425262728292:2;2<2=2>2?2@2A2B2C2D2E2F2G2H2I2J2K2L2M2N2O2P2Q2R2S2T2U2V2W2X...
output:
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000...
result:
ok single line: '000000000000000000000000000000...0000000000000000000000000000000'
Test #7:
score: 0
Accepted
time: 387ms
memory: 439596kb
input:
1 2000 2000 1000000 0102030405060708090:0;0<0=0>0?0@0A0B0C0D0E0F0G0H0I0J0K0L0M0N0O0P0Q0R0S0T0U0V0W0X0Y0Z0[0\0]0^0_0`0a101112131415161718191:1;1<1=1>1?1@1A1B1C1D1E1F1G1H1I1J1K1L1M1N1O1P1Q1R1S1T1U1V1W1X1Y1Z1[1\1]1^1_1`1a202122232425262728292:2;2<2=2>2?2@2A2B2C2D2E2F2G2H2I2J2K2L2M2N2O2P2Q2R2S2T2U2V2W2X...
output:
010001100010000000101000000110010001001010101100100100000001000000101010100010000001011111010000000001001000010101011111001010101010100100010001011011000010010100110110000100010110101110000011111101100110010100100101010000100100101110100000000101100010100111000011001110100010001010000101111001101000...
result:
ok single line: '010001100010000000101000000110...1010101000010010101000100000111'
Test #8:
score: -100
Memory Limit Exceeded
input:
1 2000 2000 1000000 0102030405060708090:0;0<0=0>0?0@0A0B0C0D0E0F0G0H0I0J0K0L0M0N0O0P0Q0R0S0T0U0V0W0X0Y0Z0[0\0]0^0_0`0a101112131415161718191:1;1<1=1>1?1@1A1B1C1D1E1F1G1H1I1J1K1L1M1N1O1P1Q1R1S1T1U1V1W1X1Y1Z1[1\1]1^1_1`1a202122232425262728292:2;2<2=2>2?2@2A2B2C2D2E2F2G2H2I2J2K2L2M2N2O2P2Q2R2S2T2U2V2W2X...
output:
011010100000000000000000001000001100000110000000100001010000000000000010001000000100000000000000000000100000100000000001100000100011010000000000000000000000000010000001000000010001100010000100000001000000000010000000000000001000010100000000001000000000000000000000001100010000000000000101000000101010...