#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector<pair<int, int>> adj[200005];
bool vis[200005];
ll a1[200005], a2[200005];
ll cmin = 0;
pair<ll, int> dfsdep(int x, int p = -1) {
pair<ll, int> ans(0, x);
for(auto &e:adj[x]) {
if(e.first == p) continue;
auto res = dfsdep(e.first, x);
res.first += e.second;
ans = max(ans, res);
}
return ans;
}
void dfsfill(int x, int p=-1, ll cd = 0) {
if(!(~a1[x])) a1[x] = cd;
else cmin = min(cmin, max(cd, a1[x]));
for(auto &e:adj[x]) {
if(e.first == p) continue;
dfsfill(e.first, x, cd+e.second);
}
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
for(int i=0;i<M;i++) {
adj[A[i]].emplace_back(B[i], T[i]);
adj[B[i]].emplace_back(A[i], T[i]);
}
memset(a1, -1, sizeof a1);
vector<long long> anses;
ll ans1 = 0;
for(int i=0;i<N;i++) {
if(vis[i]) continue;
int e1 = dfsdep(i).second;
int e2 = dfsdep(e1).second;
ans1 = max(ans1, dfsdep(e1).first);
cmin = LLONG_MAX;
dfsfill(e1); dfsfill(e2);
anses.emplace_back(cmin);
}
sort(anses.rbegin(), anses.rend());
//for(auto e:anses) cout << e << ' ';
return max(ans1, anses.size() > 0 ? max(anses[0] + anses[1] + L, anses.size() > 1 ? anses[1] + anses[2] + 2*L: 0ll): 1ll);
}
| # | 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... |