This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include "dreaming.h"
typedef long long int64;
using namespace std;
const int maxn = 1e5 + 2;
int64 dp[maxn]; // dp[i] -> max dist to leaf in subtree i
vector <pair <int64, int64> > ad [maxn];
vector <int64> a;
bitset <maxn> vis;
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
for (int i = 0; i < M; i++){
ad[A[i]].push_back({B[i], T[i]});
ad[B[i]].push_back({A[i], T[i]});
}
int64 rs = 0;
for (int i = 0; i < N; i++){
if (!vis[i]) {
auto process = [&] () {
int64 diam = 0, mxdist = 1e18;
auto dfs = [&] (int u, auto &&dfs) -> void {
vis[u] = 1;
for (auto &[v, cost]: ad[u]) {
if (!vis[v]) {
dfs(v, dfs);
dp[u] = max(dp[u], dp[v] + cost);
}
}
}; dfs(i, dfs);
auto sum = [&] (int u, int p, auto &&sum) -> void {
vector <int64> sm;
for (auto &[v, cost]: ad[u]) {
if (v == p) continue;
sm.push_back(dp[v] + cost);
sum(v, u, sum);
}
int64 ret = 0;
sort(sm.begin(), sm.end(), greater <int64> ());
for (int i = 0; i < min(2, (int) sm.size()); i++) ret+=sm[i];
diam = max(diam, ret);
return;
}; sum(i, -1, sum);
auto dist = [&] (int u, int p, int64 pans, auto &&dist) -> void {
vector <int64> pref, suff;
for (auto &[v, cost]: ad[u]) {
if (v == p) continue;
pref.push_back(dp[v] + cost);
suff.push_back(dp[v] + cost);
}
for (int i = 1; i < pref.size(); i++) pref[i] = max(pref[i-1], pref[i]);
for (int i = suff.size() - 2; i >= 0; i--) suff[i] = max(suff[i + 1], suff[i]);
mxdist = min(mxdist, max(pans, suff.empty() ? 0: suff[0]));
// cout << u << " " << max(pans, suff.empty() ? 0: suff[0]) << "\n";
int idx = 0;
for (auto &[v, cost]: ad[u]) {
if (v == p) continue;
int64 p1 = (idx == 0 ? -1e9 : pref[idx-1]);
int64 p2 = (idx == suff.size() - 1 ? -1e9: suff[idx + 1]);
dist(v, u, max({p1, p2, pans}) + cost, dist);
idx++;
}
}; dist(i, -1, 0, dist);
rs = max(rs, diam);
a.push_back(mxdist);
// cout << i << " " << diam << " " << mxdist << "\n";
}; process();
}
}
sort(a.begin(), a.end(), greater <int64> ());
rs = a[0] + a[1] + L;
return rs;
}
Compilation message (stderr)
dreaming.cpp: In lambda function:
dreaming.cpp:53:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | for (int i = 1; i < pref.size(); i++) pref[i] = max(pref[i-1], pref[i]);
| ~~^~~~~~~~~~~~~
dreaming.cpp: In instantiation of 'travelTime(int, int, int, int*, int*, int*)::<lambda()>::<lambda(int, int, int64, auto:25&&)> [with auto:25 = travelTime(int, int, int, int*, int*, int*)::<lambda()>::<lambda(int, int, int64, auto:25&&)>&; int64 = long long int]':
dreaming.cpp:65:39: required from here
dreaming.cpp:53:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
dreaming.cpp:61:41: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | int64 p2 = (idx == suff.size() - 1 ? -1e9: suff[idx + 1]);
| ~~~~^~~~~~~~~~~~~~~~~~
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |