QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#413293#8680. Turning RedsupepapupuRE 89ms32240kbC++202.4kb2024-05-17 11:24:542024-05-17 11:24:55

Judging History

你现在查看的是最新测评结果

  • [2024-05-17 11:24:55]
  • 评测
  • 测评结果:RE
  • 用时:89ms
  • 内存:32240kb
  • [2024-05-17 11:24:54]
  • 提交

answer

#include <bits/stdc++.h>

#define x first
#define y second
#define el '\n'
#define debug(x) cerr << #x << ": " << x << el
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int N = 2e5 + 10, INF = 0x3f3f3f3f, mod = 998244353;

bool st[N * 3];
int n, m, ans, cnt;
int c[N], cur[N];
vector<int> l[N], b[N * 2], used;

void dfs(int u) {
    used.emplace_back(u);
    st[u] = 1;
    for (int v: l[u]) {
        if (st[v + n]) continue;
        used.emplace_back(v + n);
        st[v + n] = 1;
        int t = (3 - cur[u]) % 3;
        cnt += t;
        for (int i: b[v]) cur[i] = (cur[i] + t) % 3;
        for (int i: b[v]) 
            if (i != u) 
                dfs(i);
    }
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    cin >> n >> m;
    string s; cin >> s;
    for (int i = 0; i < n; ++i) 
        if (s[i] == 'G') c[i + 1] = cur[i + 1] = 1;
        else if (s[i] == 'B') c[i + 1] = cur[i + 1] = 2;
    for (int i = 1; i <= m; ++i) {
        int sz; cin >> sz;
        b[i].resize(sz);
        for (int j = 0; j < sz; ++j) {
            cin >> b[i][j];
            l[b[i][j]].emplace_back(i);
        }
    }
    for (int i = 1; i <= n; ++i) {
        if (st[i]) continue;
        if (l[i].empty()) {
            if (c[i]) 
                return cout << "impossible\n", 0;
            continue;
        }
        if (l[i].size() == 1) {
            cnt = 0;
            dfs(i);
            ans += cnt;
            for (int j: used) 
                if (j <= n && cur[j]) 
                    return cout << "impossible\n", 0;
            used.clear();
            continue;
        }
        int mn = INF, e = l[i][0];
        vector<int> vec;
        for (int j = 0; j < 3; ++j) {
            cnt = j;
            for (int u: b[e]) cur[u] = (cur[u] + j) % 3;
            used.emplace_back(e + n);
            dfs(i);
            for (int u: used) {
                if (u <= n) {
                    if (cur[u]) cnt = INF;
                    st[u] = 0, cur[u] = c[u];
                } else st[u + n] = 0;
            }
            if (cnt < mn) mn = cnt, vec = move(used);
            used.clear();
        }
        if (mn == INF) return cout << "impossible\n", 0;
        for (int i: vec) 
            if (i <= n) st[i] = 1;
        ans += mn;
    }
    cout << ans << el;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 89ms
memory: 32240kb

input:

200000 171004
RGRGBBBGGBRRRBBRRBRBBGRRRGGGGRRRRGBBBRGGGBGGRGGRGRRBRBRRGGBGGGGBRBRBRRBRBRGRRGRBRRBGRRRRBRBBBRGBBBGRRBRGRRGGRGGBGRBGRBRGBBRRBBBGBGRGBBRBGGGGBBBBRBRRRGBGRRBBGGGRRBRBBRGRBGRBRGGBRBBBBRGGRRRBGRBGRBRRRGRGRRBRGRRGRRGBBBBRGGBBRRBGGBGBGRBRGBGGRRGRBRRGGRGRBBBRRBBRRGGRRRRGBRGGGGGGRGBRGRBBBGRBBG...

output:

impossible

result:

ok single line: 'impossible'

Test #2:

score: 0
Accepted
time: 1ms
memory: 3836kb

input:

10 0
RRRRRRRRRR

output:

0

result:

ok single line: '0'

Test #3:

score: 0
Accepted
time: 0ms
memory: 5640kb

input:

10 0
GGGGGGGGGG

output:

impossible

result:

ok single line: 'impossible'

Test #4:

score: 0
Accepted
time: 0ms
memory: 7700kb

input:

10 0
BBBBBBBBBB

output:

impossible

result:

ok single line: 'impossible'

Test #5:

score: 0
Accepted
time: 1ms
memory: 5596kb

input:

10 0
RGBRGBRGBR

output:

impossible

result:

ok single line: 'impossible'

Test #6:

score: 0
Accepted
time: 1ms
memory: 5648kb

input:

10 0
GBBRRRBRGB

output:

impossible

result:

ok single line: 'impossible'

Test #7:

score: 0
Accepted
time: 1ms
memory: 5672kb

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: 5868kb

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: 54ms
memory: 20740kb

input:

200000 10000
RBBBBGRRGGRBRGRGBBRBGGRGRGRBBRRRGRGRGRBGRBBBBGBGBBGGBBGGGGBRGBBBBBBRRGBBGGGRBBBGBRGGGRRGGGGBGBGBGRBBGGRRRGGGRGGRBRRBGRGRBBGBGGRGBBRBBBGGRGBBGBBBBBGRGBGGRGBGGBGRRBGBBGRBGBGGRBBBGBBBGGGBBBRBBGRRRBGBBRBRBBGRGGGBBRGRRRGRRRRGRRBGBRRRRGBBRBBGRRRRBRBRRRGBGRGGBGRBRBGBBRRGBBRBBBGBGGGBBRGRBRRGBBR...

output:

impossible

result:

ok single line: 'impossible'

Test #10:

score: 0
Accepted
time: 62ms
memory: 25200kb

input:

200000 49974
GRBRRGBRRRRRGBRBGRRBGGGRRRRBGGBBBGRRGBBRBGBRBRRBRBBRGRBGGBBGBBRRRBGGBRBRRGBRRRBRRGRGRGRBGRBGBBBGGGRGRBGRGGRBBRBRGGBRBBRRGGGRBGBGBGRBRBRGGBBBRBBRGRRBBGBGRBBRBBBRGGGGRRBGBBRGBGBGGBRBGGRGGGBRRRBBBGRRRRGBBBGBBGRRRBGRGGGRGBRGBGRBBRBRRBGGRGBBRGBBBRGRBBBRRGGRRRGGRRRGRRBGGGBGRRGBRRRRRGRGBGGRRRR...

output:

impossible

result:

ok single line: 'impossible'

Test #11:

score: 0
Accepted
time: 72ms
memory: 29084kb

input:

200000 97963
GRBBRBGGGBGBBGRBBBRGRGBBBGBRBGBGGGBRBBGRGRRBRRGBRRBBGRRGBRRGBBRBGRRRGRGRRRBGBBRGGBRRBBBGRBBGBBRRRGRRGRGRRRBBRGBRRRRRGGGGGBGRRBGRGGBBBBRBBRBBBBBBRRRGBGGRGRGRBBRRBGBRRBBRGBGRBRBRGBRRGBRBGGRGGGGBGGGRBBRGRGGBBGRRGRBGGRBGRBBBRRGGRGGBRRBBRGGRBRBBGRRRGGGBRBRGBRBRBBGGBRGRBBRGGBBBGGBGRRRRRRGGGBR...

output:

impossible

result:

ok single line: 'impossible'

Test #12:

score: -100
Runtime Error

input:

200000 400000
GRGBBBRRGGRGBBRBBGBRRRBBRBRGRGRRBBRBRGRBRGGRRGGBRGRRBRBRRBRRGGBBGRBBGRRRBRBGBBRBGBRBBBBBBGBRGRBBGRGBGBBGRGBGBBBBGRGRRRGGGBGGGGRBRBRBRRBGGBGRRGBGGGRRBBBGRBRBGGBBRBBGRGGBGRGBGRRGGBRGGRGRGGBGBRBBBRGGBRRRRBGRBGRRGBRBRGRRBBGGGRRGRGGBRBBBBRGBBGRGGBGGRBGGRBRBBRBBBGRGGGGGBGGRRBBGRGGGRGBRBGGGGR...

output:


result: