QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#576415#8680. Turning RedJWRuixiAC ✓162ms40172kbC++202.4kb2024-09-19 20:17:452024-09-19 20:17:46

Judging History

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

  • [2024-09-19 20:17:46]
  • 评测
  • 测评结果:AC
  • 用时:162ms
  • 内存:40172kb
  • [2024-09-19 20:17:45]
  • 提交

answer

#include <bits/stdc++.h>
#define IL inline
#define LL long long
#define eb emplace_back
#define L(i, j, k) for (int i = (j); i <= (k); ++i)
#define R(i, j, k) for (int i = (j); i >= (k); --i)
using namespace std;

using vi = vector<int>;

using T = pair<int, int>;
using vt = vector<T>;
#define fi first
#define se second

constexpr int N = 2e5 + 9;
constexpr int M = 4e5 + 9;
constexpr int I = 0x3f3f3f3f;
int n, m, b[M], c[M];
vi a[N];
vt g[M];
string s;

IL int gt (char c) {
  switch (c) {
    case 'R' : return 0;
    case 'G' : return 1;
    case 'B' : return 2;
  }
  return -1;
}

bool vis[M];

int main () {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m >> s;
  L (i, 1, m) {
    int k;
    cin >> k;
    while (k--) {
      int z;
      cin >> z;
      a[z].eb(i);
    }
    b[i] = -1;
  }
  L (i, 1, n) {
    int z = (3 - gt(s[i - 1])) % 3;
    if (a[i].empty()) {
      if (z) {
        cout << "impossible\n";
        return 0;
      }
    } else if (a[i].size() == 1) {
      if (~b[a[i][0]] && b[a[i][0]] != z) {
        cout << "impossible\n";
        return 0;
      }
      b[a[i][0]] = z;
    } else {
      g[a[i][0]].eb(a[i][1], z);
      g[a[i][1]].eb(a[i][0], z);
    }
  }
  int as = 0;
  L (i, 1, m) {
    if (vis[i]) {
      continue;
    }
    vi d;
    queue<int> q;
    vis[i] = 1;
    q.emplace(i);
    while (!q.empty()) {
      int u = q.front();
      q.pop();
      d.eb(u);
      for (auto [v, w] : g[u]) {
        if (!vis[v]) {
          vis[v] = 1;
          q.emplace(v);
        }
      }
    }
    int mn = I;
    L (o, 0, 2) {
      if (~b[i] && b[i] != o) {
        continue;
      }
      for (int k : d) {
        vis[k] = 0;
        c[k] = b[k];
      }
      c[i] = o;
      int nw = 0;
      vis[i] = 1;
      q.emplace(i);
      while (!q.empty()) {
        int u = q.front();
        q.pop();
        nw += c[u];
        for (auto [v, w] : g[u]) {
          int z = (w - c[u] + 3) % 3;
          if (~c[v] && c[v] != z) {
            nw = I;
          }
          if (!vis[v]) {
            vis[v] = 1;
            c[v] = z;
            q.emplace(v);
          }
        }
      }
      mn = min(mn, nw);
    }
    if (mn < I) {
      as += mn;
    } else {
      cout << "impossible\n";
      return 0;
    }
  }
  cout << as << '\n';
}
// I love WHQ!  

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 51ms
memory: 15296kb

input:

200000 171004
RGRGBBBGGBRRRBBRRBRBBGRRRGGGGRRRRGBBBRGGGBGGRGGRGRRBRBRRGGBGGGGBRBRBRRBRBRGRRGRBRRBGRRRRBRBBBRGBBBGRRBRGRRGGRGGBGRBGRBRGBBRRBBBGBGRGBBRBGGGGBBBBRBRRRGBGRRBBGGGRRBRBBRGRBGRBRGGBRBBBBRGGRRRBGRBGRBRRRGRGRRBRGRRGRRGBBBBRGGBBRRBGGBGBGRBRGBGGRRGRBRRGGRGRBBBRRBBRRGGRRRRGBRGGGGGGRGBRGRBBBGRBBG...

output:

impossible

result:

ok single line: 'impossible'

Test #2:

score: 0
Accepted
time: 2ms
memory: 3808kb

input:

10 0
RRRRRRRRRR

output:

0

result:

ok single line: '0'

Test #3:

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

input:

10 0
GGGGGGGGGG

output:

impossible

result:

ok single line: 'impossible'

Test #4:

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

input:

10 0
BBBBBBBBBB

output:

impossible

result:

ok single line: 'impossible'

Test #5:

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

input:

10 0
RGBRGBRGBR

output:

impossible

result:

ok single line: 'impossible'

Test #6:

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

input:

10 0
GBBRRRBRGB

output:

impossible

result:

ok single line: 'impossible'

Test #7:

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

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: 2ms
memory: 7984kb

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

input:

200000 10000
RBBBBGRRGGRBRGRGBBRBGGRGRGRBBRRRGRGRGRBGRBBBBGBGBBGGBBGGGGBRGBBBBBBRRGBBGGGRBBBGBRGGGRRGGGGBGBGBGRBBGGRRRGGGRGGRBRRBGRGRBBGBGGRGBBRBBBGGRGBBGBBBBBGRGBGGRGBGGBGRRBGBBGRBGBGGRBBBGBBBGGGBBBRBBGRRRBGBBRBRBBGRGGGBBRGRRRGRRRRGRRBGBRRRRGBBRBBGRRRRBRBRRRGBGRGGBGRBRBGBBRRGBBRBBBGBGGGBBRGRBRRGBBR...

output:

impossible

result:

ok single line: 'impossible'

Test #10:

score: 0
Accepted
time: 44ms
memory: 14548kb

input:

200000 49974
GRBRRGBRRRRRGBRBGRRBGGGRRRRBGGBBBGRRGBBRBGBRBRRBRBBRGRBGGBBGBBRRRBGGBRBRRGBRRRBRRGRGRGRBGRBGBBBGGGRGRBGRGGRBBRBRGGBRBBRRGGGRBGBGBGRBRBRGGBBBRBBRGRRBBGBGRBBRBBBRGGGGRRBGBBRGBGBGGBRBGGRGGGBRRRBBBGRRRRGBBBGBBGRRRBGRGGGRGBRGBGRBBRBRRBGGRGBBRGBBBRGRBBBRRGGRRRGGRRRGRRBGGGBGRRGBRRRRRGRGBGGRRRR...

output:

impossible

result:

ok single line: 'impossible'

Test #11:

score: 0
Accepted
time: 47ms
memory: 15304kb

input:

200000 97963
GRBBRBGGGBGBBGRBBBRGRGBBBGBRBGBGGGBRBBGRGRRBRRGBRRBBGRRGBRRGBBRBGRRRGRGRRRBGBBRGGBRRBBBGRBBGBBRRRGRRGRGRRRBBRGBRRRRRGGGGGBGRRBGRGGBBBBRBBRBBBBBBRRRGBGGRGRGRBBRRBGBRRBBRGBGRBRBRGBRRGBRBGGRGGGGBGGGRBBRGRGGBBGRRGRBGGRBGRBBBRRGGRGGBRRBBRGGRBRBBGRRRGGGBRBRGBRBRBBGGBRGRBBRGGBBBGGBGRRRRRRGGGBR...

output:

impossible

result:

ok single line: 'impossible'

Test #12:

score: 0
Accepted
time: 74ms
memory: 39972kb

input:

200000 400000
GRGBBBRRGGRGBBRBBGBRRRBBRBRGRGRRBBRBRGRBRGGRRGGBRGRRBRBRRBRRGGBBGRBBGRRRBRBGBBRBGBRBBBBBBGBRGRBBGRGBGBBGRGBGBBBBGRGRRRGGGBGGGGRBRBRBRRBGGBGRRGBGGGRRBBBGRBRBGGBBRBBGRGGBGRGBGRRGGBRGGRGRGGBGBRBBBRGGBRRRRBGRBGRRGBRBRGRRBBGGGRRGRGGBRBBBBRGBBGRGGBGGRBGGRBRBBRBBBGRGGGGGBGGRRBBGRGGGRGBRBGGGGR...

output:

200051

result:

ok single line: '200051'

Test #13:

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

input:

10 10
GRBBGBBRRG
2 10 1
2 9 10
2 5 6
2 4 2
2 8 4
2 7 5
2 2 3
2 3 7
2 6 9
1 1

output:

12

result:

ok single line: '12'

Test #14:

score: 0
Accepted
time: 120ms
memory: 29236kb

input:

200000 199999
BRGBBBRBBGGRRGBGRBGBRRGGGBRGGBBGRRRGGBRBBGRBGGBGRGBGBRBGGRBRRGBRBGRBGGGRRGGBGGBBBBBRRGRRRRBGRBGGGBBGGGRGRRGRGRGBGRBBBGGRGRRRRGRGGGGGGGBBBBGRGRGGRBRGRGBBBRRBGRGRGGBGRBRRRRGBBRBBGBGRRRGBRRRBRRGBBBRRBGRBRRBGBGBGGRBBRRGGRRRBBRRGBRGRRGBRRGRRGGRGRRRRGBGRBBRRGRRBRRBBRGBRRBRGGRRBBBRGRGBBGGBRRR...

output:

199855

result:

ok single line: '199855'

Test #15:

score: 0
Accepted
time: 162ms
memory: 29068kb

input:

200000 199999
BRGGGBBBGGRBGGBRGGGBBBBBBBRRGBBBGBBRBGGBGGGBGBBGBBGRBRRGGRRBBGGBRRGBRGBBBGGGRBRRRBRBBBRGBRRBRRGBGBRGBGRGGGGBBRGGRRGBGGBBGBBRBGRRBRBRGGBBRRRGBBGRRBRRBRGGGBBGGRGRGGGBGGGRBRGGRBGGGGBBRBRRBRRGRBBRBGGBBBGRGRBGRRBGGRRRBRBRGGGBGGRBGBBBRGGRRRRBGRGRBGGRBGBBBGBBRRRGGRGBRRRBRGRRRGGGRRRBGRRGGRBRBR...

output:

impossible

result:

ok single line: 'impossible'

Test #16:

score: 0
Accepted
time: 153ms
memory: 27952kb

input:

200000 200000
RRBBBGRBBGRBGBBRRGGBRBBGGBRGRBGBGBBBRBBGGBGRGBBRBRGBGRRGBBBGBGRRRBGRGBBGBBRGBRGBGGBBGBRRRRRRBBRBGGBGGRBBRBBBRBBBRRRGGGGRGBBBRBBGRRBRBBRBBGGRRRGGBRGRRRGRBBRGGGGRBGGBBGGRRRBGBBGBBRGRBRGRBGBRBGBGGGBBGGRBRRGBBRRGRBGBRBRRBBGGBGGRBGRGGGRRGGBGRRBBRBRRGGBRGGRGRBBRRRRBBRBGRBBBRGGGRGRGBGRGRGBRGR...

output:

199859

result:

ok single line: '199859'

Test #17:

score: 0
Accepted
time: 120ms
memory: 28820kb

input:

200000 200000
RRRRBRRGGGGGGRBBBGRRGBBRRBGRRBRRGRRBGGRGBRBGBRGGBRRBBGGGRGRGRGBGRBRGRBBGBRRBRRBGGGRBGBRGBBGGRGBRGGRBGBGGRBGGRGGGGRGGGGRRRGBBGRBRRGBRRBGRBBBRRBGRGRGBBGRRBBRBBGGRBRBGRBGBGBRRGGGBBGGBGRRGRRBGGBBRGGBRGRGBGGGRBBBRGRRGRBGRRGGGRBRGGGGRRGGRGGRBGRGBRBGBBRGBGRGBBBGRRGRGGBBBRBGGRGBRGRBGRRRRBBGRGR...

output:

200292

result:

ok single line: '200292'

Test #18:

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

input:

10 9
BRBRBRGRRB
1 8
1 4
1 6
1 9
1 3
1 7
10 2 5 6 4 1 7 8 9 3 10
1 5
1 2

output:

12

result:

ok single line: '12'

Test #19:

score: 0
Accepted
time: 89ms
memory: 31300kb

input:

200000 199999
BRRGBGBBGRGBGRGRGRBRRGBGBRRGBBBRGBBBRBBRRRGRRRBBRBGGRRGBBBGRRRBBBRBBRRBRRBBBGGGBRRRBRBGRBGRBRGGBGRBRGGGBGRGRGBBBRGGRRRBBBGBGGRRGRBRBBBBRRGBGRBRBGRGBRGRGRBRBGRGBRBRGBBBRGGGGGBRRGBRBBBGBRGRRGBRBGBBRBGGRGRGBBGRGRBGRRGBRGRBGRBBRBGRBRBRRBBBGGGBBGGBGBGGBGBBRRGGGGRBRBGBGBBGRBGRGRBBBBRGRGBBGRG...

output:

200007

result:

ok single line: '200007'

Test #20:

score: 0
Accepted
time: 70ms
memory: 24484kb

input:

200000 199999
RBRGBRGRRGRBGGBGRRRGBGRBRRBGGBRBGBRGRGRRRBBRRRRRRGGBBGRGRGBGRGRBBBRBGRBBGGGRBBRRGBRGGRRGBRGGRBRRRGRGRRRBGRRRBRGBRBBBRGGRGRRGGRBGGRBRRGBGGGBGGBRGBGGBBRRBRRRRGBBBRGBRBGBBGRBRGGBBBBRGRGGBRBRRGGBRGBGBRBGBGBGGBRRGRGGRRGRBRRRBGGBGRBGRGRGGGBRGRGRRBBGRBRGRRGGBGRRBGGGRRRBGRBGRGRBBRGBBGBGRRGRBGG...

output:

impossible

result:

ok single line: 'impossible'

Test #21:

score: 0
Accepted
time: 2ms
memory: 5700kb

input:

10 10
BRBBRGBGRG
2 10 6
2 8 9
2 7 1
2 2 4
2 6 7
2 5 10
2 9 3
2 2 5
2 1 8
2 3 4

output:

impossible

result:

ok single line: 'impossible'

Test #22:

score: 0
Accepted
time: 112ms
memory: 28840kb

input:

200000 200000
RRGBBGRGBGGRBRBRRRBGGBBGGBGRBGBGRRBRRBBGBBGBBGGGGBBGBRRRGBRRBRBGGGRBRRGBGRRGBGGBBGRBRRBGBRGBBBBGGRRRGBGGBBBBRRBBGBBBBGGGRGRGBBBRRGBGRBGRBRGRRRBBRGGRRBBGRRGGGBBBBBRGGRBGBGGGGGGGRGGRRBGGBBBBBRGRRBBBRRGGGBGRGRGGRBGRRBGBBRBGRGBBGRGBBGRGGRGRRRBGGGRRBRGRGGRRGRGBBBGBGBBBBBRGBBBRBGGGGGBRGBBBRG...

output:

impossible

result:

ok single line: 'impossible'

Test #23:

score: 0
Accepted
time: 124ms
memory: 29000kb

input:

200000 200000
RRRGGRBRRBBRBBGBGGGBBBGGBGRRGRGBBGBGBRBRGGGBGRGRGGBGGGRBRRGBGGBRGRBBBBBRGRRRRBRRRBRRRGRGRBBGRGGBGBRRBGRRGRGRBGGRBRBBRRBBBBBBBBGBBBRGBGRRBRGBBBBRGBBBRGBGGRBRRBBRGRGGRRGGRRGGBRGBBGRGGGGRBRGRGGBGRRGGBBBGRGBBBBGBGRBGBBBBBRRRGRGBBBBBBRRBGRRBRRBGGBRRGBBRRBRBRBRBBBGGRBBGRRGRBGRRBGBGRBRBRRRGGR...

output:

impossible

result:

ok single line: 'impossible'

Test #24:

score: 0
Accepted
time: 23ms
memory: 14652kb

input:

60000 50000
BRGGGRGRRGGGGBBBGGBGRGGRGBGBGRGBBBBRGGRBRBGRBRGBGRBBRRRBRRGGRGBBBRGRBRBRBRBBBBGBGRGGBRRGGBGBGBGBGBGGRBRRGRRGBGGGBBRBBGGGRRGRBBBBBGGBBGRBBBGBGGRRRRRGRBGRRRRBGRBRRBBGRRBGRBGRGGGGBBGGRGRBGRRRRRGGRRBRRRBRBRBRBBRGBRGBRRBRGBGBRRGRBRBRRRBBGGRGGRRBGRRBBGGBRRBBBBRRRGBRRBRBRGGBGBGGGGBBGGGRBGRGRGBB...

output:

30280

result:

ok single line: '30280'

Test #25:

score: 0
Accepted
time: 25ms
memory: 11988kb

input:

54937 54937
GGGRGBGRRRRRBGBBRGRBBGBGBRRRBBGGBGGRBBGRBRGBGRBBGGGRBBGGGBRGRGRRRGRRGGGBGGRBRBGBGRGRGRGGGBGRRBRBGBBBGRRRGBRBGGGGGRBGGGRRGRRBBGGGGGBRGBBGGRBGRGRBRGBRRGGRGGBRRBGRBRRBBGGGRBGBRRRRBGBGRRBRGBRRRBRRGGRRGBBGBBGBGRGGRBRGRGRBGBGGRRRRGGGRGGRGRRGBGRBBGBRBGBRGGBBGBGGBRBBGBBBGGRRGGBBBBRRBGRGGRRBGRRRR...

output:

54911

result:

ok single line: '54911'

Test #26:

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

input:

200000 400000
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR...

output:

0

result:

ok single line: '0'

Test #27:

score: 0
Accepted
time: 59ms
memory: 39976kb

input:

200000 400000
GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG...

output:

400000

result:

ok single line: '400000'

Test #28:

score: 0
Accepted
time: 69ms
memory: 39972kb

input:

200000 400000
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB...

output:

200000

result:

ok single line: '200000'

Test #29:

score: 0
Accepted
time: 44ms
memory: 14388kb

input:

200000 10
BBBRBBBRRGRBRRRRBGBRBRRRGBBBRGRGGGGRGBGGGGRGRBBGGRGBBBRRRBBRBGGRRGBBRRRRGRRBGGRBBGBBBGBGGRGRGGGRRGGRRBRBGGRBBRBRRGRRRGGRGBBRRBGRGRBRRRGRBRRGRGBBRGGBBBRBBRRBGBGRBGGBBGGBBGRBGGBBGBRRRGBRBGRGBBBRBRBRBBRRRGBGBRRGRRRBGBGGGRGBGGGGRBGBBBRRGRGBRGRGRRGRGRBGBGGBGGGBRGRBBBBRBRBRBBGGBRRRGBGBGRRRGBBRRG...

output:

impossible

result:

ok single line: 'impossible'

Extra Test:

score: 0
Extra Test Passed