Submission #289284

# Submission time Handle Problem Language Result Execution time Memory
289284 2020-09-02T14:10:17 Z SamAnd Toy Train (IOI17_train) C++17
16 / 100
36 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] == 0)
            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 (z4)
        return solv4();
    if (z3)
        return solv3();
    if (z1)
        return solv1();
}

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 15 ms 21248 KB Output is correct
2 Correct 15 ms 21248 KB Output is correct
3 Correct 16 ms 21248 KB Output is correct
4 Correct 16 ms 21260 KB Output is correct
5 Correct 18 ms 21248 KB Output is correct
6 Correct 19 ms 21248 KB Output is correct
7 Correct 20 ms 21504 KB Output is correct
8 Correct 16 ms 21504 KB Output is correct
9 Correct 20 ms 21248 KB Output is correct
10 Correct 16 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 20 ms 21376 KB Output is correct
2 Correct 20 ms 22272 KB Output is correct
3 Correct 23 ms 23168 KB Output is correct
4 Correct 36 ms 25848 KB Output is correct
5 Correct 29 ms 25600 KB Output is correct
6 Correct 25 ms 25472 KB Output is correct
7 Correct 33 ms 25088 KB Output is correct
8 Correct 23 ms 23936 KB Output is correct
9 Correct 28 ms 22784 KB Output is correct
10 Correct 23 ms 24192 KB Output is correct
11 Correct 23 ms 23936 KB Output is correct
12 Correct 28 ms 25464 KB Output is correct
13 Correct 26 ms 25600 KB Output is correct
14 Correct 27 ms 25592 KB Output is correct
15 Correct 26 ms 25720 KB Output is correct
16 Correct 26 ms 25592 KB Output is correct
17 Correct 26 ms 25720 KB Output is correct
18 Correct 21 ms 24704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 23 ms 24832 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 23 ms 25344 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 15 ms 21248 KB Output is correct
2 Correct 15 ms 21248 KB Output is correct
3 Correct 16 ms 21248 KB Output is correct
4 Correct 16 ms 21260 KB Output is correct
5 Correct 18 ms 21248 KB Output is correct
6 Correct 19 ms 21248 KB Output is correct
7 Correct 20 ms 21504 KB Output is correct
8 Correct 16 ms 21504 KB Output is correct
9 Correct 20 ms 21248 KB Output is correct
10 Correct 16 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 -