#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],s[MAXN],b[MAXN],ans;
vector<pll> tree[MAXN];
void dfs(int x, int par, ll bac){
ll sum=0;
vector<ll> h;
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]});
sum+=tmp;
c++;
h.eb(-i.se-max(s[i.fi],b[i.fi])+tmp);
}
sort(h.begin(),h.end());
if(par&&c>=1) t[x]=sum - h[0] + bac;
if(c>=2) s[x]=sum - h[0] - h[1];
b[x]=sum;
ans = max({ans, 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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
4984 KB |
Output is correct |
2 |
Correct |
8 ms |
4984 KB |
Output is correct |
3 |
Correct |
8 ms |
5116 KB |
Output is correct |
4 |
Correct |
8 ms |
4984 KB |
Output is correct |
5 |
Correct |
8 ms |
4984 KB |
Output is correct |
6 |
Incorrect |
8 ms |
4988 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
4984 KB |
Output is correct |
2 |
Correct |
8 ms |
4984 KB |
Output is correct |
3 |
Correct |
8 ms |
5116 KB |
Output is correct |
4 |
Correct |
8 ms |
4984 KB |
Output is correct |
5 |
Correct |
8 ms |
4984 KB |
Output is correct |
6 |
Incorrect |
8 ms |
4988 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
4984 KB |
Output is correct |
2 |
Correct |
8 ms |
4984 KB |
Output is correct |
3 |
Correct |
8 ms |
5116 KB |
Output is correct |
4 |
Correct |
8 ms |
4984 KB |
Output is correct |
5 |
Correct |
8 ms |
4984 KB |
Output is correct |
6 |
Incorrect |
8 ms |
4988 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
4984 KB |
Output is correct |
2 |
Correct |
8 ms |
4984 KB |
Output is correct |
3 |
Correct |
8 ms |
5116 KB |
Output is correct |
4 |
Correct |
8 ms |
4984 KB |
Output is correct |
5 |
Correct |
8 ms |
4984 KB |
Output is correct |
6 |
Incorrect |
8 ms |
4988 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |