이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#include<ext/rope>
using namespace std;
using namespace __gnu_cxx;
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false);cin.tie(0)
#define fopen freopen("input.txt", "r", stdin)
#define eb emplace_back
#define em emplace
#define prec(a) cout<<fixed;cout.precision(a);
#define all(a) (a).begin(), (a).end()
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef tuple<int,int,int> tiii;
const ll INF = 2e16;
const int inf = 2e9;
template<class T>
void pr(T t) {cout << t << " ";}
template<class T, class ...Args>
void pr(T a, Args ...args) {cout << a << " ";pr(args...);}
template<class ...Args>
void prl(Args ...args) {pr(args...);cout << endl;}
int n;
const int MAXN=200010;
ll T[MAXN],t[MAXN],s[MAXN],b[MAXN],ans;
vector<pll> tree[MAXN];
//t = 자식1,부모1 s = 자식2 b = 중심이 아님
void dfs(int x, int par, ll bac){
ll sum=0;
vector<pll> hs,hb;
int c=0;
for(auto i:tree[x]) if(i.fi!=par){
dfs(i.fi,x,i.se);
ll tmp = max({t[i.fi],s[i.fi],b[i.fi]});
b[x] += max({t[i.fi],s[i.fi],b[i.fi],T[i.fi]});
sum+=tmp;
c++;
hs.eb(i.se+s[i.fi]-tmp,i.fi);
hb.eb(i.se+b[i.fi]-tmp,i.fi);
}
sort(hs.begin(),hs.end());reverse(hs.begin(), hs.end());
sort(hb.begin(),hb.end());reverse(hb.begin(), hb.end());
if(par&&c>=1){
T[x] = sum + hs[0].fi + bac;
t[x] = sum + hb[0].fi + bac;
}
if(c>=2){
if(hs[0].se!=hb[0].se) s[x] = max(s[x],sum + hs[0].fi + hb[0].fi);
if(hs[0].se!=hb[1].se) s[x] = max(s[x],sum + hs[0].fi + hb[1].fi);
if(hs[1].se!=hb[0].se) s[x] = max(s[x],sum + hs[1].fi + hb[0].fi);
if(hs[1].se!=hb[1].se) s[x] = max(s[x],sum + hs[1].fi + hb[1].fi);
s[x] = max(s[x], sum + hb[0].fi + hb[1].fi);
}
ans = max({ans, T[x], t[x], s[x], b[x]});
}
int main(){
fastio;
cin>>n;
for(int i=1;i<n;i++){
int a, b, c;cin>>a>>b>>c;
tree[a].eb(b,c);
tree[b].eb(a,c);
}
dfs(1,0,0);
cout<<ans;
}
# | 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... |