Submission #1022845

# Submission time Handle Problem Language Result Execution time Memory
1022845 2024-07-14T06:12:56 Z shiomusubi496 Toy Train (IOI17_train) C++17
11 / 100
10 ms 2692 KB
#include "train.h"

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep2(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rrep(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define rrep2(i, a, b) for (int i = (int)(b) - 1; i >= (int)(a); --i)
#define all(v) begin(v), end(v)

using namespace std;

template<class T, class U> bool chmin(T& a, const U& b) { return a > b ? a = b, true : false; }
template<class T, class U> bool chmax(T& a, const U& b) { return a < b ? a = b, true : false; }

using ll = long long;

std::vector<int> who_wins(std::vector<int> a, std::vector<int> r, std::vector<int> u, std::vector<int> v) {
    int n = a.size(), m = u.size();
    vector<vector<int>> g(n), rg(n);
    vector<bool> self_cycle(n);
    rep (i, m) {
        if (u[i] == v[i]) self_cycle[u[i]] = true;
        else {
            g[u[i]].push_back(v[i]);
            rg[v[i]].push_back(u[i]);
        }
    }

    auto calc_scc = [&](vector<vector<int>> g) {
        vector<vector<int>> rg(n);
        rep (i, n) for (int j : g[i]) rg[j].push_back(i);
        vector<int> cmp(n, -1);
        vector<int> ord, used(n);
        auto dfs = [&](auto self, int v) -> void {
            used[v] = true;
            for (int u : g[v]) {
                if (!used[u]) self(self, u);
            }
            ord.push_back(v);
        };
        rep (i, n) if (!used[i]) dfs(dfs, i);
        reverse(all(ord));
        int k = 0;
        auto rdfs = [&](auto self, int v) -> void {
            cmp[v] = k;
            for (int u : rg[v]) {
                if (cmp[u] == -1) self(self, u);
            }
        };
        for (int i : ord) {
            if (cmp[i] == -1) rdfs(rdfs, i), ++k;
        }
        return cmp;
    };
    auto cmp = calc_scc(g);

    {
        vector<bool> memo(n);
        vector<int> cmp2;
        {
            vector<bool> used(n);
            queue<int> que;
            rep (i, n) {
                if (r[i] == 1) {
                    used[i] = true;
                }
                if (a[i] == 1) {
                    for (auto j : g[i]) {
                        if (cmp[i] != cmp[j]) used[i] = true;
                    }
                }
                else {
                    used[i] = true;
                    for (auto j : g[i]) {
                        if (cmp[i] == cmp[j]) used[i] = false;
                    }
                }
                if (used[i]) que.push(i);
            }
            vector<int> deg(n);
            rep (i, m) {
                if (u[i] != v[i] && cmp[u[i]] == cmp[v[i]]) ++deg[u[i]];
            }
            while (!que.empty()) {
                int v = que.front(); que.pop();
                for (int u : rg[v]) {
                    if (cmp[u] != cmp[v]) continue;
                    if (used[u]) continue;
                    if (a[u] == 1 || (--deg[u]) == 0) {
                        used[u] = true;
                        que.push(u);
                    }
                }
            }
            vector<vector<int>> g(n);
            rep (i, m) {
                if (u[i] == v[i]) continue;
                if (!used[u[i]] && !used[v[i]]) g[u[i]].push_back(v[i]);
            }
            cmp2 = calc_scc(g);
        }
        vector<int> cnt_c2(n);
        rep (i, n) cnt_c2[cmp[i]]++;
        queue<int> que;
        rep (i, n) {
            if (a[i] == 0) {
                // ここが嘘で、これ以外にも r=0 からなる cycle があれば検知しなくてはならない
                // if (self_cycle[i] && r[i] == 0) {
                //     memo[i] = true;
                // }
                if (self_cycle[i] && r[i] == 0) memo[i] = true;
                else if (cnt_c2[cmp[i]] >= 2) memo[i] = true;
                for (int j : g[i]) {
                    if (cmp[i] != cmp[j]) {
                        memo[i] = true;
                        break;
                    }
                }
                if (memo[i]) que.push(i);
            }
            else {
                memo[i] = true;
                for (int j : g[i]) {
                    if (cmp[i] == cmp[j]) {
                        memo[i] = false;
                        break;
                    }
                }
                if (memo[i]) que.push(i);
            }
        }
        vector<int> deg(n);
        rep (i, m) {
            if (u[i] == v[i]) continue;
            if (cmp[u[i]] == cmp[v[i]]) ++deg[u[i]];
        }
        while (!que.empty()) {
            int v = que.front(); que.pop();
            for (int u : rg[v]) {
                if (cmp[u] != cmp[v]) continue;
                if (memo[u]) continue;
                if (a[u] == 0 || (--deg[u]) == 0) {
                    memo[u] = true;
                    que.push(u);
                }
            }
        }
        rep (i, n) {
            if (self_cycle[i]) continue;
            if (memo[i]) r[i] = 0;
        }
    }
    {
        queue<int> que;
        rep (i, n) {
            if (r[i] == 1) que.push(i);
        }
        vector<int> deg(n);
        rep (i, m) {
            if (u[i] != v[i]) ++deg[u[i]];
        }
        while (!que.empty()) {
            int v = que.front(); que.pop();
            for (int u : rg[v]) {
                if (r[u]) continue;
                if (a[u] == 1 || (!self_cycle[u] && (--deg[u]) == 0)) {
                    r[u] = true;
                    que.push(u);
                }
            }
        }
    }
    return r;
}
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 1884 KB 3rd lines differ - on the 42nd token, expected: '0', found: '1'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 348 KB 3rd lines differ - on the 2nd token, expected: '1', found: '0'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2692 KB Output is correct
2 Correct 6 ms 2652 KB Output is correct
3 Correct 6 ms 2496 KB Output is correct
4 Correct 9 ms 2396 KB Output is correct
5 Correct 8 ms 2396 KB Output is correct
6 Correct 8 ms 2240 KB Output is correct
7 Correct 8 ms 2276 KB Output is correct
8 Correct 7 ms 2440 KB Output is correct
9 Correct 7 ms 2268 KB Output is correct
10 Correct 7 ms 2396 KB Output is correct
11 Correct 8 ms 2140 KB Output is correct
12 Correct 7 ms 2140 KB Output is correct
13 Correct 8 ms 2396 KB Output is correct
14 Correct 8 ms 2344 KB Output is correct
15 Correct 10 ms 2396 KB Output is correct
16 Correct 8 ms 2464 KB Output is correct
17 Correct 7 ms 2396 KB Output is correct
18 Correct 4 ms 1992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 7 ms 2396 KB 3rd lines differ - on the 1st token, expected: '1', found: '0'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 9 ms 2652 KB 3rd lines differ - on the 1st token, expected: '1', found: '0'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 1884 KB 3rd lines differ - on the 42nd token, expected: '0', found: '1'
2 Halted 0 ms 0 KB -