Submission #967385

# Submission time Handle Problem Language Result Execution time Memory
967385 2024-04-22T03:58:00 Z steveonalex Beads and wires (APIO14_beads) C++17
0 / 100
3 ms 6060 KB
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
 
#define ALL(v) (v).begin(), (v).end()
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
 
// mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng(1);
ll rngesus(ll l, ll r){return ((ull) rng()) % (r - l + 1) + l;}
 
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
 
ll LASTBIT(ll mask){return mask & (-mask);}
ll pop_cnt(ll mask){return __builtin_popcountll(mask);}
ll ctz(ll mask){return __builtin_ctzll(mask);}
ll clz(ll mask){return __builtin_clzll(mask);}
ll logOf(ll mask){return 63 - clz(mask);}
 
template <class T1, class T2>
    bool minimize(T1 &a, T2 b){
        if (a > b){a = b; return true;}
        return false;
    }
template <class T1, class T2>
    bool maximize(T1 &a, T2 b){
        if (a < b){a = b; return true;}
        return false;
    }
template <class T>
    void printArr(T& a, string separator = " ", string finish = "\n", ostream& out = cout){
        for(auto i: a) out << i << separator;
        out << finish;
    }
template <class T>
    void remove_dup(vector<T> &a){
        sort(ALL(a));
        a.resize(unique(ALL(a)) - a.begin());
    }

const int N = 2e5 + 69;
const ll INF = 1e18 + 69;

int n;
vector<pair<int, int>> graph[N];
ll dp[N][2];

void dfs(int u, int p){
    for(pair<int, int> v: graph[u]) if (v.first != p){
        dfs(v.first, u);
    }

    for(pair<int, int> v: graph[u]) if (v.first != p){
        dp[u][0] += max(dp[v.first][1] + v.second, dp[v.first][0]);
        ll cu = 0;
        for(pair<int, int> w: graph[u]) if (w.first != p){
            if (w.first == v.first) cu += v.second + dp[v.first][0];
            else cu += max(dp[w.first][1] + w.second, dp[w.first][0]);
        }
        maximize(dp[u][1], cu);
    }
    for(pair<int, int> v1: graph[u]) if (v1.first != p){
        for(pair<int, int> v2: graph[u]) if (v2.first != p){
            if (v2 >= v1) break;
            ll cu = 0;
            for(pair<int, int> v: graph[u]) if (v.first != p){
                if (v1 == v || v2 == v){
                    cu += v.second + dp[v.first][0];
                }
                else cu += max(dp[v.first][1] + v.second, dp[v.first][0]);
            }
            maximize(dp[u][0], cu);

        }
    }
}

int main(void){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    cin >> n;
    for(int i = 1; i<n; ++i){
        int u, v, w; cin >> u >> v >> w;
        graph[u].push_back({v, w});
        graph[v].push_back({u, w});
    }

    for(int i = 1; i<=n; ++i) sort(ALL(graph[i]));

    for(int i = 1; i<=n; ++i) dp[i][1] = -INF;
    dfs(1, 1);

    cout << dp[1][0] << "\n";

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5980 KB Output is correct
2 Correct 2 ms 5980 KB Output is correct
3 Correct 2 ms 6060 KB Output is correct
4 Correct 2 ms 5980 KB Output is correct
5 Correct 2 ms 5980 KB Output is correct
6 Incorrect 2 ms 5980 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5980 KB Output is correct
2 Correct 2 ms 5980 KB Output is correct
3 Correct 2 ms 6060 KB Output is correct
4 Correct 2 ms 5980 KB Output is correct
5 Correct 2 ms 5980 KB Output is correct
6 Incorrect 2 ms 5980 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5980 KB Output is correct
2 Correct 2 ms 5980 KB Output is correct
3 Correct 2 ms 6060 KB Output is correct
4 Correct 2 ms 5980 KB Output is correct
5 Correct 2 ms 5980 KB Output is correct
6 Incorrect 2 ms 5980 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5980 KB Output is correct
2 Correct 2 ms 5980 KB Output is correct
3 Correct 2 ms 6060 KB Output is correct
4 Correct 2 ms 5980 KB Output is correct
5 Correct 2 ms 5980 KB Output is correct
6 Incorrect 2 ms 5980 KB Output isn't correct
7 Halted 0 ms 0 KB -