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 <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#define X first
#define Y second
#define PB push_back
using namespace std;
typedef pair < int, int > pii;
typedef vector < pii > vp;
typedef long long ll;
const int N = 1e5 + 500;
const ll INF = 0x3f3f3f3f;
vp v[N];
int n;
ll dp[N][5];
ll f(int x,int k,int lst){
if(v[x].size() == (x != 1))
return (k == 1) * (-INF) * 100LL;
if(dp[x][k] != -1) return dp[x][k];
ll m0 = 0, m1 = -INF * 100LL, m2 = -INF * 100LL;
for(pii A : v[x]){
int nxt = A.X, tez = A.Y;
if(nxt == lst) continue;
ll op1 = f(nxt, 0, x);
ll op2 = f(nxt, 1, x);
ll ob = max(op1, op2 + tez);
//printf("X : %d nxt : %d op1 : )
m2 = max(m2 + ob, m1 + op1 + tez);
m1 = max(m1 + ob, m0 + op1 + tez);
m0 += max(op1, op2 + tez);
}
//printf("X = %d %lld %lld %lld\n", x, m0, m1, m2);
if(k == 1)
return dp[x][k] = m1;
return dp[x][k] = max(m0, m2);
}
int main(){
memset(dp, -1, sizeof(dp));
scanf("%d", &n);
for(int i = 1;i < n;i++){
int x, y, z; scanf("%d%d%d", &x, &y, &z);
v[x].PB({y, z});
v[y].PB({x, z});
}
printf("%lld\n", f(1, 0, 1));;
}
Compilation message (stderr)
beads.cpp: In function 'int main()':
beads.cpp:47:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
beads.cpp:49:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int x, y, z; scanf("%d%d%d", &x, &y, &z);
~~~~~^~~~~~~~~~~~~~~~~~~~~~
# | 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... |