#include<bits/stdc++.h>
#define taskname "A"
#define pb push_back
#define mp make_pair
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int,int> ii;
const int maxn = 2e5 + 5;
int n;
ll dp[maxn][2][2];
vector<ii> adj[maxn];
void dfs(int u , int par , int parcost){
for(auto & c : adj[u]){
if(c.first == par)continue;
dfs(c.first , u , c.second);
}
{
vector<ll> val;
for(auto & c : adj[u]){
if(c.first == par)continue;
dp[u][1][0] += dp[c.first][0][0];
dp[u][1][1] += dp[c.first][0][0];
val.pb(max(dp[c.first][1][0] , dp[c.first][1][1]) + c.second - dp[c.first][0][0]);
}
sort(val.begin(),val.end(),greater<ll>());
if(val.size() >= 2)dp[u][1][1] = max(dp[u][1][1] , dp[u][1][1] + val[0] + val[1]);
}
{
vector<ll> val;
for(auto & c : adj[u]){
if(c.first == par)continue;
dp[u][0][0] += dp[c.first][0][0];
val.pb(dp[c.first][1][0] + c.second - dp[c.first][0][0]);
}
sort(val.begin(),val.end(),greater<ll>());
if(val.size() >= 1)dp[u][0][0] = max(dp[u][0][0] , dp[u][0][0] + parcost + val[0]);
else dp[u][0][0] = -1e9;
dp[u][0][0] = max(dp[u][0][0] , dp[u][1][0]);
}
{
vector<ll> val;
for(auto & c : adj[u]){
if(c.first == par)continue;
dp[u][0][1] += dp[c.first][0][0];
val.pb(dp[c.first][1][1] + c.second - dp[c.first][0][0]);
}
sort(val.begin(),val.end(),greater<ll>());
if(val.size() >= 1)dp[u][0][1] = max(dp[u][0][1] , dp[u][0][1] + parcost + val[0]);
else dp[u][0][1] = -1e9;
dp[u][0][1] = max(dp[u][0][1] , dp[u][1][1]);
dp[u][0][1] = max(dp[u][0][1] , dp[u][0][0]);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
if(fopen(taskname".inp", "r")) {
freopen(taskname".inp", "r", stdin);
freopen(taskname".out", "w", stdout);
}
cin >> n;
for(int i = 1 ; i < n ; ++i){
int u , v , c;cin >> u >> v >> c;
adj[u].pb(mp(v,c));adj[v].pb(mp(u , c));
}
dfs(1 , 0 , 0);
cout << max(dp[1][1][0] , dp[1][1][1]);
}
Compilation message
beads.cpp: In function 'int main()':
beads.cpp:68:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen(taskname".inp", "r", stdin);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
beads.cpp:69:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen(taskname".out", "w", stdout);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
4992 KB |
Output is correct |
3 |
Incorrect |
4 ms |
5024 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
4992 KB |
Output is correct |
3 |
Incorrect |
4 ms |
5024 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
4992 KB |
Output is correct |
3 |
Incorrect |
4 ms |
5024 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
4992 KB |
Output is correct |
3 |
Incorrect |
4 ms |
5024 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |