# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
152552 | pink_bittern | Beads and wires (APIO14_beads) | C++14 | 9 ms | 7544 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include <complex>
#define pb push_back
#define pll pair <ll, ll>
#define MOMI using namespace std;
#define mp make_pair
#define pyshnapyshnakaa ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
#pragma optimize("TKACHENKO-GORYACHENKO")
#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
//#define double long double
typedef long long ll;
typedef long double ld;
using namespace std;
const ll maxn = 300000;
const ll inf = 1e15;
struct edge{
ll to;
ll c;
};
ll n, m, k;
ll D[maxn][2];
ll C[maxn];
vector <edge> V[maxn];
void dfs(ll v, ll p, ll c){
ll q;
ll s = 0;
for (q = 0; q < V[v].size(); q++){
ll v1 = V[v][q].to;
if (v1 == p){
continue;
}
dfs(v1, v, V[v][q].c);
s += D[v1][1];
}
ll mx1 = -inf, mx2 = -inf;
for (q = 0; q < V[v].size(); q++){
ll v1 = V[v][q].to;
if (v1 == p){
continue;
}
ll dmx = D[v1][0] + V[v][q].c - D[v1][1];
if (mx1 < dmx){
mx2 = mx1;
mx1 = dmx;
continue;
}
if (mx2 < dmx){
mx2 = dmx;
}
}
D[v][0] = max(D[v][0], s + mx1 + mx2);
D[v][1] = max(max(D[v][1], D[v][0]), s + mx1 + c);
}
int main(){
ll q, w, e, t, a, b, c;
cin >> n;
for (q = 0; q < n - 1; q++){
cin >> a >> b >> c;
a--; b--;
edge e, f;
e.to = b; f.to = a;
e.c = f.c = c;
V[a].pb(e); V[b].pb(f);
C[a] += c; C[b] += c;
}
// cout << "END " << endl;
dfs(0, 0, -inf);
cout << D[0][0];
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |