This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//gross
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef pair<ll,ll> pl;
typedef vector<pl> vpl;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vpl> al;
struct EG {
ll st,ed,wo;
};
const ll INFL = 1e18;
const int MN = 200200;
ll dp[MN][2];
bool bp[MN][2];
vl lev;
vvi g;
void dfa(int u, int p) {
if(u != p) {
lev[u] = lev[p]+1;
}
for(int i=0;i<g[u].size();i++) {
int v = g[u][i];
if(v == p) {continue;}
dfa(v,u);
}
}
vl ko;
ll ds(int u, int p, int m) {
if(bp[u][m]) {
return dp[u][m];
}
ll res;
if(m) {
if(g[u].size() >= 1) {
res = 0;
ll ma = -INFL;
for(int i=0;i<g[u].size();i++) {
int v = g[u][i];
if(v == p) {continue;}
ll mv = max(ko[v]+ds(v,u,1),ds(v,u,0));
res += mv;
ma = max(ma,ko[v]+ds(v,u,0)-mv);
}
res += ma;
} else {
res = -INFL;
}
} else {
res = 0;
vl du;
for(int i=0;i<g[u].size();i++) {
int v = g[u][i];
if(v == p) {continue;}
ll mv = max(ds(v,u,0),ko[v]+ds(v,u,1));
res += mv;
du.push_back(ko[v]+ds(v,u,0)-mv);
}
if(du.size() >= 2) {
sort(du.rbegin(),du.rend());
if(du[0]+du[1] >= 0) {
res += du[0]+du[1];
}
}
}
bp[u][m] = true;
return dp[u][m] = res;
}
int main() {
ios::sync_with_stdio(0);cin.tie(0);
memset(bp,0,sizeof(bp));
ll n;
cin >> n;
lev.assign(n,0);
g.assign(n,vi());
ko.assign(n,0);
vector<EG> el;
for(int i=1;i<n;i++) {
ll a,b,c;
cin >> a >> b >> c;
a--;b--;
g[a].push_back(b);
g[b].push_back(a);
el.push_back({a,b,c});
}
dfa(0,0);
for(int i=0;i<el.size();i++) {
ll a = el[i].st;
ll b = el[i].ed;
if(lev[a] > lev[b]) {swap(a,b);}
ko[b] = el[i].wo;
}
cout << ds(0,0,0) << '\n';
}
Compilation message (stderr)
beads.cpp: In function 'void dfa(int, int)':
beads.cpp:24:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | for(int i=0;i<g[u].size();i++) {
| ~^~~~~~~~~~~~
beads.cpp: In function 'll ds(int, int, int)':
beads.cpp:40:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for(int i=0;i<g[u].size();i++) {
| ~^~~~~~~~~~~~
beads.cpp:54:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | for(int i=0;i<g[u].size();i++) {
| ~^~~~~~~~~~~~
beads.cpp: In function 'int main()':
beads.cpp:89:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<EG>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | for(int i=0;i<el.size();i++) {
| ~^~~~~~~~~~
# | 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... |