이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define taskname "A"
#define pb push_back
#define mp make_pair
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int,int> ii;
const int maxn = 2e5 + 5;
int n;
ll dp[maxn][2][2];
vector<ii> adj[maxn];
void dfs(int u , int par , int parcost){
ll sum = 0;
for(auto & c : adj[u]){
if(c.first == par)continue;
dfs(c.first , u , c.second);
sum += dp[c.first][0][0];
}
dp[u][0][0] = dp[u][0][1] = dp[u][1][0] = dp[u][1][1] = sum;
{
vector<ll> val;
ll tmp1 = 0;
for(auto & c : adj[u]){
if(c.first == par)continue;
tmp1 = max(tmp1 , dp[c.first][0][1] - dp[c.first][0][0]);
val.pb(dp[c.first][1][0] + c.second - dp[c.first][0][0]);
}
if(val.size() >= 2){
sort(val.begin(),val.end(),greater<ll>());
for(auto & c : adj[u]){
if(c.first == par)continue;
if(val[0] == dp[c.first][1][0] + c.second - dp[c.first][0][0]){
dp[u][1][1] = max(dp[u][1][1] , sum + val[1] + max(dp[c.first][1][0],dp[c.first][1][1]) + c.second - dp[c.first][0][0]);
}else{
dp[u][1][1] = max(dp[u][1][1] , sum + val[0] + max(dp[c.first][1][0],dp[c.first][1][1]) + c.second - dp[c.first][0][0]);
}
}
}
dp[u][1][1] = max(dp[u][1][1] , sum + tmp1);
}
{
vector<ll> val;
for(auto & c : adj[u]){
if(c.first == par)continue;
val.pb(dp[c.first][1][0] + c.second - dp[c.first][0][0]);
}
sort(val.begin(),val.end(),greater<ll>());
if(val.size() >= 1)dp[u][0][0] = max(dp[u][0][0] , dp[u][0][0] + parcost + val[0]);
else dp[u][0][0] = -1e9;
dp[u][0][0] = max(dp[u][0][0] , dp[u][1][0]);
}
{
vector<ll> val;
for(auto & c : adj[u]){
if(c.first == par)continue;
val.pb(max(dp[c.first][1][1],dp[c.first][1][0]) + c.second - dp[c.first][0][0]);
}
sort(val.begin(),val.end(),greater<ll>());
if(val.size() >= 1)dp[u][0][1] = max(dp[u][0][1] , dp[u][0][1] + parcost + val[0]);
else dp[u][0][1] = -1e9;
dp[u][0][1] = max(dp[u][0][1] , dp[u][1][1]);
dp[u][0][1] = max(dp[u][0][1] , dp[u][0][0]);
}
// cout << u << " " << dp[u][0][0] << " " << dp[u][0][1] << " " << dp[u][1][0] << " " << dp[u][1][1] << endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
if(fopen(taskname".inp", "r")) {
freopen(taskname".inp", "r", stdin);
freopen(taskname".out", "w", stdout);
}
cin >> n;
for(int i = 1 ; i < n ; ++i){
int u , v , c;cin >> u >> v >> c;
adj[u].pb(mp(v,c));adj[v].pb(mp(u , c));
}
dfs(1 , 0 , 0);
cout << max(dp[1][1][0] , dp[1][1][1]);
}
컴파일 시 표준 에러 (stderr) 메시지
beads.cpp: In function 'int main()':
beads.cpp:80:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen(taskname".inp", "r", stdin);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
beads.cpp:81:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen(taskname".out", "w", stdout);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |