이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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(f[v][0],f[v][2] + w),
t0 = f[u][0] + tmp,
t1 = max(max(max(f[u][1] + tmp,f[u][0] + max(f[v][1],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();
int root = 1;
visit(root,0);
cout << max(f[root][0],f[root][1]) << "\n";
}
# | 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... |