QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#780326#8680. Turning Reducup-team5217#WA 67ms33316kbC++232.8kb2024-11-25 10:04:562024-11-25 10:04:56

Judging History

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

  • [2024-11-25 10:04:56]
  • 评测
  • 测评结果:WA
  • 用时:67ms
  • 内存:33316kb
  • [2024-11-25 10:04:56]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define maxn 500005

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++)
        if (deg[i] == 0 && a[i] != 0) return cout << "impossible" << endl, void();

    queue<int> que;
    vector<int> btns, ptns;

    auto lim = [&](void) -> bool {
        while (!que.empty()) {
            int p = que.front();
            que.pop();
            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) return false;
                    } else
                        que.push(x), op[x] = d, vis[x] = true, btns.push_back(x);
                }
            }
        }
        return true;
    };

    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;
        }

    if (!lim()) return cout << "impossible" << endl, void();

    // 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();

            btns.push_back(p), vis[p] = true, que.push(p);
            int sum = 0;
            if (!lim())
                sum = INT_MAX;
            else
                for (auto b : btns) sum += op[b];

            for (auto b : btns) 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]});
        }
        if (cur.first == INT_MAX) return cout << "impossible" << endl, void();

        btns.clear(), ptns.clear();

        btns.push_back(p), op[p] = cur.second, vis[p] = true, que.push(p);
        lim();
    }

    // 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;
}

详细

Test #1:

score: 100
Accepted
time: 67ms
memory: 33316kb

input:

200000 171004
RGRGBBBGGBRRRBBRRBRBBGRRRGGGGRRRRGBBBRGGGBGGRGGRGRRBRBRRGGBGGGGBRBRBRRBRBRGRRGRBRRBGRRRRBRBBBRGBBBGRRBRGRRGGRGGBGRBGRBRGBBRRBBBGBGRGBBRBGGGGBBBBRBRRRGBGRRBBGGGRRBRBBRGRBGRBRGGBRBBBBRGGRRRBGRBGRBRRRGRGRRBRGRRGRRGBBBBRGGBBRRBGGBGBGRBRGBGGRRGRBRRGGRGRBBBRRBBRRGGRRRRGBRGGGGGGRGBRGRBBBGRBBG...

output:

impossible

result:

ok single line: 'impossible'

Test #2:

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

input:

10 0
RRRRRRRRRR

output:

0

result:

ok single line: '0'

Test #3:

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

input:

10 0
GGGGGGGGGG

output:

impossible

result:

ok single line: 'impossible'

Test #4:

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

input:

10 0
BBBBBBBBBB

output:

impossible

result:

ok single line: 'impossible'

Test #5:

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

input:

10 0
RGBRGBRGBR

output:

impossible

result:

ok single line: 'impossible'

Test #6:

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

input:

10 0
GBBRRRBRGB

output:

impossible

result:

ok single line: 'impossible'

Test #7:

score: -100
Wrong Answer
time: 0ms
memory: 15796kb

input:

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

output:

0

result:

wrong answer 1st lines differ - expected: 'impossible', found: '0'