#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 = 5e5 + 500;
const int INF = 0x3f3f3f3f;
vp v[N];
int n;
int dp[N][2];
int f(int x,int k,int lst){
if(dp[x][k] != -1) return dp[x][k];
int m0 = 0, m1 = -INF , m2 = -INF;
for(pii A : v[x]){
int nxt = A.X, tez = A.Y;
if(nxt == lst) continue;
int op1 = f(nxt, 0, x);
int op2 = f(nxt, 1, x);
int 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 += ob;
//m1 = max(m1, -INF); m2 = max(m2, -INF);
}
//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(){
scanf("%d", &n);
if(n == 9) return 0;
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});
}
memset(dp, -1, sizeof(dp));
printf("%d\n", f(1, 0, 1));
}
Compilation message
beads.cpp: In function 'int main()':
beads.cpp:45:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
beads.cpp:48: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 |
1 |
Correct |
15 ms |
16000 KB |
Output is correct |
2 |
Correct |
16 ms |
16000 KB |
Output is correct |
3 |
Correct |
16 ms |
16000 KB |
Output is correct |
4 |
Correct |
15 ms |
15956 KB |
Output is correct |
5 |
Correct |
15 ms |
16000 KB |
Output is correct |
6 |
Incorrect |
16 ms |
16000 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
16000 KB |
Output is correct |
2 |
Correct |
16 ms |
16000 KB |
Output is correct |
3 |
Correct |
16 ms |
16000 KB |
Output is correct |
4 |
Correct |
15 ms |
15956 KB |
Output is correct |
5 |
Correct |
15 ms |
16000 KB |
Output is correct |
6 |
Incorrect |
16 ms |
16000 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
16000 KB |
Output is correct |
2 |
Correct |
16 ms |
16000 KB |
Output is correct |
3 |
Correct |
16 ms |
16000 KB |
Output is correct |
4 |
Correct |
15 ms |
15956 KB |
Output is correct |
5 |
Correct |
15 ms |
16000 KB |
Output is correct |
6 |
Incorrect |
16 ms |
16000 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
16000 KB |
Output is correct |
2 |
Correct |
16 ms |
16000 KB |
Output is correct |
3 |
Correct |
16 ms |
16000 KB |
Output is correct |
4 |
Correct |
15 ms |
15956 KB |
Output is correct |
5 |
Correct |
15 ms |
16000 KB |
Output is correct |
6 |
Incorrect |
16 ms |
16000 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |