Submission #852637

# Submission time Handle Problem Language Result Execution time Memory
852637 2023-09-22T09:38:59 Z boris_mihov Closing Time (IOI23_closing) C++17
9 / 100
1000 ms 37724 KB
#include "closing.h"
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
#include <queue>

typedef long long llong;
const int MAXN = 500 + 10;
const llong INF = 8e18;
const int INTINF = 2e9;

int n;
int x;
int y;
llong k;
int sz[MAXN];
int par[MAXN];
bool isOnPath[MAXN];
int suffixSizes[MAXN];
std::vector <int> path;
std::vector <int> pathSizes;
std::vector <std::pair <int,int>> g[MAXN];
llong dpR[MAXN][2 * MAXN][2][2]; // root
llong dpC[MAXN][2 * MAXN][2][2]; // children, knapsack
llong dpS[MAXN][2 * MAXN][2][2]; // stick
bool blR[MAXN][2 * MAXN][2][2];
bool blC[MAXN][2 * MAXN][2][2];
bool blS[MAXN][2 * MAXN][2][2];
llong distX[MAXN];
llong distY[MAXN];

void reset()
{
    path.clear();
    pathSizes.clear();
    for (int i = 0 ; i < n ; ++i)
    {
        sz[i] = 0;
        g[i].clear();
        isOnPath[i] = false;
        distX[i] = distY[i] = 0;
        for (int j = 0 ; j <= 2 * n ; ++j)
        {
            for (int flagX = 0 ; flagX < 2 ; ++flagX)
            {
                for (int flagY = 0 ; flagY < 2 ; ++flagY)
                {
                    dpR[i][j][flagX][flagY] = 0;
                    dpC[i][j][flagX][flagY] = 0;
                    dpS[i][j][flagX][flagY] = 0;
                    blR[i][j][flagX][flagY] = false;
                    blC[i][j][flagX][flagY] = false;
                    blS[i][j][flagX][flagY] = false;
                }
            }
        }
    }
}

void dfs(int node, int par, llong to[], llong dist)
{
    to[node] = dist;
    for (const auto &[u, d] : g[node])
    {
        if (u == par)
        {
            continue;
        }

        dfs(u, node, to, dist + d);
    }
}

bool dfsPath(int node, int par)
{
    if (node == y)
    {
        path.push_back(node);
        return true;
    }

    for (const auto &[u, d] : g[node])
    {
        if (u == par)
        {
            continue;
        }

        if (dfsPath(u, node))
        {
            path.push_back(node);
            return true;
        }
    }

    return false;
}

void dfsSize(int node, int p)
{
    sz[node] = 1;
    par[node] = p;
    for (const auto &[u, d] : g[node])
    {
        if (p == u)
        {
            continue;
        }

        dfsSize(u, node);
        if (!isOnPath[u]) sz[node] += sz[u];
    }
}

llong fRoot(int, int, int, int);
llong fChildren(int, int, int, int, int);
llong fStick(int, int, int, int);

llong fRoot(int node, int choose, int flagX, int flagY)
{
    if (choose == 0)
    {
        return 0;
    }

    if (!flagX && !flagY)
    {
        return k + 1;
    }

    if (blR[node][choose][flagX][flagY])
    {
        return dpR[node][choose][flagX][flagY];
    }

    blR[node][choose][flagX][flagY] = true;
    dpR[node][choose][flagX][flagY] = fChildren(node, 0, choose, flagX, flagY);
    dpR[node][choose][flagX][flagY] = std::min(dpR[node][choose][flagX][flagY], k + 1);
    return dpR[node][choose][flagX][flagY];
}

llong fChildren(int node, int idx, int choose, int flagX, int flagY)
{
    if (idx == g[node].size())
    {
        if (choose == 0) return 0;
        return k + 1;
    }

    int current = g[node][idx].first;
    if (isOnPath[current] || current == par[node])
    {
        return fChildren(node, idx + 1, choose, flagX, flagY);
    }

    if (blC[current][choose][flagX][flagY])
    {
        return dpC[current][choose][flagX][flagY];
    }
    
    blC[current][choose][flagX][flagY] = true;
    dpC[current][choose][flagX][flagY] = k + 1;
    
    for (int currX = 0 ; currX <= flagX ; ++currX)
    {
        for (int currY = 0 ; currY <= flagY ; ++currY)
        {
            for (int subtreeChoose = std::max(choose - 2 * suffixSizes[(idx + 1 == g[node].size() ? 0 : g[node][idx + 1].first)], currX + currY) ; subtreeChoose <= std::min(2 * sz[g[node][idx].first], choose) ; ++subtreeChoose)
            {
                if (fChildren(node, idx + 1, choose - subtreeChoose, flagX, flagY) > k) continue;
                dpC[current][choose][flagX][flagY] = std::min(dpC[current][choose][flagX][flagY], fChildren(node, idx + 1, choose - subtreeChoose, flagX, flagY) + std::max((currX == 0 ? 0 : distX[current]), (currY == 0 ? 0 : distY[current])) + fRoot(current, subtreeChoose - currX - currY, currX, currY));
            }
        }
    }

    return dpC[current][choose][flagX][flagY];
}

llong fStick(int idx, int choose, int flagX, int flagY)
{
    if (idx == path.size())
    {
        if (choose == 0) return 0;
        return k + 1;
    }

    if (blS[idx][choose][flagX][flagY])
    {
        return dpS[idx][choose][flagX][flagY];
    }

    blS[idx][choose][flagX][flagY] = true;
    dpS[idx][choose][flagX][flagY] = k + 1;
    for (int currX = 0 ; currX <= flagX ; ++currX)
    {
        for (int currY = flagY ; currY < 2 ; ++currY)
        {
            for (int subtreeChoose = std::max(choose - 2 * pathSizes[idx + 1], currX + currY) ; subtreeChoose <= std::min(2 * sz[path[idx]], choose) ; ++subtreeChoose)
            {
                if (fStick(idx + 1, choose - subtreeChoose, currX, currY) > k) continue;
                dpS[idx][choose][flagX][flagY] = std::min(dpS[idx][choose][flagX][flagY], fStick(idx + 1, choose - subtreeChoose, currX, currY) + std::max((currX == 0 ? 0 : distX[path[idx]]), (currY == 0 ? 0 : distY[path[idx]])) + fRoot(path[idx], subtreeChoose - currX - currY, currX, currY));
            }
        }
    }

    return dpS[idx][choose][flagX][flagY];
}

int max_score(int N, int X, int Y, long long K, std::vector <int> U, std::vector <int> V, std::vector <int> W)
{
    reset();
    n = N;
    x = X;
    y = Y;
    k = K;

    for (int i = 0 ; i < n - 1 ; ++i)
    {
        g[U[i]].emplace_back(V[i], W[i]);
        g[V[i]].emplace_back(U[i], W[i]);
    }    

    dfs(x, -1, distX, 0);
    dfs(y, -1, distY, 0);
    dfsPath(x, -1);
    dfsSize(x, -1);

    for (int i = 0 ; i < n ; ++i)
    {
        for (int j = g[i].size() - 1 ; j >= 0 ; --j)
        {
            int add = 0;
            suffixSizes[g[i][j].first] = suffixSizes[(j + 1 == g[i].size() ? 0 : g[i][j + 1].first)];
            if (!isOnPath[g[i][j].first] && g[i][j].first != par[i])
            {
                add = sz[g[i][j].first];
            }

            suffixSizes[g[i][j].first] += add;
        }
    }

    std::reverse(path.begin(), path.end());
    for (const int &i : path)
    {
        isOnPath[i] = true;
    }

    pathSizes.resize(path.size() + 1, 0);
    for (int i = path.size() - 1 ; i >= 0 ; --i)
    {
        pathSizes[i] = pathSizes[i + 1] + sz[path[i]];
    }
    
    int begin = 1;
    int end = 2 * n;
    if (fStick(0, n, 1, 0) > k) end = n - 1;
    else begin = n;

    for (int i = end ; i >= begin ; --i)
    {
        if (fStick(0, i, 1, 0) <= k)
        {
            return i;
        }
    }

    return 0;
}

Compilation message

closing.cpp: In function 'llong fChildren(int, int, int, int, int)':
closing.cpp:146:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  146 |     if (idx == g[node].size())
      |         ~~~~^~~~~~~~~~~~~~~~~
closing.cpp:170:81: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  170 |             for (int subtreeChoose = std::max(choose - 2 * suffixSizes[(idx + 1 == g[node].size() ? 0 : g[node][idx + 1].first)], currX + currY) ; subtreeChoose <= std::min(2 * sz[g[node][idx].first], choose) ; ++subtreeChoose)
      |                                                                         ~~~~~~~~^~~~~~~~~~~~~~~~~
closing.cpp: In function 'llong fStick(int, int, int, int)':
closing.cpp:183:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  183 |     if (idx == path.size())
      |         ~~~~^~~~~~~~~~~~~~
closing.cpp: In function 'int max_score(int, int, int, long long int, std::vector<int>, std::vector<int>, std::vector<int>)':
closing.cpp:235:61: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  235 |             suffixSizes[g[i][j].first] = suffixSizes[(j + 1 == g[i].size() ? 0 : g[i][j + 1].first)];
      |                                                       ~~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 50 ms 10068 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 2 ms 10840 KB Output is correct
3 Correct 2 ms 10844 KB Output is correct
4 Correct 2 ms 8792 KB Output is correct
5 Correct 2 ms 12888 KB Output is correct
6 Correct 3 ms 11096 KB Output is correct
7 Correct 2 ms 11100 KB Output is correct
8 Correct 9 ms 13144 KB Output is correct
9 Correct 4 ms 13144 KB Output is correct
10 Correct 4 ms 9048 KB Output is correct
11 Correct 4 ms 9052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 2 ms 10840 KB Output is correct
3 Correct 2 ms 10844 KB Output is correct
4 Correct 2 ms 8792 KB Output is correct
5 Correct 2 ms 12888 KB Output is correct
6 Correct 3 ms 11096 KB Output is correct
7 Correct 2 ms 11100 KB Output is correct
8 Correct 9 ms 13144 KB Output is correct
9 Correct 4 ms 13144 KB Output is correct
10 Correct 4 ms 9048 KB Output is correct
11 Correct 4 ms 9052 KB Output is correct
12 Correct 16 ms 15192 KB Output is correct
13 Correct 4 ms 15196 KB Output is correct
14 Correct 49 ms 17496 KB Output is correct
15 Correct 9 ms 17240 KB Output is correct
16 Correct 20 ms 13144 KB Output is correct
17 Correct 20 ms 13272 KB Output is correct
18 Correct 11 ms 12888 KB Output is correct
19 Execution timed out 1047 ms 37724 KB Time limit exceeded
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 2 ms 10840 KB Output is correct
3 Correct 2 ms 10844 KB Output is correct
4 Correct 2 ms 8792 KB Output is correct
5 Correct 2 ms 12888 KB Output is correct
6 Correct 3 ms 11096 KB Output is correct
7 Correct 2 ms 11100 KB Output is correct
8 Correct 9 ms 13144 KB Output is correct
9 Correct 4 ms 13144 KB Output is correct
10 Correct 4 ms 9048 KB Output is correct
11 Correct 4 ms 9052 KB Output is correct
12 Correct 16 ms 15192 KB Output is correct
13 Correct 4 ms 15196 KB Output is correct
14 Correct 49 ms 17496 KB Output is correct
15 Correct 9 ms 17240 KB Output is correct
16 Correct 20 ms 13144 KB Output is correct
17 Correct 20 ms 13272 KB Output is correct
18 Correct 11 ms 12888 KB Output is correct
19 Execution timed out 1047 ms 37724 KB Time limit exceeded
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6488 KB Output is correct
3 Correct 2 ms 10840 KB Output is correct
4 Correct 2 ms 10844 KB Output is correct
5 Correct 2 ms 8792 KB Output is correct
6 Correct 2 ms 12888 KB Output is correct
7 Correct 1 ms 6488 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6488 KB Output is correct
10 Correct 1 ms 10840 KB Output is correct
11 Correct 2 ms 10840 KB Output is correct
12 Correct 1 ms 8792 KB Output is correct
13 Correct 2 ms 10844 KB Output is correct
14 Incorrect 1 ms 10844 KB 1st lines differ - on the 1st token, expected: '38', found: '34'
15 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6488 KB Output is correct
3 Correct 2 ms 10840 KB Output is correct
4 Correct 2 ms 10844 KB Output is correct
5 Correct 2 ms 8792 KB Output is correct
6 Correct 2 ms 12888 KB Output is correct
7 Correct 3 ms 11096 KB Output is correct
8 Correct 2 ms 11100 KB Output is correct
9 Correct 9 ms 13144 KB Output is correct
10 Correct 4 ms 13144 KB Output is correct
11 Correct 4 ms 9048 KB Output is correct
12 Correct 4 ms 9052 KB Output is correct
13 Correct 16 ms 15192 KB Output is correct
14 Correct 4 ms 15196 KB Output is correct
15 Correct 49 ms 17496 KB Output is correct
16 Correct 9 ms 17240 KB Output is correct
17 Correct 20 ms 13144 KB Output is correct
18 Correct 20 ms 13272 KB Output is correct
19 Correct 1 ms 6488 KB Output is correct
20 Correct 1 ms 6492 KB Output is correct
21 Correct 1 ms 6488 KB Output is correct
22 Correct 1 ms 10840 KB Output is correct
23 Correct 2 ms 10840 KB Output is correct
24 Correct 1 ms 8792 KB Output is correct
25 Correct 2 ms 10844 KB Output is correct
26 Incorrect 1 ms 10844 KB 1st lines differ - on the 1st token, expected: '38', found: '34'
27 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6488 KB Output is correct
3 Correct 2 ms 10840 KB Output is correct
4 Correct 2 ms 10844 KB Output is correct
5 Correct 2 ms 8792 KB Output is correct
6 Correct 2 ms 12888 KB Output is correct
7 Correct 3 ms 11096 KB Output is correct
8 Correct 2 ms 11100 KB Output is correct
9 Correct 9 ms 13144 KB Output is correct
10 Correct 4 ms 13144 KB Output is correct
11 Correct 4 ms 9048 KB Output is correct
12 Correct 4 ms 9052 KB Output is correct
13 Correct 16 ms 15192 KB Output is correct
14 Correct 4 ms 15196 KB Output is correct
15 Correct 49 ms 17496 KB Output is correct
16 Correct 9 ms 17240 KB Output is correct
17 Correct 20 ms 13144 KB Output is correct
18 Correct 20 ms 13272 KB Output is correct
19 Correct 11 ms 12888 KB Output is correct
20 Execution timed out 1047 ms 37724 KB Time limit exceeded
21 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6488 KB Output is correct
3 Correct 2 ms 10840 KB Output is correct
4 Correct 2 ms 10844 KB Output is correct
5 Correct 2 ms 8792 KB Output is correct
6 Correct 2 ms 12888 KB Output is correct
7 Correct 3 ms 11096 KB Output is correct
8 Correct 2 ms 11100 KB Output is correct
9 Correct 9 ms 13144 KB Output is correct
10 Correct 4 ms 13144 KB Output is correct
11 Correct 4 ms 9048 KB Output is correct
12 Correct 4 ms 9052 KB Output is correct
13 Correct 16 ms 15192 KB Output is correct
14 Correct 4 ms 15196 KB Output is correct
15 Correct 49 ms 17496 KB Output is correct
16 Correct 9 ms 17240 KB Output is correct
17 Correct 20 ms 13144 KB Output is correct
18 Correct 20 ms 13272 KB Output is correct
19 Correct 11 ms 12888 KB Output is correct
20 Execution timed out 1047 ms 37724 KB Time limit exceeded
21 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6488 KB Output is correct
3 Correct 2 ms 10840 KB Output is correct
4 Correct 2 ms 10844 KB Output is correct
5 Correct 2 ms 8792 KB Output is correct
6 Correct 2 ms 12888 KB Output is correct
7 Correct 3 ms 11096 KB Output is correct
8 Correct 2 ms 11100 KB Output is correct
9 Correct 9 ms 13144 KB Output is correct
10 Correct 4 ms 13144 KB Output is correct
11 Correct 4 ms 9048 KB Output is correct
12 Correct 4 ms 9052 KB Output is correct
13 Correct 16 ms 15192 KB Output is correct
14 Correct 4 ms 15196 KB Output is correct
15 Correct 49 ms 17496 KB Output is correct
16 Correct 9 ms 17240 KB Output is correct
17 Correct 20 ms 13144 KB Output is correct
18 Correct 20 ms 13272 KB Output is correct
19 Correct 11 ms 12888 KB Output is correct
20 Execution timed out 1047 ms 37724 KB Time limit exceeded
21 Halted 0 ms 0 KB -