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 <pair <int64, 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;
int64 diam = 0, mxdist = 1e15;
int wh = 0;
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);
}
}
};
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;
};
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]);
if (max(pans, suff.empty() ? 0: suff[0]) < mxdist) {
mxdist = max(pans, suff.empty() ? 0: suff[0]);
wh = u;
}
int idx = 0;
for (auto &[v, cost]: ad[u]) {
if (v == p) continue;
int64 p1 = (idx == 0 ? -1e15 : pref[idx-1]);
int64 p2 = (idx == suff.size() - 1 ? -1e15: suff[idx + 1]);
dist(v, u, max({p1, p2, pans}) + cost, dist);
idx++;
}
};
for (int i = 0; i < N; i++){
if (!vis[i]) {
auto process = [&] () {
diam = 0, mxdist = 1e15;
wh = 0;
dfs(i, dfs);
sum(i, -1, sum);
dist(i, -1, 0, dist);
rs = max(rs, diam);
a.push_back({mxdist, wh});
}; process();
}
}
sort(a.begin(), a.end(), greater <pair <int64, int64>> ());
for (int i = 1; i < a.size(); i++){
ad[a[0].second].push_back({a[i].second, L});
ad[a[i].second].push_back({a[0].second, L});
}
diam = 0;
for (int i = 0; i < N; i++){
dp[i] = 0;
}
vis = 0;
dfs(1, dfs);
sum(1, -1, sum);
return diam;
}
Compilation message (stderr)
dreaming.cpp: In lambda function:
dreaming.cpp:48:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | for (int i = 1; i < pref.size(); i++) pref[i] = max(pref[i-1], pref[i]);
| ~~^~~~~~~~~~~~~
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:77:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
77 | for (int i = 1; i < a.size(); i++){
| ~~^~~~~~~~~~
dreaming.cpp: In instantiation of 'travelTime(int, int, int, int*, int*, int*)::<lambda(int, int, int64, auto:25&&)> [with auto:25 = travelTime(int, int, int, int*, int*, int*)::<lambda(int, int, int64, auto:25&&)>&; int64 = long long int]':
dreaming.cpp:70:36: required from here
dreaming.cpp:48:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | for (int i = 1; i < pref.size(); i++) pref[i] = max(pref[i-1], pref[i]);
| ~~^~~~~~~~~~~~~
dreaming.cpp:58:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | int64 p2 = (idx == suff.size() - 1 ? -1e15: 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... |