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;
typedef long long ll;
#define int ll
#define rep(i, n) for(int i = 1; i <= n; ++i)
#define forn(i, l, r) for(int i = l; i <= r; ++i)
#define ford(i, r, l) for(int i = r; i >= l; --i)
#define FOR(i, n) for(int i = 0; i < n; ++i)
#define fi first
#define se second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define endl "\n"
#define task "beads"
#define sz(a) int(a.size())
#define C(x, y) make_pair(x, y)
#define all(a) (a).begin(), (a).end()
#define bit(i, mask) (mask >> i & 1)
template<typename T> bool maximize(T &res, const T &val) { if (res < val){ res = val; return true; }; return false; }
template<typename T> bool minimize(T &res, const T &val) { if (res > val){ res = val; return true; }; return false; }
inline int readInt() {char c;while(c=getchar(),c!='-'&&(c<'0'||c>'9'));bool sign=(c=='-');if(sign)c=getchar();int n=c-'0';while(c=getchar(),c>='0'&&c<='9')n=10*n+c-'0';return(!sign)?n:-n;}
inline ll readLong() {char c;while(c=getchar(),c!='-'&&(c<'0'||c>'9'));bool sign=(c=='-');if(sign)c=getchar();ll n=c-'0';while(c=getchar(),c>='0'&&c<='9')n=10*n+c-'0';return(!sign)?n:-n;}
inline string readString() {char c;while(c=getchar(),c==' '||c=='\n'||c=='\t');string s({c});while(c=getchar(),c!=EOF&&c!=' '&&c!='\n'&&c!='\t')s+=c;return s;}
const int N = 1e4 + 4;
const int M = 3e5 + 3;
const int K = 22;
const int INF = 1e9 + 3;
const ll LINF = 1e18;
int n;
int dp[N][2];
vector<pii> g[N];
int dfs(int u, int p, bool c)
{
int &res = dp[u][c];
if(res != -1) return res;
res = -INF;
int sum = 0;
for(pii e : g[u])
{
int v = e.fi, w = e.se;
if(v == p) continue;
sum += max(dfs(v, u, 0), dfs(v, u, 1) + w);
}
int Max1 = -INF, Max2 = -INF, cnt = 0;
for(pii e : g[u])
{
int v = e.fi, w = e.se;
if(v == p) continue;
++cnt;
int diff = dfs(v, u, 0) + w - max(dfs(v, u, 0), dfs(v, u, 1) + w);
if(diff > Max1) Max2 = Max1, Max1 = diff;
else maximize(Max2, diff);
}
if(c == 0) maximize(res, sum);
if(cnt >= 1 && c == 1) maximize(res, sum + Max1);
return res;
}
void solve()
{
cin >> n;
rep(i, n - 1)
{
int u, v, w;
cin >> u >> v >> w;
g[u].pb({v, w});
g[v].pb({u, w});
}
int res = 0;
rep(i, n)
{
memset(dp, -1, sizeof dp);
maximize(res, dfs(i, i, 0));
}
cout << res << endl;
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int TC = 1;
if(fopen(task".inp", "r"))
{
freopen(task".inp", "r", stdin);
freopen(task"_slow.out", "w", stdout);
}
while(TC--)
{
solve();
cout << endl;
}
return 0;
}
Compilation message (stderr)
beads.cpp: In function 'int main()':
beads.cpp:103:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
103 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
beads.cpp:104:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
104 | freopen(task"_slow.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |