Submission #289280

# Submission time Handle Problem Language Result Execution time Memory
289280 2020-09-02T14:07:48 Z SamAnd Toy Train (IOI17_train) C++17
16 / 100
28 ms 25848 KB
#include "train.h"
#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
typedef long long ll;
const int N = 5003;

int n;
vector<int> a, r;
vector<int> g[N];
bool u[N][N];
vector<int> rg[N];

vector<int> solv1()
{
    vector<int> ans(n);
    for (int i = n - 1; i >= 0; --i)
    {
        if (a[i])
        {
            if (r[i])
            {
                if (u[i][i])
                {
                    ans[i] = 1;
                }
                else
                {
                    ans[i] = ans[i + 1];
                }
            }
            else
            {
                if (u[i][i + 1])
                {
                    ans[i] = ans[i + 1];
                }
                else
                {
                    ans[i] = 0;
                }
            }
        }
        else
        {
            if (!r[i])
            {
                if (u[i][i])
                {
                    ans[i] = 0;
                }
                else
                {
                    ans[i] = ans[i + 1];
                }
            }
            else
            {
                if (u[i][i + 1])
                {
                    ans[i] = ans[i + 1];
                }
                else
                {
                    ans[i] = 1;
                }
            }
        }
    }
    return ans;
}

int c[N];
vector<int> v;
void dfs0(int x)
{
    c[x] = 1;
    for (int i = 0; i < g[x].size(); ++i)
    {
        int h = g[x][i];
        if (!c[h])
            dfs0(h);
    }
    v.push_back(x);
}

int k;
void dfs1(int x)
{
    c[x] = k;
    for (int i = 0; i < rg[x].size(); ++i)
    {
        int h = rg[x][i];
        if (!c[h])
            dfs1(h);
    }
}

vector<int> xg[N];
int q[N];
void kond()
{
    for (int x = 0; x < n; ++x)
    {
        if (!c[x])
            dfs0(x);
    }
    reverse(all(v));
    memset(c, 0, sizeof c);
    for (int i = 0; i < n; ++i)
    {
        int x = v[i];
        if (!c[x])
        {
            ++k;
            dfs1(x);
        }
    }

    for (int x = 0; x < n; ++x)
    {
        for (int i = 0; i < g[x].size(); ++i)
        {
            int y = g[x][i];
            if (c[x] != c[y])
            {
                xg[c[x]].push_back(c[y]);
            }
            else
                ++q[c[x]];
        }
    }
}

int ans[N];
bool cc[N];
bool uu[N];
void dfs3(int x)
{
    cc[x] = true;
    if (q[x] && uu[x])
        ans[x] = 1;
    for (int i = 0; i < xg[x].size(); ++i)
    {
        int h = xg[x][i];
        if (!cc[h])
            dfs3(h);
        if (ans[h])
            ans[x] = 1;
    }
}

vector<int> solv3()
{
    kond();
    for (int x = 0; x < n; ++x)
    {
        if (r[x])
            uu[c[x]] = true;
    }
    for (int i = 1; i <= k; ++i)
    {
        if (!cc[i])
        {
            dfs3(i);
        }
    }

    vector<int> ansv;
    for (int x = 0; x < n; ++x)
    {
        ansv.push_back(ans[c[x]]);
    }
    return ansv;
}

void dfs2(int x)
{
    cc[x] = true;
    for (int i = 0; i < g[x].size(); ++i)
    {
        int h = g[x][i];
        if (r[h])
            continue;
        if (!cc[h])
            dfs2(h);
        else
            uu[c[x]] = true;
    }
}

void dfs4(int x)
{
    cc[x] = true;
    if (uu[x])
        ans[x] = 0;
    for (int i = 0; i < xg[x].size(); ++i)
    {
        int h = xg[x][i];
        if (!cc[h])
            dfs4(h);
        if (ans[h])
            ans[x] = 0;
    }
}

vector<int> solv4()
{
    kond();
    for (int x = 0; x < n; ++x)
    {
        if (r[x])
            continue;
        if (!cc[x])
            dfs2(x);
    }
    memset(cc, false, sizeof cc);
    for (int i = 1; i <= k; ++i)
        ans[i] = 1;
    for (int i = 1; i <= k; ++i)
    {
        if (!cc[i])
            dfs4(i);
    }

    vector<int> ansv;
    for (int x = 0; x < n; ++x)
    {
        ansv.push_back(ans[c[x]]);
    }
    return ansv;
}

std::vector<int> who_wins(std::vector<int> A, std::vector<int> R, std::vector<int> U, std::vector<int> V)
{
    a = A;
    r = R;

    n = sz(a);
    bool z1 = true;
    for (int i = 0; i < sz(U); ++i)
    {
        int x = U[i];
        int y = V[i];
        g[x].push_back(y);
        rg[y].push_back(x);
        u[x][y] = true;

        if (x != y && x + 1 != y)
            z1 = false;
    }

    bool z3 = true;
    bool z4 = true;
    for (int i = 0; i < n; ++i)
    {
        if (a[i])
        {
            z4 = false;
        }
        else
        {
            z3 = false;
        }
    }
    if (z3)
        return solv3();
    if (z1)
        return solv1();
    if (z4)
        return solv4();
}

Compilation message

train.cpp: In function 'void dfs0(int)':
train.cpp:82:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   82 |     for (int i = 0; i < g[x].size(); ++i)
      |                     ~~^~~~~~~~~~~~~
train.cpp: In function 'void dfs1(int)':
train.cpp:95:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   95 |     for (int i = 0; i < rg[x].size(); ++i)
      |                     ~~^~~~~~~~~~~~~~
train.cpp: In function 'void kond()':
train.cpp:126:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  126 |         for (int i = 0; i < g[x].size(); ++i)
      |                         ~~^~~~~~~~~~~~~
train.cpp: In function 'void dfs3(int)':
train.cpp:147:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  147 |     for (int i = 0; i < xg[x].size(); ++i)
      |                     ~~^~~~~~~~~~~~~~
train.cpp: In function 'void dfs2(int)':
train.cpp:184:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  184 |     for (int i = 0; i < g[x].size(); ++i)
      |                     ~~^~~~~~~~~~~~~
train.cpp: In function 'void dfs4(int)':
train.cpp:201:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  201 |     for (int i = 0; i < xg[x].size(); ++i)
      |                     ~~^~~~~~~~~~~~~~
train.cpp: In function 'std::vector<int> who_wins(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
train.cpp:276:1: warning: control reaches end of non-void function [-Wreturn-type]
  276 | }
      | ^
# Verdict Execution time Memory Grader output
1 Correct 16 ms 21248 KB Output is correct
2 Correct 15 ms 21376 KB Output is correct
3 Correct 17 ms 21248 KB Output is correct
4 Correct 16 ms 21248 KB Output is correct
5 Correct 15 ms 21248 KB Output is correct
6 Correct 17 ms 21248 KB Output is correct
7 Correct 17 ms 21248 KB Output is correct
8 Correct 17 ms 21504 KB Output is correct
9 Correct 20 ms 21248 KB Output is correct
10 Correct 15 ms 21248 KB Output is correct
11 Correct 15 ms 21248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 768 KB 3rd lines differ - on the 1st token, expected: '0', found: '1'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 26 ms 21352 KB Output is correct
2 Correct 21 ms 22272 KB Output is correct
3 Correct 28 ms 23168 KB Output is correct
4 Correct 25 ms 25848 KB Output is correct
5 Correct 27 ms 25592 KB Output is correct
6 Correct 26 ms 25464 KB Output is correct
7 Correct 24 ms 25088 KB Output is correct
8 Correct 23 ms 23936 KB Output is correct
9 Correct 22 ms 22784 KB Output is correct
10 Correct 24 ms 24192 KB Output is correct
11 Correct 23 ms 24000 KB Output is correct
12 Correct 25 ms 25384 KB Output is correct
13 Correct 26 ms 25600 KB Output is correct
14 Correct 25 ms 25728 KB Output is correct
15 Correct 25 ms 25720 KB Output is correct
16 Correct 25 ms 25600 KB Output is correct
17 Correct 24 ms 25728 KB Output is correct
18 Correct 20 ms 24704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 22 ms 24824 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 Incorrect 27 ms 25508 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 Correct 16 ms 21248 KB Output is correct
2 Correct 15 ms 21376 KB Output is correct
3 Correct 17 ms 21248 KB Output is correct
4 Correct 16 ms 21248 KB Output is correct
5 Correct 15 ms 21248 KB Output is correct
6 Correct 17 ms 21248 KB Output is correct
7 Correct 17 ms 21248 KB Output is correct
8 Correct 17 ms 21504 KB Output is correct
9 Correct 20 ms 21248 KB Output is correct
10 Correct 15 ms 21248 KB Output is correct
11 Correct 15 ms 21248 KB Output is correct
12 Incorrect 1 ms 768 KB 3rd lines differ - on the 1st token, expected: '0', found: '1'
13 Halted 0 ms 0 KB -