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>
using namespace std;
#pragma GCC target ("avx2")
#pragma GCC optimization ("Ofast")
#pragma GCC optimization ("unroll-loops")
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef long double ld;
typedef pair<ll, ll> pll;
#define FOR(i, a, b) for(int i = a; i < b; i++)
#define ROF(i, a, b) for(int i = a; i >= b; i--)
#define ms memset
#define pb push_back
#define fi first
#define se second
ll MOD = 1000000007;
ll power(ll base, ll n){
if (n == 0) return 1;
if (n == 1) return base;
ll halfn = power(base, n/2);
if (n % 2 == 0) return (halfn * halfn) % MOD;
return (((halfn * halfn) % MOD) * base) % MOD;
}
ll inverse(ll n){
return power(n, MOD-2);
}
ll add(ll a, ll b){
return (a+b) % MOD;
}
ll mul(ll a, ll b){
a %= MOD;
return (a*b) % MOD;
}
ll gcd(ll a, ll b){
if (a == 1) return 1;
if (a == 0) return b;
return gcd(b%a, a);
}
const int N = 10001;
vii g[N];
ll dp[N][2];
//dp[i][0] without edge i, with parent of i
//dp[i][1] with the edge
void dfs(int u, int p, int W){
dp[u][0]=0;dp[u][1]=0;
ll mini=MOD;
for (ii edge : g[u]){
int v=edge.fi; int w=edge.se;
if (v==p) continue;
dfs(v,u,w);
dp[u][0]+=max(dp[v][0],dp[v][1]);
mini=min(mini,dp[v][1]-dp[v][0]-w);
}
if (mini!=MOD) dp[u][1]=max(dp[u][0],dp[u][0]-mini+W);
}
void solve(){
int n; cin >> n;
FOR(i,0,n-1){
int u,v,w; cin>>u>>v>>w;
g[u].pb({v,w}); g[v].pb({u,w});
}
ll ans=0;
FOR(i,1,n+1){
dfs(i,0,-MOD);
ans=max(ans,max(dp[i][0],dp[i][1]));
}
cout<<ans<<'\n';
return;
}
int main() {
ios::sync_with_stdio(false);
int TC = 1;
//cin >> TC;
FOR(i, 1, TC+1){
//cout << "Case #" << i << ": ";
solve();
}
}
Compilation message (stderr)
beads.cpp:5: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
5 | #pragma GCC optimization ("Ofast")
|
beads.cpp:6: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
6 | #pragma GCC optimization ("unroll-loops")
|
# | 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... |