이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// INCOMPLETE
#include<bits/stdc++.h>
#define int long long
#define task "beads"
using namespace std;
using ii=pair<int,int>;
const int mn=2e5+11,inf=0x3f3f3f3f3f3f3f3f;
const int mk=111;
int n;
vector<ii>adj[mn];
template<typename _t>
bool ckmax(_t& target,const _t& source){
return target<source ? target=source, 1 : 0;
}
int dp[mn][4];
void dfs(int u,int pre){
vector<int>mval(5,-inf);
for(const ii& k:adj[u]){
int v=k.first,w=k.second;
if(v==pre)continue;
dfs(v,u);
int eval=max(dp[v][0],dp[v][2]+w);
dp[u][0]+=eval;
ckmax(mval[4],max(dp[v][1],dp[v][3]+w)-eval);
ckmax(mval[1],max(mval[2]+max(dp[v][0],dp[v][1])+w,mval[3]+dp[v][0]+w)-eval);
ckmax(mval[2],dp[v][0]+w-eval);
ckmax(mval[3],dp[v][1]+w-eval);
}
dp[u][1]=dp[u][0]+max(mval[1],mval[4]);
dp[u][2]=dp[u][0]+mval[2];
dp[u][3]=dp[u][0]+mval[3];
}
int32_t main()
{
cin.tie(0)->sync_with_stdio(0);
cin>>n;
for(int i=1,u,v,w;i<n;i++){
cin>>u>>v>>w;
adj[u].emplace_back(v,w);
adj[v].emplace_back(u,w);
}
dfs(1,-1);
cout<<max(dp[1][0],dp[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... |