답안 #832339

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
832339 2023-08-21T09:22:24 Z finn__ 장난감 기차 (IOI17_train) C++17
27 / 100
8 ms 2076 KB
#include "train.h"
#include <bits/stdc++.h>
using namespace std;

constexpr size_t N = 5000;

vector<int> g[N], rg[N], postorder, scc[N], scc_non_charge[N];
bitset<N> visited, is_charging, f, deleted;
int ind[N];

void postorder_dfs(int u)
{
    visited[u] = 1;
    for (auto const &v : g[u])
        if (!visited[v] && !deleted[v])
            postorder_dfs(v);
    postorder.push_back(u);
}

void find_scc(int u, size_t s)
{
    visited[u] = 1;
    scc[s].push_back(u);
    ind[u] = s;
    for (auto const &v : rg[u])
        if (!visited[v])
            find_scc(v, s);
}

void find_scc_non_charge(int u, size_t s)
{
    visited[u] = 1;
    scc_non_charge[s].push_back(u);
    for (auto const &v : rg[u])
        if (!visited[v] && !deleted[v])
            find_scc_non_charge(v, s);
}

vector<int> who_wins(vector<int> a, vector<int> r, vector<int> u, vector<int> v)
{
    size_t const n = a.size(), m = u.size();
    int subtask = 1;
    for (size_t i = 0; i < m; ++i)
        if (u[i] != v[i] && u[i] + 1 != v[i])
        {
            subtask = 3;
            break;
        }

    vector<int> w(n);

    if (subtask == 1)
    {
        vector<bool> has_edge(n), has_loop(n);
        for (size_t i = 0; i < m; ++i)
        {
            if (u[i] == v[i])
                has_loop[u[i]] = 1;
            else
                has_edge[u[i]] = 1;
        }

        for (size_t i = 0; i < n; ++i)
        {
            for (size_t j = i; j < n; ++j)
                if (has_loop[j])
                {
                    if (!(a[j] ^ r[j]))
                    {
                        w[i] = a[j];
                        break;
                    }
                    if (!has_edge[j])
                    {
                        w[i] = !a[j];
                        break;
                    }
                }
        }

        return w;
    }

    for (size_t i = 0; i < n; ++i)
        is_charging[i] = r[i];
    for (size_t i = 0; i < m; ++i)
        g[u[i]].push_back(v[i]), rg[v[i]].push_back(u[i]);

    for (size_t i = 0; i < n; ++i)
        if (!visited[i])
            postorder_dfs(i);
    visited.reset();
    size_t s = 0;
    for (auto it = postorder.rbegin(); it != postorder.rend(); ++it)
        if (!visited[*it])
        {
            find_scc(*it, s++);
        }

    for (int &x : a)
        if (!x)
        {
            subtask = 4;
            break;
        }

    if (subtask == 3)
    {
        for (size_t i = 0; i < s; ++i)
        {
            bool has_charging_node = 0;
            for (int u : scc[i])
                has_charging_node |= r[u];
            bool has_cycle = scc[i].size() > 1;
            for (int u : scc[i])
                for (int v : g[u])
                    has_cycle |= u == v;
            if (has_cycle && has_charging_node)
            {
                f[i] = 1;
            }
        }
    }
    else
    {
        for (size_t i = 0; i < n; ++i)
            deleted[i] = r[i];
        visited.reset();
        postorder.clear();
        for (size_t i = 0; i < n; ++i)
            if (!visited[i] && !deleted[i])
                postorder_dfs(i);
        visited.reset();
        size_t t = 0;
        for (auto it = postorder.rbegin(); it != postorder.rend(); ++it)
            if (!visited[*it])
            {
                find_scc_non_charge(*it, t++);
            }

        for (size_t i = 0; i < t; ++i)
        {
            bool has_cycle = scc_non_charge[i].size() > 1;
            for (int u : scc_non_charge[i])
                for (int v : g[u])
                    has_cycle |= u == v;
            if (has_cycle)
            {
                f[ind[scc_non_charge[i][0]]] = 1;
            }
        }
    }

    for (size_t i = s - 1; i < s; --i)
        if (f[i])
        {
            for (auto const u : scc[i])
                for (auto const &v : rg[u])
                    f[ind[v]] = 1;
        }

    for (size_t i = 0; i < n; ++i)
        w[i] = f[ind[i]] ^ (subtask == 4);

    return w;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 980 KB Output is correct
2 Correct 3 ms 980 KB Output is correct
3 Correct 3 ms 980 KB Output is correct
4 Correct 4 ms 980 KB Output is correct
5 Correct 3 ms 980 KB Output is correct
6 Correct 3 ms 980 KB Output is correct
7 Correct 3 ms 980 KB Output is correct
8 Correct 3 ms 980 KB Output is correct
9 Correct 3 ms 980 KB Output is correct
10 Correct 2 ms 852 KB Output is correct
11 Correct 2 ms 852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 724 KB 3rd lines differ - on the 2nd token, expected: '1', found: '0'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 1996 KB Output is correct
2 Correct 5 ms 1876 KB Output is correct
3 Correct 5 ms 1876 KB Output is correct
4 Correct 6 ms 1876 KB Output is correct
5 Correct 6 ms 1748 KB Output is correct
6 Correct 8 ms 1700 KB Output is correct
7 Correct 6 ms 1676 KB Output is correct
8 Correct 6 ms 1680 KB Output is correct
9 Correct 5 ms 1620 KB Output is correct
10 Correct 6 ms 1536 KB Output is correct
11 Correct 7 ms 1620 KB Output is correct
12 Correct 6 ms 1704 KB Output is correct
13 Correct 7 ms 1876 KB Output is correct
14 Correct 6 ms 1876 KB Output is correct
15 Correct 6 ms 1876 KB Output is correct
16 Correct 6 ms 1884 KB Output is correct
17 Correct 6 ms 1876 KB Output is correct
18 Correct 5 ms 1620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 1620 KB Output is correct
2 Correct 6 ms 1828 KB Output is correct
3 Correct 7 ms 1876 KB Output is correct
4 Correct 7 ms 1612 KB Output is correct
5 Correct 7 ms 2004 KB Output is correct
6 Correct 6 ms 2004 KB Output is correct
7 Correct 7 ms 2004 KB Output is correct
8 Correct 6 ms 1876 KB Output is correct
9 Correct 6 ms 1816 KB Output is correct
10 Correct 7 ms 2068 KB Output is correct
11 Correct 6 ms 2004 KB Output is correct
12 Correct 6 ms 2076 KB Output is correct
13 Correct 7 ms 2072 KB Output is correct
14 Correct 6 ms 1964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 7 ms 1876 KB 3rd lines differ - on the 1st token, expected: '1', found: '0'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 980 KB Output is correct
2 Correct 3 ms 980 KB Output is correct
3 Correct 3 ms 980 KB Output is correct
4 Correct 4 ms 980 KB Output is correct
5 Correct 3 ms 980 KB Output is correct
6 Correct 3 ms 980 KB Output is correct
7 Correct 3 ms 980 KB Output is correct
8 Correct 3 ms 980 KB Output is correct
9 Correct 3 ms 980 KB Output is correct
10 Correct 2 ms 852 KB Output is correct
11 Correct 2 ms 852 KB Output is correct
12 Incorrect 1 ms 724 KB 3rd lines differ - on the 2nd token, expected: '1', found: '0'
13 Halted 0 ms 0 KB -