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>
#define double long double
//T.erase(unique(T.begin(),T.end()),T.end());
//written by djs100201
#define all(v) v.begin(),v.end()
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using PP = pair<ll, P>;
const ll n_ = 2e5 + 100, inf = (ll)1e9 * (ll)1e5 + 7, mod = 1e9 + 7, sqrtN = 320;
ll dy[4] = { -1,0,1,0 }, dx[4] = { 0,1,0,-1 };
ll n, m, k, tc = 1, a, b, c, d, sum, x, y, z, base, ans, q;
ll gcd(ll x, ll y) {
if (!y)return x;
return gcd(y, x % y);
}
vector<P>v[n_];
ll dp[n_][2][2];
void dfs(ll x, ll par, ll cost) {
ll val1 = 0, val2 = -inf;
ll t1 = -inf, t2 = -inf;
ll a1 = 0, a2 = -inf;
ll sum1 = 0, sum2 = -inf;
priority_queue<ll>pq;
for (auto nxt : v[x]) {
if (nxt.first == par)continue;
dfs(nxt.first, x, nxt.second);
ll val = max(dp[nxt.first][0][0], dp[nxt.first][1][0]);
dp[x][0][0] += val;
dp[x][1][0] += val;
dp[x][0][1] += val;
dp[x][1][1] += val;
ll c1 = dp[nxt.first][0][1] + nxt.second - val;
ll c2 = dp[nxt.first][0][0] + nxt.second - val;
pq.push(c2);
sum1 = max(sum1, t1 + c2);
sum1 = max(sum1, t2 + c2);
sum1 = max(sum1, t2 + c1);
sum1 = max(sum1, max(dp[nxt.first][1][1],dp[nxt.first][0][1]) - val);
t1 = max(t1, c1);
t2 = max(t2, c2);
}
if (pq.size()) {
ll val = pq.top();
dp[x][1][0] += val + cost;
}
else dp[x][1][0] = -inf;
dp[x][0][1] += sum1;
dp[x][1][1] += t1 + cost;
}
void solve() {
cin >> n;
for (int i = 1; i < n; i++) {
cin >> a >> b >> c;
v[a].push_back({ b,c });
v[b].push_back({ a,c });
}
dfs(1, 0, -inf);
cout << max(dp[1][0][0], dp[1][0][1]);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
//cin >> tc;
while (tc--)solve();
}
Compilation message (stderr)
beads.cpp: In function 'void dfs(ll, ll, ll)':
beads.cpp:22:5: warning: unused variable 'val1' [-Wunused-variable]
22 | ll val1 = 0, val2 = -inf;
| ^~~~
beads.cpp:22:15: warning: unused variable 'val2' [-Wunused-variable]
22 | ll val1 = 0, val2 = -inf;
| ^~~~
beads.cpp:24:5: warning: unused variable 'a1' [-Wunused-variable]
24 | ll a1 = 0, a2 = -inf;
| ^~
beads.cpp:24:13: warning: unused variable 'a2' [-Wunused-variable]
24 | ll a1 = 0, a2 = -inf;
| ^~
beads.cpp:25:15: warning: unused variable 'sum2' [-Wunused-variable]
25 | ll sum1 = 0, sum2 = -inf;
| ^~~~
# | 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... |