이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
const int nax = 200005;
vector<pair<int,int>>g[nax];
int dp[nax][2], n;
int solve(int v, int topar, int p = -1) {
int &ret = dp[v][(topar>0)];
if(ret!=-1) return ret;
ret = 0;
int base = 0;
for(auto [u, w] : g[v]) if(u!=p) {
base += solve(u, w, v);
}
ret = base;
if(topar>0) {
for(auto [u, w] : g[v]) if(u!=p) {
int cur = base - solve(u, w, v) + solve(u, 0, v) + topar + w;
ret = max(ret, cur);
}
}
for(int i=0; i<g[v].size(); ++i) {
for(int j=i+1; j<g[v].size(); ++j) {
auto [u1, w1] = g[v][i];
auto [u2, w2] = g[v][j];
if(u1==p || u2==p) continue;
int cur = base - solve(u1, w1, v) - solve(u2, w2, v) + solve(u1, 0, v)
+ solve(u2, 0, v) + w1 + w2;
ret = max(ret, cur);
}
}
return ret;
}
void PlayGround() {
cin>>n;
for(int i=1; i<n; ++i) {
int u, v, w;
cin>>u>>v>>w;
g[u].emplace_back(v, w);
g[v].emplace_back(u, w);
}
memset(dp, -1, sizeof dp);
cout<<solve(1, 0)<<'\n';
// cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
PlayGround();
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
beads.cpp: In function 'int solve(int, int, int)':
beads.cpp:29:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
29 | for(int i=0; i<g[v].size(); ++i) {
| ~^~~~~~~~~~~~
beads.cpp:30:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | for(int j=i+1; j<g[v].size(); ++j) {
| ~^~~~~~~~~~~~
# | 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... |