QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#755245#9551. The Emperorucup-team045#WA 1ms3844kbC++202.1kb2024-11-16 16:50:492024-11-16 16:50:49

Judging History

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

  • [2024-11-16 16:50:49]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3844kb
  • [2024-11-16 16:50:49]
  • 提交

answer

#include<iostream>
#include<cstring>
#include<vector>
using namespace std;
using LL = long long;

struct Node{
    int a, b, c, d;
};

int main(){

#ifdef LOCAL
    freopen("data.in", "r", stdin);
    freopen("data.out", "w", stdout);
#endif

    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);

    int n;
    cin >> n;
    vector<Node> p(n + 1);
    for(int i = 1; i <= n; i++){
        string s;
        cin >> s;
        if (s[0] == 'H'){
            string _; int v1, v2;
            cin >> _ >> v1 >> _ >> v2;
            p[i] = {0, 0, v1, v2}; 
        }
        else{
            string _; int v1, v2, v3, v4;
            cin >> v1 >> _ >> v2 >> _ >> _ >> v3 >> _ >> v4;
            p[i] = {v1, v2, v3, v4};
        }
    }
    vector<int> len(n + 1, -1), out(n + 1);
    for(int i = 1; i <= n; i++) out[i] = i;
    for(int i = 1; i <= n; i++){
        auto [a, b, c, d] = p[i];
        if (p[d].a == c){
            len[i] = 2;
            out[i] = p[d].b;
        }
    }

    const int mod = 998244353;
    for(int _ = 0; _ < n; _++){
        for(int i = 1; i <= n; i++){
            if (len[i] != -1 and len[out[i]] != -1 and p[out[i]].a != 0){
                len[i] += len[out[i]];
                if (len[i] >= mod) len[i] -= mod;
                out[i] = out[out[i]];
            }
        }
        for(int i = 1; i <= n; i++){
            auto [a, b, c, d] = p[i];
            if (len[i] == -1){
                if (len[d] != -1 and p[out[d]].a == c){
                    len[i] = len[d] + 2;
                    if (len[i] >= mod) len[i] -= mod;
                    out[i] = p[out[d]].b;
                }
            }
        }
    }

    bool ok = true;
    int ans = 1;

    vector<bool> v(n + 1);
    auto dfs = [&](auto &&dfs, int u) -> void {
        if (p[u].a == 0) return;
        if (v[u]){
            ok = false;
            return;
        }
        v[u] = true;
        ans += len[u];
        if (ans >= mod) ans -= mod;
        dfs(dfs, out[u]);
    };

    dfs(dfs, 1);
    if (ok) cout << ans << '\n';
    else cout << -1 << '\n';

}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3620kb

input:

1
HALT; PUSH 1 GOTO 1

output:

1

result:

ok 1 number(s): "1"

Test #2:

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

input:

5
POP 1 GOTO 2; PUSH 1 GOTO 2
HALT; PUSH 1 GOTO 3
POP 1 GOTO 4; PUSH 2 GOTO 4
POP 1 GOTO 2; PUSH 2 GOTO 4
HALT; PUSH 99 GOTO 4

output:

5

result:

ok 1 number(s): "5"

Test #3:

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

input:

1
POP 1 GOTO 1; PUSH 1 GOTO 1

output:

-1

result:

ok 1 number(s): "-1"

Test #4:

score: -100
Wrong Answer
time: 1ms
memory: 3616kb

input:

61
POP 62 GOTO 61; PUSH 30 GOTO 60
POP 1 GOTO 3; PUSH 62 GOTO 61
POP 2 GOTO 61; PUSH 62 GOTO 61
POP 4 GOTO 7; PUSH 2 GOTO 61
POP 62 GOTO 61; PUSH 3 GOTO 4
POP 62 GOTO 61; PUSH 3 GOTO 5
POP 5 GOTO 10; PUSH 3 GOTO 6
POP 62 GOTO 61; PUSH 4 GOTO 7
POP 62 GOTO 61; PUSH 4 GOTO 8
POP 6 GOTO 12; PUSH 4 GOTO...

output:

-1

result:

wrong answer 1st numbers differ - expected: '150994941', found: '-1'