QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#780313 | #8680. Turning Red | ucup-team5217# | RE | 64ms | 27116kb | C++23 | 2.8kb | 2024-11-25 09:55:00 | 2024-11-25 09:55:01 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define maxn 200005
typedef pair<int, int> pii;
vector<int> btn[maxn], fa[maxn];
int a[maxn], b[maxn], deg[maxn], odeg[maxn], op[maxn], f[3][maxn];
bool vis[maxn];
void solve(void) {
int n, m;
cin >> n >> m;
string s;
cin >> s;
for (int i = 0; i < n; i++) a[i + 1] = b[i + 1] = (s[i] == 'R' ? 0 : (s[i] == 'G' ? 1 : 2));
for (int i = 1, k, x; i <= m; i++) {
cin >> k;
while (k--) cin >> x, btn[i].push_back(x), fa[x].push_back(i), deg[x]++, odeg[x]++;
}
// for (int i = 1; i <= n; i++) cerr << deg[i] << ' ';
// cerr << endl;
// for (int i = 1; i <= n; i++) cerr << a[i] << ' ';
// cerr << endl;
for (int i = 1; i <= n; i++)
if (deg[i] == 0 && a[i] != 0) return cout << "impossible" << endl, void();
// cerr << "?" << endl;
queue<int> que;
vector<int> btns, ptns;
auto lim = [&](void) {
while (!que.empty()) {
int p = que.front();
que.pop(), btns.push_back(p);
// cerr << "! " << p << endl;
for (auto i : btn[p]) {
a[i] = (a[i] + op[p]) % 3, ptns.push_back(i);
if (--deg[i] == 1) {
int x = fa[i][fa[i][0] == p], d = (3 - a[i]) % 3;
if (vis[x]) {
if (op[x] != d) cout << "impossible" << endl, exit(0);
} else
que.push(x), op[x] = d, vis[x] = true;
}
}
}
return;
};
for (int i = 1; i <= n; i++)
if (deg[i] == 1) {
int x = fa[i].front();
que.push(x), op[x] = (3 - a[i]) % 3, vis[x] = true;
}
lim();
// cerr << "###" << endl;
// for (int i = 1; i <= n; i++) cerr << op[i] << ' ';
// cerr << endl;
for (int i = 1; i <= n; i++) {
if (a[i] == 0) continue;
int p = fa[i][0];
pii cur = {INT_MAX, 0};
for (op[p] = 0; op[p] < 2; op[p]++) {
btns.clear(), ptns.clear();
que.push(p), vis[p] = true;
lim();
int sum = 0;
for (auto b : btns) sum += f[op[p]][b] = op[b], vis[b] = false;
for (auto p : ptns) a[p] = b[p], deg[p] = odeg[p];
cur = min(cur, pii{sum, op[p]});
}
for (auto b : btns) op[b] = f[cur.second][b], vis[b] = true;
for (auto p : ptns) a[p] = deg[p] = 0;
}
// cerr << "###" << endl;
// for (int i = 1; i <= n; i++) cerr << op[i] << ' ';
// cerr << endl;
int ans = 0;
for (int i = 1; i <= n; i++) ans += op[i];
cout << ans << endl;
return;
}
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
int _ = 1;
while (_--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 64ms
memory: 27116kb
input:
200000 171004 RGRGBBBGGBRRRBBRRBRBBGRRRGGGGRRRRGBBBRGGGBGGRGGRGRRBRBRRGGBGGGGBRBRBRRBRBRGRRGRBRRBGRRRRBRBBBRGBBBGRRBRGRRGGRGGBGRBGRBRGBBRRBBBGBGRGBBRBGGGGBBBBRBRRRGBGRRBBGGGRRBRBBRGRBGRBRGGBRBBBBRGGRRRBGRBGRBRRRGRGRRBRGRRGRRGBBBBRGGBBRRBGGBGBGRBRGBGGRRGRBRRGGRGRBBBRRBBRRGGRRRRGBRGGGGGGRGBRGRBBBGRBBG...
output:
impossible
result:
ok single line: 'impossible'
Test #2:
score: 0
Accepted
time: 0ms
memory: 7796kb
input:
10 0 RRRRRRRRRR
output:
0
result:
ok single line: '0'
Test #3:
score: 0
Accepted
time: 1ms
memory: 7632kb
input:
10 0 GGGGGGGGGG
output:
impossible
result:
ok single line: 'impossible'
Test #4:
score: 0
Accepted
time: 0ms
memory: 7640kb
input:
10 0 BBBBBBBBBB
output:
impossible
result:
ok single line: 'impossible'
Test #5:
score: 0
Accepted
time: 1ms
memory: 5660kb
input:
10 0 RGBRGBRGBR
output:
impossible
result:
ok single line: 'impossible'
Test #6:
score: 0
Accepted
time: 1ms
memory: 5660kb
input:
10 0 GBBRRRBRGB
output:
impossible
result:
ok single line: 'impossible'
Test #7:
score: 0
Accepted
time: 1ms
memory: 9904kb
input:
10 2 BBRRBRBRRR 7 7 8 5 3 10 9 4 5 6 10 1 3 2
output:
impossible
result:
ok single line: 'impossible'
Test #8:
score: 0
Accepted
time: 1ms
memory: 9976kb
input:
10 9 RRRRRBGRRG 1 10 5 9 3 8 2 5 2 8 7 1 7 1 10 2 6 1 1 3 1 6 4 1 9 4 2
output:
5
result:
ok single line: '5'
Test #9:
score: 0
Accepted
time: 40ms
memory: 21676kb
input:
200000 10000 RBBBBGRRGGRBRGRGBBRBGGRGRGRBBRRRGRGRGRBGRBBBBGBGBBGGBBGGGGBRGBBBBBBRRGBBGGGRBBBGBRGGGRRGGGGBGBGBGRBBGGRRRGGGRGGRBRRBGRGRBBGBGGRGBBRBBBGGRGBBGBBBBBGRGBGGRGBGGBGRRBGBBGRBGBGGRBBBGBBBGGGBBBRBBGRRRBGBBRBRBBGRGGGBBRGRRRGRRRRGRRBGBRRRRGBBRBBGRRRRBRBRRRGBGRGGBGRBRBGBBRRGBBRBBBGBGGGBBRGRBRRGBBR...
output:
impossible
result:
ok single line: 'impossible'
Test #10:
score: 0
Accepted
time: 47ms
memory: 23240kb
input:
200000 49974 GRBRRGBRRRRRGBRBGRRBGGGRRRRBGGBBBGRRGBBRBGBRBRRBRBBRGRBGGBBGBBRRRBGGBRBRRGBRRRBRRGRGRGRBGRBGBBBGGGRGRBGRGGRBBRBRGGBRBBRRGGGRBGBGBGRBRBRGGBBBRBBRGRRBBGBGRBBRBBBRGGGGRRBGBBRGBGBGGBRBGGRGGGBRRRBBBGRRRRGBBBGBBGRRRBGRGGGRGBRGBGRBBRBRRBGGRGBBRGBBBRGRBBBRRGGRRRGGRRRGRRBGGGBGRRGBRRRRRGRGBGGRRRR...
output:
impossible
result:
ok single line: 'impossible'
Test #11:
score: 0
Accepted
time: 52ms
memory: 24780kb
input:
200000 97963 GRBBRBGGGBGBBGRBBBRGRGBBBGBRBGBGGGBRBBGRGRRBRRGBRRBBGRRGBRRGBBRBGRRRGRGRRRBGBBRGGBRRBBBGRBBGBBRRRGRRGRGRRRBBRGBRRRRRGGGGGBGRRBGRGGBBBBRBBRBBBBBBRRRGBGGRGRGRBBRRBGBRRBBRGBGRBRBRGBRRGBRBGGRGGGGBGGGRBBRGRGGBBGRRGRBGGRBGRBBBRRGGRGGBRRBBRGGRBRBBGRRRGGGBRBRGBRBRBBGGBRGRBBRGGBBBGGBGRRRRRRGGGBR...
output:
impossible
result:
ok single line: 'impossible'
Test #12:
score: -100
Runtime Error
input:
200000 400000 GRGBBBRRGGRGBBRBBGBRRRBBRBRGRGRRBBRBRGRBRGGRRGGBRGRRBRBRRBRRGGBBGRBBGRRRBRBGBBRBGBRBBBBBBGBRGRBBGRGBGBBGRGBGBBBBGRGRRRGGGBGGGGRBRBRBRRBGGBGRRGBGGGRRBBBGRBRBGGBBRBBGRGGBGRGBGRRGGBRGGRGRGGBGBRBBBRGGBRRRRBGRBGRRGBRBRGRRBBGGGRRGRGGBRBBBBRGBBGRGGBGGRBGGRBRBBRBBBGRGGGGGBGGRRBBGRGGGRGBRBGGGGR...