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;
#define X first
#define Y second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <int, pii> pip;
typedef pair <pii, int> ppi;
typedef pair <ll, ll> pll;
const int INF=0x3f3f3f3f;
const ll MAX=(ll)INF*INF;
const int N=1e4+5;
int n;
vector <pii> ls[N];
int dp[N][2];
int rek(int node, int fl, int par) {
int &ret=dp[node][fl];
if (ret!=-1) return ret;
int sum0=0, ed;
for (pii sus:ls[node]) {
if (sus.X==par) {
ed=sus.Y;
continue;
}
sum0+=rek(sus.X, 0, node);
}
ret=sum0;
for (pii sus:ls[node]) {
if (sus.X==par) continue;
if (!fl) ret=max(ret, ed+sus.Y+sum0-rek(sus.X, 0, node)+rek(sus.X, 1, node));
}
return ret;
}
void pokreni(int node, int par) {
rek(node, 0, par);
rek(node, 1, par);
for (pii sus:ls[node]) if (sus.X!=par) pokreni(sus.X, node);
}
int dfs(int node, int par, int val0, int val1) {
int sum0=val0, ed=0;
// printf("node: %d, val0: %d, val1: %d\n", node, val0, val1);
for (pii sus:ls[node]) {
if (sus.X==par) {
ed=sus.Y;
continue;
}
sum0+=dp[sus.X][0];
}
int ret=sum0;
int maxi[2]={sum0+ed-val0+val1, 0};
if (!par) maxi[0]=0;
// printf("node: %d, sum0: %d, maxi0: %d, maxi1: %d\n", node, sum0, maxi[0], maxi[1]);
for (pii sus:ls[node]) {
if (sus.X==par) continue;
int x=sum0+sus.Y-dp[sus.X][0]+dp[sus.X][1];
// printf("sus: %d, x == %d\n", sus.X, x);
for (int i=0; i<2; ++i) if (x>maxi[i]) swap(x, maxi[i]);
}
// printf("maxi0: %d, maxi1: %d\n", maxi[0], maxi[1]);
int p0, p1=sum0;
for (pii sus:ls[node]) {
if (sus.X==par) continue;
int curr=sum0+sus.Y-dp[sus.X][0]+dp[sus.X][1];
// printf("sus: %d, curr: %d\n", sus.X, curr);
if (maxi[0]==curr) p0=maxi[1]+sus.Y-dp[sus.X][0];
else p0=maxi[0]+sus.Y-dp[sus.X][0];
// printf("dijete: %d, p0 == %d, p1 == %d\n", sus.X, p0, p1-dp[sus.X][0]);
ret=max(ret, dfs(sus.X, node, max(0, p0), p1-dp[sus.X][0]));
}
// printf("node: %d, sum0: %d, ret: %d\n", node, sum0, ret);
return ret;
}
void load() {
scanf("%d", &n);
for (int i=0; i<n-1; ++i) {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
ls[a].pb(mp(b, c));
ls[b].pb(mp(a, c));
}
}
int main() {
load();
memset(dp, -1, sizeof dp);
pokreni(1, 0);
int p[2]={0, 0};
printf("%d\n", dfs(1, 0, 0, 0));
return 0;
}
Compilation message (stderr)
beads.cpp: In function 'int main()':
beads.cpp:101:9: warning: unused variable 'p' [-Wunused-variable]
101 | int p[2]={0, 0};
| ^
beads.cpp: In function 'void load()':
beads.cpp:88:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
88 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
beads.cpp:91:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
91 | scanf("%d %d %d", &a, &b, &c);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
beads.cpp: In function 'int rek(int, int, int)':
beads.cpp:38:33: warning: 'ed' may be used uninitialized in this function [-Wmaybe-uninitialized]
38 | if (!fl) ret=max(ret, ed+sus.Y+sum0-rek(sus.X, 0, node)+rek(sus.X, 1, node));
| ^
# | 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... |