이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;
int main(){
int n;
cin >> n;
vector<vector<pair<int, int>>> E(n);
for (int i = 0; i < n - 1; i++){
int a, b, c;
cin >> a >> b >> c;
a--;
b--;
E[a].push_back(make_pair(c, b));
E[b].push_back(make_pair(c, a));
}
vector<int> p(n, -1);
vector<vector<pair<int, int>>> c(n);
queue<int> Q;
Q.push(0);
vector<int> bfs;
while (!Q.empty()){
int v = Q.front();
Q.pop();
bfs.push_back(v);
for (auto P : E[v]){
int w = P.second;
if (w != p[v]){
p[w] = v;
c[v].push_back(P);
Q.push(w);
}
}
}
reverse(bfs.begin(), bfs.end());
vector<int> dp1(n, 0);
vector<int> dp2(n, -INF);
for (int v : bfs){
int cnt = c[v].size();
vector<vector<int>> dp3(3, vector<int>(cnt + 1, -INF));
dp3[0][0] = 0;
for (int i = 0; i < cnt; i++){
int l = c[v][i].first;
int w = c[v][i].second;
for (int j = 0; j < 3; j++){
dp3[j][i + 1] = max(dp3[j][i + 1], dp3[j][i] + max(dp1[w], dp2[w] + l));
if (j < 2){
dp3[j + 1][i + 1] = max(dp3[j + 1][i + 1], dp3[j][i] + dp1[w] + l);
}
}
}
dp1[v] = max(dp3[0][cnt], dp3[2][cnt]);
dp2[v] = dp3[1][cnt];
}
cout << dp1[0] << endl;
}
# | 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... |