이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <utility>
#include <functional>
using i64 = long long;
std::vector<std::vector<std::pair<int,int>>> neigh;
struct Node
{
int w_up=0;
std::vector<Node> ch;
Node(int where, int prev)
{
for(auto[w,c] : neigh[where])
{
if(w==prev) w_up = c;
else ch.emplace_back(w,where);
}
}
// returns {cost if I manage the top edge (might not use it),
// cost if you have the top edge (including edge)}
// answer to problem is root.calculate_ms().second
std::pair<int,int> calculate_ms() const
{
std::vector<std::pair<int,int>> cw;
for(auto& i : ch) cw.push_back(i.calculate_ms());
if(cw.size()==0) return {0,w_up};
if(cw.size()==1)
{
auto[a,b] = cw[0];
return {std::max(a,b+w_up),w_up+a};
}
long long s = 0;
for(auto &[a,b] : cw) s+=a;
std::vector<int> diff;
for(int i = 0; i < cw.size(); i++) diff.push_back(cw[i].second-cw[i].first);
int m = 0;
for(int i = 1; i < diff.size(); i++) if (diff[i]>diff[m]) m = i;
int m2 = !m;
for(int i = 1; i < diff.size(); i++) if(i!=m && diff[i] > diff[m2]) m2 = i;
auto with_two = diff[m]+diff[m2]+s;
auto without = s;
auto with_one = diff[m]+s+w_up;
return {std::max(with_two,std::max(without,with_one)),w_up+std::max(with_two,without)};
}
};
int main()
{
int N;
std::cin >> N;
neigh.resize(N);
for(int i = 0; i < N-1; i++)
{
int a,b,c;
std::cin >> a >> b >> c;
a--;b--;
neigh[a].emplace_back(b,c);
neigh[b].emplace_back(a,c);
}
Node tree(0,-1);
std::cout << tree.calculate_ms().second << "\n";
}
컴파일 시 표준 에러 (stderr) 메시지
beads.cpp: In member function 'std::pair<int, int> Node::calculate_ms() const':
beads.cpp:38:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | for(int i = 0; i < cw.size(); i++) diff.push_back(cw[i].second-cw[i].first);
| ~~^~~~~~~~~~~
beads.cpp:40:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for(int i = 1; i < diff.size(); i++) if (diff[i]>diff[m]) m = i;
| ~~^~~~~~~~~~~~~
beads.cpp:42:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | for(int i = 1; i < diff.size(); i++) if(i!=m && diff[i] > diff[m2]) m2 = i;
| ~~^~~~~~~~~~~~~
# | 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... |