Submission #860159

# Submission time Handle Problem Language Result Execution time Memory
860159 2023-10-11T20:46:03 Z ThegeekKnight16 Chase (CEOI17_chase) C++17
40 / 100
4000 ms 491236 KB
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,tune=native")
#define int long long
const int MAXN = 1e5 + 10;
const int MAXV = 101;
array<array<array<int, 3>, MAXV>, MAXN> dp;
array<vector<int>, MAXN> grafo;
array<int, MAXN> pom, pomviz;
int N, V;
int resp = 0;

void dfs(int v, int p)
{
    dp[v].fill({0, 0, 0});
    pomviz[v] = 0;
    for (auto viz : grafo[v])
    {
        if (viz == p) continue;
        pomviz[v] += pom[viz];
    }

    for (auto viz : grafo[v])
    {
        if (viz == p) continue;
        dfs(viz, v);
        for (int k = 1; k <= V; k++)
        {
            int prox = max(dp[viz][k][0], dp[viz][k-1][0] + pomviz[v]);
            auto &atual = dp[v][k];
            if (prox > atual[0])
            {
                swap(atual[2], atual[1]);
                swap(atual[1], atual[0]);
                atual[0] = prox;
            }
            else if (prox > atual[1])
            {
                swap(atual[2], atual[1]);
                atual[1] = prox;
            }
            else if (prox > atual[2])
            {
                atual[2] = prox;
            }
        }
    }
}

void girarArvore(int v, int p)
{
    // cerr << "v: " << v << '\n';
    // cerr << '\t' << "dp: ";
    // for (auto x : dp[v][V]) cerr << get<0>(x) << " ";
    // cerr << '\n';
    // cerr << '\t' << "pomviz: " << pomviz[v] << '\n';
    resp = max(resp, dp[v][V][0]);

    for (auto viz : grafo[v])
    {
        if (viz == p) continue;

        //Salva
        auto antv = dp[v]; int antpom = pomviz[v];
        auto antviz = dp[viz]; int antpomviz = pomviz[viz];

        //Gira
        pomviz[v] -= pom[viz]; pomviz[viz] += pom[v];
        dp[v].fill({0, 0, 0}); dp[viz].fill({0, 0, 0});
        for (auto viz2 : grafo[v])
        {
            for (int k = 1; k <= V; k++)
            {
                auto &atual = dp[v][k];
                if (viz2 == viz) continue;
                int prox = max(dp[viz2][k][0], dp[viz2][k-1][0] + pomviz[v]);
                if (prox > atual[0])
                {
                    swap(atual[2], atual[1]);
                    swap(atual[1], atual[0]);
                    atual[0] = prox;
                }
                else if (prox > atual[1])
                {
                    swap(atual[2], atual[1]);
                    atual[1] = prox;
                }
                else if (prox > atual[2])
                {
                    atual[2] = prox;
                }
            }
        }
        for (auto viz2 : grafo[viz])
        {
            for (int k = 1; k <= V; k++)
            {
                auto &atual = dp[viz][k];
                int prox = max(dp[viz2][k][0], dp[viz2][k-1][0] + pomviz[viz]);
                if (prox > atual[0])
                {
                    swap(atual[2], atual[1]);
                    swap(atual[1], atual[0]);
                    atual[0] = prox;
                }
                else if (prox > atual[1])
                {
                    swap(atual[2], atual[1]);
                    atual[1] = prox;
                }
                else if (prox > atual[2])
                {
                    atual[2] = prox;
                }
            }
        }

        girarArvore(viz, v);

        //Volta
        dp[v] = antv; dp[viz] = antviz;
        pomviz[v] = antpom; pomviz[viz] = antpomviz;
    }
}

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> N >> V;
    for (int i = 1; i <= N; i++) cin >> pom[i];
    for (int i = 1; i < N; i++)
    {
        int X, Y;
        cin >> X >> Y;
        grafo[X].push_back(Y);
        grafo[Y].push_back(X);
    }
    dfs(1, 0);
    girarArvore(1, 1);
    cout << resp << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 4 ms 9052 KB Output is correct
8 Correct 3 ms 9052 KB Output is correct
9 Correct 12 ms 6492 KB Output is correct
10 Correct 3 ms 6492 KB Output is correct
11 Correct 2 ms 6492 KB Output is correct
12 Correct 2 ms 6492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 406 ms 491236 KB Output is correct
2 Correct 394 ms 490068 KB Output is correct
3 Execution timed out 4030 ms 245448 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 4 ms 9052 KB Output is correct
8 Correct 3 ms 9052 KB Output is correct
9 Correct 12 ms 6492 KB Output is correct
10 Correct 3 ms 6492 KB Output is correct
11 Correct 2 ms 6492 KB Output is correct
12 Correct 2 ms 6492 KB Output is correct
13 Correct 406 ms 491236 KB Output is correct
14 Correct 394 ms 490068 KB Output is correct
15 Execution timed out 4030 ms 245448 KB Time limit exceeded
16 Halted 0 ms 0 KB -