QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#179876 | #385. 游戏 | hos_lyric# | 100 ✓ | 21ms | 11512kb | C++14 | 5.1kb | 2023-09-15 11:50:12 | 2023-09-15 11:50:14 |
Judging History
answer
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using Int = long long;
template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")
struct Scc {
int n;
vector<int> as, bs;
vector<int> ptr, zu, us;
int l;
vector<int> ids;
int operator[](int u) const { return ids[u]; }
explicit Scc(int n_) : n(n_), as(), bs(), l(-1) {}
void ae(int u, int v) {
assert(0 <= u); assert(u < n);
assert(0 <= v); assert(v < n);
as.push_back(u);
bs.push_back(v);
}
void dfs0(int u) {
if (!ids[u]) {
ids[u] = -1;
for (int i = ptr[u]; i < ptr[u + 1]; ++i) dfs0(zu[i]);
us.push_back(u);
}
}
void dfs1(int u) {
if (!~ids[u]) {
ids[u] = l;
for (int i = ptr[u]; i < ptr[u + 1]; ++i) dfs1(zu[i]);
}
}
void run() {
const int m = as.size();
ptr.resize(n + 2);
zu.resize(m);
for (int u = 0; u < n + 2; ++u) ptr[u] = 0;
for (int i = 0; i < m; ++i) ++ptr[as[i] + 2];
for (int u = 2; u < n + 1; ++u) ptr[u + 1] += ptr[u];
for (int i = 0; i < m; ++i) zu[ptr[as[i] + 1]++] = bs[i];
ids.assign(n, 0);
us.clear();
for (int u = 0; u < n; ++u) dfs0(u);
for (int u = 0; u < n + 2; ++u) ptr[u] = 0;
for (int i = 0; i < m; ++i) ++ptr[bs[i] + 2];
for (int u = 2; u < n + 1; ++u) ptr[u + 1] += ptr[u];
for (int i = 0; i < m; ++i) zu[ptr[bs[i] + 1]++] = as[i];
l = 0;
for (int j = n; --j >= 0; ) if (!~ids[us[j]]) { dfs1(us[j]); ++l; }
}
vector<vector<int>> group() const {
assert(~l);
vector<vector<int>> uss(l);
for (int u = 0; u < n; ++u) uss[ids[u]].push_back(u);
return uss;
}
};
int N;
char S[100'010];
int M;
vector<int> U, V;
vector<char> P, Q;
int main() {
for (; ~scanf("%d%*d", &N); ) {
scanf("%s", S);
scanf("%d", &M);
U.resize(M);
V.resize(M);
P.resize(M);
Q.resize(M);
for (int i = 0; i < M; ++i) {
scanf("%d %c%d %c", &U[i], &P[i], &V[i], &Q[i]);
--U[i];
--V[i];
}
vector<int> xs;
for (int u = 0; u < N; ++u) if (S[u] == 'x') {
xs.push_back(u);
}
const int xsLen = xs.size();
string ans = "-1";
for (int f = 0; f < 1 << xsLen; ++f) {
string s = S;
for (int j = 0; j < xsLen; ++j) {
s[xs[j]] = "aA"[f >> j & 1];
}
// cerr<<"s = "<<s<<endl;
// -1: true, -2: false
auto calc = [&](int u, char p) -> int {
if (s[u] == 'A') {
return (p == 'A') ? -1 : -2;
} else {
int d = (p - 'A') - (s[u] - 'a');
if (d < 0) d += 3;
return (d == 0) ? -2 : (d == 1) ? (u << 1) : (u << 1 | 1);
}
};
bool ok = true;
Scc scc(N << 1);
for (int i = 0; i < M; ++i) {
const int z = calc(U[i], P[i]);
const int w = calc(V[i], Q[i]);
if (z == -1) {
if (w == -2) {
ok = false;
break;
} else if (w >= 0) {
// w
scc.ae(w ^ 1, w);
}
} else if (z >= 0) {
if (w == -2) {
// not z
scc.ae(z, z ^ 1);
} else if (w >= 0) {
// z ==> w
scc.ae(z, w);
scc.ae(w ^ 1, z ^ 1);
}
}
}
if (ok) {
scc.run();
for (int u = 0; u < N; ++u) {
ok = ok && (scc[u << 1] != scc[u << 1 | 1]);
}
if (ok) {
ans = s;
for (int u = 0; u < N; ++u) if (s[u] != 'A') {
ans[u] = 'A' + ((s[u] - 'a') + ((scc[u << 1] < scc[u << 1 | 1]) ? 2 : 1)) % 3;
}
// judge
for (int u = 0; u < N; ++u) {
assert('A' <= ans[u]); assert(ans[u] <= 'C');
assert(ans[u] != toupper(S[u]));
}
for (int i = 0; i < M; ++i) {
if (ans[U[i]] == P[i]) {
assert(ans[V[i]] == Q[i]);
}
}
break;
}
}
}
puts(ans.c_str());
}
return 0;
}
详细
Test #1:
score: 5
Accepted
time: 1ms
memory: 3524kb
input:
2 0 ba 4 2 C 2 C 1 B 1 A 2 C 1 C 2 B 1 A
output:
CC
result:
ok
Test #2:
score: 5
Accepted
time: 1ms
memory: 3636kb
input:
2 1 xc 4 2 B 2 A 2 A 1 C 2 C 2 C 2 B 1 A
output:
CA
result:
ok
Test #3:
score: 5
Accepted
time: 0ms
memory: 3684kb
input:
5 0 bcbbb 10 1 B 1 C 3 C 1 C 2 B 5 A 1 C 1 C 2 A 5 A 3 A 1 C 2 B 1 C 3 A 1 B 1 B 4 B 1 B 4 A
output:
CACCA
result:
ok
Test #4:
score: 5
Accepted
time: 1ms
memory: 3516kb
input:
5 2 acxax 10 2 B 1 A 3 C 4 C 4 B 3 C 5 B 3 B 2 B 3 A 3 A 2 A 5 B 1 C 3 B 1 B 2 C 4 C 4 B 2 A
output:
BACCC
result:
ok
Test #5:
score: 5
Accepted
time: 1ms
memory: 3544kb
input:
10 0 abbbacabab 20 5 A 3 B 9 A 4 C 4 C 10 A 8 B 3 C 6 C 10 A 5 A 9 C 8 A 2 C 5 A 8 B 10 A 10 A 5 B 4 B 6 C 10 C 2 C 4 B 5 C 7 B 5 A 10 B 3 A 7 B 6 B 4 B 3 C 8 B 1 B 3 B 4 B 3 C 8 A 2 A
output:
CAACCABCBA
result:
ok
Test #6:
score: 5
Accepted
time: 1ms
memory: 3496kb
input:
10 4 xbbxccxbxc 20 2 A 10 A 10 C 4 C 1 A 5 A 2 A 2 A 9 C 2 B 2 B 6 C 2 B 9 A 3 B 2 B 4 B 9 A 4 A 5 A 2 B 10 A 3 B 6 A 8 C 8 B 3 B 3 B 3 B 7 A 9 C 6 C 6 A 8 C 4 A 10 A 2 C 4 B 9 C 7 B
output:
BACCABBABA
result:
ok
Test #7:
score: 5
Accepted
time: 0ms
memory: 3552kb
input:
20 0 cccccccccccccccccccc 40 15 A 13 C 6 B 19 A 3 C 5 B 20 C 6 C 1 C 10 B 10 C 7 C 13 B 17 A 1 A 3 A 15 A 15 A 13 B 18 B 14 A 4 A 1 A 3 A 4 C 14 A 1 B 6 B 2 B 8 B 3 C 15 A 1 B 17 B 6 A 17 A 12 B 6 A 8 B 1 C 15 B 1 B 18 C 3 C 2 A 8 A 14 B 6 B 15 C 9 A 17 A 10 C 15 A 7 A 8 C 6 A 18 B 12 C 14 C 10 A 9 ...
output:
BAABABAABAAAABBABAAA
result:
ok
Test #8:
score: 5
Accepted
time: 1ms
memory: 3640kb
input:
20 0 caaabacbacabbcacabaa 40 7 B 19 C 15 A 12 B 2 A 12 C 20 B 1 B 4 B 4 A 4 A 18 C 4 A 3 A 15 C 12 A 2 C 12 C 15 B 6 A 10 A 12 B 13 C 7 C 2 C 6 C 16 C 18 A 12 B 17 B 8 C 11 B 14 B 11 A 8 B 8 C 19 A 7 C 6 C 3 C 1 C 4 B 1 C 5 A 13 B 4 C 14 B 9 A 11 A 9 C 8 B 16 B 2 C 20 A 2 A 14 C 10 B 13 A 17 A 19 B ...
output:
BBCCABACBBBAAACABCCC
result:
ok
Test #9:
score: 5
Accepted
time: 1ms
memory: 3664kb
input:
20 8 xccccccxxcxcccccxxxx 40 12 A 18 C 16 C 14 C 10 B 2 C 1 A 10 A 11 A 13 B 15 A 6 B 8 A 14 B 16 A 15 B 4 C 5 C 19 C 4 B 17 C 11 A 12 B 14 C 18 B 17 B 14 A 11 C 16 C 6 C 13 C 20 B 16 B 13 C 8 B 7 C 9 B 16 B 1 B 18 A 7 B 6 A 8 B 19 A 16 C 17 A 8 A 6 A 10 A 14 B 20 C 6 B 13 C 5 A 14 A 3 C 20 B 14 C 4...
output:
CAABBBACCAAABBBACCBC
result:
ok
Test #10:
score: 5
Accepted
time: 1ms
memory: 3624kb
input:
20 8 cbabxccxxxxxbaxxaaac 40 8 C 7 C 19 C 12 C 19 A 6 B 6 C 20 B 12 B 18 B 6 A 10 B 11 B 14 B 20 C 18 B 4 A 7 C 18 A 1 A 18 C 14 A 5 B 13 B 3 B 7 A 8 C 1 C 14 B 4 C 13 A 1 A 7 A 19 A 11 B 16 A 9 A 7 A 11 C 4 B 10 B 1 A 1 A 1 B 5 A 8 C 20 C 13 A 3 B 11 B 18 C 13 A 11 A 20 B 3 A 16 C 17 A 5 C 6 C 17 B...
output:
BCCCCBBBBCAACBBBCBBB
result:
ok
Test #11:
score: 5
Accepted
time: 1ms
memory: 3544kb
input:
100 0 cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc 200 62 A 60 B 22 A 46 A 87 A 67 B 29 A 43 A 64 B 28 B 83 B 16 A 50 A 64 B 48 A 95 A 28 B 90 A 5 A 12 A 23 B 86 A 62 A 81 B 64 B 9 B 5 A 10 B 89 B 18 B 69 B 62 A 63 A 55 A 28 B 92 B 49 A 43 A 71...
output:
ABABBBAAAAABBBBBBABBABABABAABBBBAABBAABBAABBABBBBBABBABABBAAABBABABBAABBAABABBAAAAABBBAAABBAAABBBBBB
result:
ok
Test #12:
score: 5
Accepted
time: 0ms
memory: 3552kb
input:
100 0 cabaabbaaabacbaccbccacbcaaccccbbcccbabbaaaacccbaaabbcaaabaabcaabcbbcaabaabaaccacbaabbacbbacaaabcbbca 200 94 C 31 A 29 B 48 B 89 C 52 A 49 B 11 C 10 C 66 C 90 B 49 B 80 B 19 B 26 B 54 B 20 B 65 B 52 A 80 B 51 A 33 A 94 C 6 C 18 C 60 A 87 B 45 B 12 C 7 A 26 B 57 A 89 C 54 B 4 B 29 B 39 A 67 C 22...
output:
ABCCCACCBCCBACBAAAAABBCBBCBAAACCBBBCCACBCCBAABCCCBCCBCBBABCCABBCBCABBCABBCBCABBACCCACBAAACABBBABCCBB
result:
ok
Test #13:
score: 5
Accepted
time: 1ms
memory: 3684kb
input:
100 8 cccccxccccccxccccxccccccxcccccccxccccxccccccccccxccccccccccccccccccccccccccccccccccccccxcccccccccccc 200 38 A 13 B 60 A 18 B 36 B 76 B 95 A 7 B 25 A 58 B 53 A 27 A 21 A 78 A 15 A 48 A 52 B 59 A 65 B 68 A 72 A 47 A 95 A 48 A 96 A 67 A 96 A 47 A 46 B 96 A 79 B 36 B 60 A 90 B 5 B 95 A 17 A 99 A 2...
output:
AABBACBBAABACBBABBBABAAABAAABBBACBAAABAABBAABABACBBAAAABBBBBBBAAAAABAAABBAAABBAAAABABBBBBABAAABBAABB
result:
ok
Test #14:
score: 5
Accepted
time: 1ms
memory: 3576kb
input:
100 8 ccbbcaacxxbcxacbcaabxaaccbxcaabacbccaccacbcacxcacccccccbcacbaabbabaacbbababaxabcccacaxabaaacabbcbccc 200 41 A 8 A 75 C 86 C 93 B 26 A 83 C 55 A 49 B 69 B 50 B 54 B 41 A 85 C 43 B 4 A 75 C 95 A 2 A 54 B 14 C 3 A 18 C 79 A 5 B 8 A 61 C 90 B 48 C 96 B 84 A 40 C 55 A 58 B 77 A 47 A 36 A 60 A 2 A 8...
output:
BBCAABBBCBCBBBBAABBCBBCABCBABCCBAAABCAACBCABABACAAAAAABCACACCCCCCCBBACCBABACBBCBBABBBBCCCBCACCCBABBA
result:
ok
Test #15:
score: 5
Accepted
time: 3ms
memory: 3760kb
input:
5000 0 ccbbbbacccabaacccbccaabcbcbabcabbbcbccacbbcccbccbcacbacaacbccaaacbcaabbacacabaaaccacbaccbbbccaabbbcabcbabaaccbaacacbaaccbacaccababcabbbccacbcabaabababaacccbbababbaabbbaccbabaabbbcbacbbbbbacbbabcabacbbcbaaaaaaccbaaaabccccacaaacabaabaccacabbaacaaccbbcbcaaaccbbccbccbccaacbccacbcbbacaaaaabbacbaab...
output:
-1
result:
ok
Test #16:
score: 5
Accepted
time: 3ms
memory: 3780kb
input:
5000 8 ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc...
output:
ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB...
result:
ok
Test #17:
score: 5
Accepted
time: 0ms
memory: 3872kb
input:
5000 8 abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcab...
output:
BCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCA...
result:
ok
Test #18:
score: 5
Accepted
time: 15ms
memory: 10328kb
input:
50000 0 baabcbbaacbbabababbbabcbcbbcabccaaacccbaaabcabccccbbaacbaaacbcacbabbbbabccabaabbcabacacaaabbaccccbcacaaacbcaacbcacbbabbcbabccbbcacbacbbccacbcabcbcbcacbaacaaccbcbabaaacbcbbccbcacacccbbcabcaccacaaaaaacbcaccaacbbbaaaccaaccaaacbacbaaccaabccaababbabaaaaacbaabaccabacbcbabbcbaabacbbacbbaaabababccac...
output:
ABBAAAABBAAABABABAAABAAAAAAABAAABBBAAAABBBAABAAAAAAABBAABBBAAABAABAAAABAAABABBAAABABABABBBAABAAAAAABABBBAAABBAAABAAABAAAABAAAAAABAABAAAAABAAABAAAAAABAABBABBAAAAABABBBAAAAAAAAABABAAAAAABAABAABABBBBBBAAABAABBAAAABBBAABBAABBBAABAABBAABBAAABBABAABABBBBBAABBABAABABAAAABAAAABBABAAABAAABBBABABAAABABBAABAAA...
result:
ok
Test #19:
score: 5
Accepted
time: 20ms
memory: 11508kb
input:
50000 8 cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc...
output:
ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB...
result:
ok
Test #20:
score: 5
Accepted
time: 21ms
memory: 11512kb
input:
50000 8 abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabca...
output:
BCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCA...
result:
ok
Extra Test:
score: 0
Extra Test Passed