Submission #31616

#TimeUsernameProblemLanguageResultExecution timeMemory
31616minhtung0404Beads and wires (APIO14_beads)C++14
28 / 100
1075 ms5504 KiB
#include<bits/stdc++.h>
const int N = 2e5 + 5;
const int inf = 1e9;
using namespace std;

typedef pair <int, int> ii;
vector <ii> adj[N];
int n, f[N][2], g[N], p[N], a, b, c, maxx;

void dfs(int u){
    int maxx = -inf;
    f[u][1] = -inf;
    f[u][0] = 0;
    for(int i = 0; i < adj[u].size(); i++){
        int v = adj[u][i].first;
        if (v == p[u]) continue;
        p[v] = u;
        dfs(v);
    }
    for (int i = 0; i < adj[u].size(); i++){
        int v = adj[u][i].first, cost = adj[u][i].second;
        if (v == p[u]) continue;
        f[u][0] += max(f[v][1]+cost, f[v][0]);
        if (f[v][1]+cost > f[v][0]) maxx = max(maxx, f[v][0] - f[v][1]);
        else maxx = max(maxx, cost);
    }
    f[u][1] = f[u][0] + maxx;
}

int main(){
    cin >> n;
    for (int i = 1; i < n; i++){
        cin >> a >> b >> c;
        adj[a].push_back(ii(b, c));
        adj[b].push_back(ii(a, c));
    }
    for (int i = 1; i <= n; i++){
        p[i] = 0;
        dfs(i);
        maxx = max(maxx, f[i][0]);
    }
    cout << maxx;
}
/*
5
1 2 10
1 3 40
1 4 15
1 5 20

10
4 10 2
1 2 21
1 3 13
6 7 1
7 9 5
2 4 3
2 5 8
1 6 55
6 8 34
*/

Compilation message (stderr)

beads.cpp: In function 'void dfs(int)':
beads.cpp:14:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < adj[u].size(); i++){
                    ~~^~~~~~~~~~~~~~~
beads.cpp:20:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < adj[u].size(); i++){
                     ~~^~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...