QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#576403#8674. Riddle of the SphinxJWRuixiTL 0ms0kbC++202.4kb2024-09-19 20:16:102024-09-19 20:16:11

Judging History

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

  • [2024-09-19 20:16:11]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-09-19 20:16:10]
  • 提交

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: 0
Time Limit Exceeded

input:


output:


result: