이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define in insert
#define all(x) x.begin(),x.end()
#define pb push_back
#define eb emplace_back
#define ff first
#define ss second
#define int long long
typedef long long ll;
typedef vector<int> vi;
typedef set<int> si;
typedef multiset<int> msi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
template<typename T, typename U>
ostream & operator << (ostream &out, const pair<T, U> &c) {
out << c.first << ' ' << c.second;
return out;
}
template<typename T>
ostream & operator << (ostream &out, vector<T> &v) {
const int sz = v.size();
for (int i = 0; i < sz; i++) {
if (i) out << ' ';
out << v[i];
}
return out;
}
template<typename T>
istream & operator >> (istream &in, vector<T> &v) {
for (T &x : v) in >> x;
return in;
}
template<typename T>
void mxx(T &a, T b){if(b > a) a = b;}
template<typename T>
void mnn(T &a, T b){if(b < a) a = b;}
const int mxn = 2e5 + 10, inf = 1e18;
int n, f[mxn], g[mxn];
vector<pair<int, int>> adj[mxn];
void dfs1(int to, int fr) {
f[to] = 0;
g[to] = -inf;
int mx1 = -inf;
for(auto it : adj[to]) {
if(it.ff == fr) continue;
int x = it.ff;
dfs1(x, to);
f[to] += max(f[x], g[x] + it.ss);
mxx(mx1, f[x] + it.ss - max(f[x], g[x] + it.ss));
}
mxx(g[to], f[to] + mx1);
}
int ans;
void dfs2(int to, int fr, int mx1, int mx2, int d) {
vector<int> v;
int tmp = 0;
if(to != 1) {
tmp += max(mx1, mx2 + d);
v.pb(mx1 + d - max(mx1, mx2 + d));
}
for(auto it : adj[to]) {
if(it.ff == fr) continue;
int x = it.ff;
tmp += max(f[x], g[x] + it.ss);
v.pb(f[x] + it.ss - max(f[x], g[x] + it.ss));
}
mxx(ans, tmp);
sort(all(v));
reverse(all(v));
if(v.size() > 1) {
mxx(ans, tmp + v[0] + v[1]);
}
int nx1 = 0;
multiset<int> s;
if(to != 1) {
nx1 += mx2 + d;
s.in(mx1 + d - mx2 - d);
}
for(auto it : adj[to]) {
if(it.ff == fr) continue;
int x = it.ff;
nx1 += max(f[x], g[x] + it.ss);
s.in(f[x] + it.ss - max(f[x], g[x] + it.ss));
}
for(auto it : adj[to]) {
if(it.ff == fr) continue;
int x = it.ff;
s.erase(s.find(f[x] + it.ss - max(f[x], g[x] + it.ss)));
nx1 -= max(f[x], g[x] + it.ss);
dfs2(x, to, nx1, nx1 + (*s.rbegin()), it.ss);
s.in(f[x] + it.ss - max(f[x], g[x] + it.ss));
nx1 += max(f[x], g[x] + it.ss);
}
}
void go() {
cin >> n;
for(int i = 1; i < n; i++) {
int x, y, c;
cin >> x >> y >> c;
adj[x].eb(y, c);
adj[y].eb(x, c);
}
dfs1(1, 0);
dfs2(1, 0, 0, 0, 0);
cout << ans << '\n';
}
signed main() {
//#ifndef ONO
//freopen("beads.in", "r", stdin);
//freopen("beads.out", "w", stdout);
//#endif
fast;
int t = 1;
// cin >> t;
while(t--) {
go();
}
return 0;
}
# | 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... |