Submission #1056654

#TimeUsernameProblemLanguageResultExecution timeMemory
1056654PanosPaskBeads and wires (APIO14_beads)C++14
28 / 100
1053 ms1628 KiB
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair

using namespace std;

typedef pair<int, int> pi;

const int INF = 1e9;

int N;
vector<vector<pi>> adj_list;

/* dp[node][is_middle]:
 * The maximum score in the subtree of node.
 * is_middle is a boolean variable denoting if node was inserted in the middle via insert operation
 */
vector<vector<int>> dp;

void process_subtree(int node, int par)
{
    dp[node][0] = 0;
    dp[node][1] = -INF;
    
    // Maximum gain from using one of the kids as an endpoint for a red edge starting at par
    // and then inserting node and making the edges blue
    int opt_follow = -INF;

    for (auto [neigh, w] : adj_list[node]) {
        if (neigh == par) {
            continue;
        }

        process_subtree(neigh, node);

        // Gain is the maximum we can get
        int gain = max(dp[neigh][1] + w, dp[neigh][0]);

        dp[node][0] += max(dp[neigh][1] + w, dp[neigh][0]);

        opt_follow = max(opt_follow, dp[neigh][0] + w - gain);
    }

    if (opt_follow != -INF) {
        dp[node][1] = dp[node][0] + opt_follow;
    }
}

int main(void)
{
    scanf("%d", &N);

    adj_list.resize(N);
    dp.resize(N, vector<int>(2, -INF));

    for (int i = 0; i < N - 1; i++) {
        int u, v, w;
        scanf("%d %d %d", &u, &v, &w);
        u--; v--;

        adj_list[u].pb(mp(v, w));
        adj_list[v].pb(mp(u, w));
    }

    int ans = 0;
    for (int i = 0; i < N; i++) {
        process_subtree(i, i);
        ans = max(ans, dp[i][0]);
    }

    printf("%d\n", ans);

    return 0;
}

Compilation message (stderr)

beads.cpp: In function 'void process_subtree(int, int)':
beads.cpp:29:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   29 |     for (auto [neigh, w] : adj_list[node]) {
      |               ^
beads.cpp: In function 'int main()':
beads.cpp:51:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   51 |     scanf("%d", &N);
      |     ~~~~~^~~~~~~~~~
beads.cpp:58:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   58 |         scanf("%d %d %d", &u, &v, &w);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...