이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/*Author: KawakiMeido*/
#include <bits/stdc++.h>
#define pb push_back
#define endl "\n"
#define ll long long
#define int long long
#define all(x) (x).begin(),(x).end()
#define pii pair<int,int>
#define fi first
#define se second
using namespace std;
/*Constants*/
const int N = 2e5+10;
const int INF = 1e9+7;
const long long LLINF = 1e15+3;
/*TestCases*/
int test=1;
void solve();
void TestCases(bool v){
if (v) cin >> test;
while(test--) solve();
}
/*Global Variables*/
int n;
int dp[N][2];
vector<pii> adj[N];
void DP(int u, int p, int w){
int res = 0;
vector<pii> tmp;
tmp.push_back({-LLINF,0});
tmp.push_back({-LLINF,0});
for (auto in:adj[u]){
int v,val; tie(v,val) = in;
if (v==p) continue;
DP(v,u,val);
res+=dp[v][1];
tmp.push_back({dp[v][0] - dp[v][1] + val,v});
}
sort(all(tmp),greater<pii>());
dp[u][0] = dp[u][1] = max(res,res+tmp[0].fi+tmp[1].fi);
dp[u][1] = max(dp[u][1],res+tmp[0].fi+w);
}
/*Solution*/
void solve(){
cin >> n;
for (int i=1; i<n; i++){
int u,v,w; cin >> u >> v >> w;
adj[u].push_back({v,w});
adj[v].push_back({u,w});
}
DP(1,0,0);
cout << dp[1][0] << endl;
}
/*Driver Code*/
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
TestCases(0);
return 0;
}
# | 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... |