Submission #344526

# Submission time Handle Problem Language Result Execution time Memory
344526 2021-01-06T05:08:43 Z Sara Beads and wires (APIO14_beads) C++14
0 / 100
8 ms 9708 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define ll long long int
#define F first
#define S second
#define pb push_back

const ll N = 2e5 + 3;
const ll LOG = 19;
const int MOD = 1e9 + 7;
const ll INF = 1e9 + 5;

int n;
int dp[3][N], hlp[3][N];
vector < pair <int, int> > adj[N], g[N];
// dp[0][v] hichi nis
// dp[1][v] markaz yeki az bache ha be baba yeki dige be paiin
// dp[2][v] markaz ha do bache be paiin

int smax(int v){
    return max({dp[0][v], dp[1][v], dp[2][v]});
}
int gmax(int v){
    return max(dp[0][v], dp[2][v]);
}
void dfs(int v = 1, int p = 0, int w = 0){
    int sum = 0;
    for (auto i : adj[v]){
        int u = i.F;
        if (u == p) continue;
        g[v].pb(i);
        dfs(u, v, i.S);
        sum += smax(u);
    }

    dp[0][v] = sum; sum += w;
    int sz = g[v].size();
    if (!sz || v == 1) dp[1][v] = -INF;
    else {
        for (auto i : g[v]){
            int u = i.F;
            dp[1][v] = max(dp[1][v], sum - smax(u) + i.S + gmax(u));
        }
    }

    if (sz < 2) dp[2][v] = -INF;
    else {
        int a = g[v][0].F;
        int wa = g[v][0].S;
        int b = g[v][1].F;
        int wb = g[v][1].S;
        hlp[0][1] = smax(a) + smax(b);
        hlp[1][1] = max(wa + gmax(a) + smax(b), wb + smax(a) + gmax(b));
        hlp[2][1] = wa + wb + gmax(a) + gmax(b);
        for (int i = 2; i < sz; i ++){
            int u = g[v][i].F;
            int w = g[v][i].S;
            hlp[0][i] = hlp[0][i - 1] + smax(u);
            hlp[1][i] = max(hlp[1][i - 1] + smax(u), hlp[0][i - 1] + w + gmax(u));
            hlp[2][i] = max(hlp[2][i - 1] + smax(u), hlp[1][i - 1] + w + gmax(u));
        } 
        dp[2][v] = hlp[2][sz - 1];
    }
    return;
}

int main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> n;
    for (int i = 0; i < n - 1; i ++){
        int u, v, w;
        cin >> u >> v >> w;
        adj[u].pb({v, w});
        adj[v].pb({u, w});
    }
    dfs();
    cout << max(dp[0][1], dp[2][1]) << '\n';
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9708 KB Output is correct
2 Correct 8 ms 9708 KB Output is correct
3 Correct 7 ms 9708 KB Output is correct
4 Correct 7 ms 9708 KB Output is correct
5 Correct 7 ms 9708 KB Output is correct
6 Incorrect 7 ms 9708 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9708 KB Output is correct
2 Correct 8 ms 9708 KB Output is correct
3 Correct 7 ms 9708 KB Output is correct
4 Correct 7 ms 9708 KB Output is correct
5 Correct 7 ms 9708 KB Output is correct
6 Incorrect 7 ms 9708 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9708 KB Output is correct
2 Correct 8 ms 9708 KB Output is correct
3 Correct 7 ms 9708 KB Output is correct
4 Correct 7 ms 9708 KB Output is correct
5 Correct 7 ms 9708 KB Output is correct
6 Incorrect 7 ms 9708 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9708 KB Output is correct
2 Correct 8 ms 9708 KB Output is correct
3 Correct 7 ms 9708 KB Output is correct
4 Correct 7 ms 9708 KB Output is correct
5 Correct 7 ms 9708 KB Output is correct
6 Incorrect 7 ms 9708 KB Output isn't correct
7 Halted 0 ms 0 KB -