이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef complex<ld> cp;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vii;
typedef vector<cp> vcp;
typedef vector<ld> vld;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<vii> vvii;
#define fastIO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define forw(i,l,r) for( int i = (l) ; i < (r) ; i++ )
#define forb(i,r,l) for( int i = (r) ; i >= (l) ; i-- )
#define log2i(x) (64 - __builtin_clzll(1ll*(x)) - 1)
#define numBit(x) (__builtin_popcountll(1ll*(x)))
#define getBit(x,i) (x>>i&1)
#define Pi acos(-1.0l)
#define sz(x) (int)x.size()
#define mt make_tuple
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define debug(x) cerr << #x << " = " << x << '\n';
const int N = 2e5+7;
const int inf = 2e9+7;
int n,ans=0;
int dp[N][2];
vii edge[N];
void solve(int u, int par=-1) {
int tot=0;
for(auto v:edge[u]) if(v.fi!=par) {
solve(v.fi,u);
tot+=max(dp[v.fi][0],dp[v.fi][1]+v.se);
}
dp[u][0]=tot; dp[u][1]=-inf;
for(auto v:edge[u]) if(v.fi!=par) {
int sum=v.se+dp[v.fi][0]-max(dp[v.fi][0],dp[v.fi][1]+v.se);
dp[u][1]=max(dp[u][1],sum+tot);
}
}
void reroot(int u, int par=-1) {
ans=max(ans,dp[u][0]);
pii tmp={-inf,-inf};
for(auto v:edge[u]) {
int sum=v.se+dp[v.fi][0]-max(dp[v.fi][0],dp[v.fi][1]+v.se);
if(tmp.fi<sum) tmp.se=tmp.fi, tmp.fi=sum;
else if(tmp.fi==sum) tmp.se=sum;
else tmp.se=max(tmp.se,sum);
}
for(auto v:edge[u]) if(v.fi!=par) {
bool ma=(v.se+dp[v.fi][0]-max(dp[v.fi][0],dp[v.fi][1]+v.se)==tmp.fi);
int tmp_dp=dp[v.fi][1];
dp[u][1]-=max(dp[v.fi][0],dp[v.fi][1]+v.se);
if(ma) dp[u][1]+=tmp.se-tmp.fi;
dp[u][0]-=max(dp[v.fi][0],dp[v.fi][1]+v.se);
dp[v.fi][1]+=max(dp[u][0],dp[u][1]+v.se);
dp[v.fi][1]=max(dp[v.fi][1],dp[v.fi][0]+v.se+dp[u][0]);
dp[v.fi][0]+=max(dp[u][0],dp[u][1]+v.se);
reroot(v.fi,u);
dp[v.fi][0]-=max(dp[u][0],dp[u][1]+v.se);
dp[v.fi][1]=tmp_dp;
dp[u][0]+=max(dp[v.fi][0],dp[v.fi][1]+v.se);
dp[u][1]+=max(dp[v.fi][0],dp[v.fi][1]+v.se);
if(ma) dp[u][1]-=tmp.se-tmp.fi;
}
}
int main() {
fastIO;
#ifndef ONLINE_JUDGE
freopen("test.inp","r",stdin);
freopen("test.out","w",stdout);
#endif // ONLINE_JUDGE
cin >> n;
forw(i,1,n) {
int u,v,w; cin >> u >> v >> w;
u--, v--;
edge[u].pb(mp(v,w));
edge[v].pb(mp(u,w));
}
solve(0);
reroot(0);
cout << ans;
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
beads.cpp: In function 'int main()':
beads.cpp:88:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
88 | freopen("test.inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
beads.cpp:89:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
89 | freopen("test.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... |