Submission #1289604

#TimeUsernameProblemLanguageResultExecution timeMemory
1289604sirnickToy Train (IOI17_train)C++20
Compilation error
0 ms0 KiB
#include <bits/stdc++.h>
using namespace std;

vector<int> who_wins(vector<int> a, vector<int> r, vector<int> u, vector<int> v) {
    int n = a.size();
    int m = u.size();

    vector<vector<int>> adj(n);
    for (int i = 0; i < m; i++)
        adj[u[i]].push_back(v[i]);

    // ---- Tarjan SCC ----
    vector<int> disc(n, -1), low(n), comp(n);
    vector<bool> onst(n, false);
    vector<int> st;
    int timer = 0, scc_count = 0;

    function<void(int)> dfs = [&](int u) {
        disc[u] = low[u] = timer++;
        st.push_back(u);
        onst[u] = true;

        for (int x : adj[u]) {
            if (disc[x] == -1) {
                dfs(x);
                low[u] = min(low[u], low[x]);
            } else if (onst[x]) {
                low[u] = min(low[u], disc[x]);
            }
        }

        if (low[u] == disc[u]) {
            while (true) {
                int w = st.back(); st.pop_back();
                onst[w] = false;
                comp[w] = scc_count;
                if (w == u) break;
            }
            scc_count++;
        }
    };

    for (int i = 0; i < n; i++)
        if (disc[i] == -1)
            dfs(i);

    // Build SCC DAG
    vector<vector<int>> cadj(scc_count);
    vector<bool> has_charge(scc_count, false);

    for (int i = 0; i < n; i++)
        if (r[i]) has_charge[comp[i]] = true;

    for (int i = 0; i < m; i++) {
        int cu = comp[u[i]];
        int cv = comp[v[i]];
        if (cu != cv) cadj[cu].push_back(cv);
    }

    // Reverse SCC edges
    vector<vector<int>> rcadj(scc_count);
    for (int c = 0; c < scc_count; c++)
        for (int nx : cadj[c])
            rcadj[nx].push_back(c);

    // Propagate winning SCCs: any SCC that reaches a SCC with charge becomes winning
    vector<int> win_scc(scc_count, false);
    queue<int> q;

    for (int c = 0; c < scc_count; c++)
        if (has_charge[c]) {
            win_scc[c] = true;
            q.push(c);
        }

    while (!q.empty()) {
        int c = q.front(); q.pop();
        for (int p : rcadj[c])
            if (!win_scc[p]) {
                win_scc[p] = true;
                q.push(p);
            }
    }

    vector<int> ans(n);
    for (int i = 0; i < n; i++)
        ans[i] = win_scc[comp[i]] ? 1 : 0;

    return ans;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;

    vector<int> a(n), r(n);
    for (int i = 0; i < n; i++) cin >> a[i];
    for (int i = 0; i < n; i++) cin >> r[i];

    int m;
    cin >> m;

    vector<int> u(m), v(m);
    for (int i = 0; i < m; i++)
        cin >> u[i] >> v[i];

    vector<int> ans = who_wins(a, r, u, v);

    for (int i = 0; i < n; i++)
        cout << ans[i] << " ";
    cout << "\n";
}

Compilation message (stderr)

/usr/bin/ld: /tmp/ccJl8Tm5.o: in function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/ccVEGhxD.o:train.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status