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>
#define forinc(i,a,b) for(int i = a, _key = b; i <= _key; ++i)
#define fordec(i,a,b) for(int i = a, _key = b; i >= _key; --i)
#define fori(i,n) for(int i = 0, _key = n; i < _key; ++i)
#define ford(i,n) for(int i = n - 1; i >= 0; --i)
#define forvct(i,v) for(int i = 0, _key = v.size(); i < _key; ++i)
#define sqr(x) ((ll)x) * (x)
#define task "beads"
#define st first
#define nd second
#define m_p make_pair
#define m_t make_tuple
#define p_b push_back
#define p_f push_front
#define pp_b pop_back
#define pp_f pop_front
#define sn string::npos
#define heap priority_queue
#define ll long long
#define db double
#define str string
#define nn 200010
using namespace std;
const ll oo = 100000000000000007LL;
int n;
ll f[nn][4];
vector<pair<int,int> > adj[nn];
void enter()
{
cin >> n;
forinc(i,2,n)
{
int u, v, w;
cin >> u >> v >> w;
adj[u].p_b(m_p(v,w));
adj[v].p_b(m_p(u,w));
}
}
void visit(const int &u, const int &x)
{
f[u][0] = 0;
f[u][1] = 0;
f[u][2] = -oo;
f[u][3] = -oo;
forvct(j,adj[u])
{
int v = adj[u][j].st, w = adj[u][j].nd;
if (v == x) continue;
visit(v,u);
ll tmp = max(max(f[v][0],f[v][1]),f[v][2] + w),
t0 = f[u][0] + tmp,
t1 = max(max(max(f[u][1] + tmp,f[u][0] + f[v][3] + w),f[u][2] + max(f[v][0],f[v][1]) + w),f[u][3] + f[v][0] + w),
t2 = max(f[u][2] + tmp,f[u][0] + f[v][0] + w),
t3 = max(f[u][3] + tmp,f[u][0] + f[v][1] + w);
f[u][0] = t0;
f[u][1] = t1;
f[u][2] = t2;
f[u][3] = t3;
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//srand(time(NULL));
//freopen(task".inp","r",stdin);
//freopen(task".out","w",stdout);
enter();
visit(1,0);
cout << max(f[1][0],f[1][1]);
}
# | 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... |