#include <bits/stdc++.h>
#include "dreaming.h"
using namespace std;
#define ll long long
#define fastIO cin.tie(0); ios::sync_with_stdio(false)
unordered_map<int, vector<pair<int, int>>> adjList;
bool vis[100005];
int d1[100005];
int d2[100005];
int farthest[100005];
void tryPush(set<int> &a, vector<int> &b, int x) {
if(a.find(x) == a.end()) {
a.emplace(x);
b.push_back(x);
}
}
void dp1(vector<int> &comp, int v, int p = -1) {
comp.push_back(v);
d1[v] = 0;
int mxp1 = -1, mxp2 = -1;
for(auto i : adjList[v]) if(i.first != p) {
dp1(comp, i.first, v);
int pl = d1[i.first] + i.second;
d1[v] = max(d1[v], pl);
if(mxp1 < pl) {
mxp2 = mxp1;
mxp1 = pl;
} else if (mxp2 < pl) mxp2 = pl;
}
if(mxp2 != -1) {
d2[v] = mxp1+mxp2;
}
}
void dfs(set<int> &l, vector<int> &l2, int v, int p = -1) {
bool leaf = true;
for(auto i : adjList[v]) if(i.first != p) {
leaf = false;
dfs(l, l2, i.first, v);
}
if(leaf) tryPush(l, l2, v);
}
pair<int, int> solve(int r) {
vector<int> connComp;
set<int> leaves2;
vector<int> leaves;
dp1(connComp, r);
dfs(leaves2, leaves, r);
if(leaves.size() == 0) {
tryPush(leaves2, leaves, connComp[0]); // size 0
} else dfs(leaves2, leaves, leaves[0]);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
for(auto i : leaves) {pq.emplace(make_pair(0, i)); farthest[i] = 0;}
int rad = 0;
while(!pq.empty()) {
pair<int, int> top = pq.top(); pq.pop();
if(top.first < farthest[top.second]) continue; // if there was a faster route, skip this shorter one
rad = max(rad, top.first);
bool traversed = false;
for(auto i : adjList[top.second]) {
if(!vis[i.first]) {
pq.emplace(make_pair(i.second + top.first, i.first));
farthest[i.first] = max(farthest[i.first], i.second + top.first);
}
}
vis[top.second] = true;
}
int d = 0;
for(auto i : connComp) d = max(d, max(d1[i], d2[i]));
return make_pair(d, rad);
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
for(int i = 0; i < N; i++) {
vis[i] = false;
d1[i] = -1;
d2[i] = -1;
}
for(int i = 0; i < M; i++) {
adjList[A[i]].push_back({B[i], T[i]});
adjList[B[i]].push_back({A[i], T[i]});
}
vector<int> radii;
vector<int> diams;
int comps = 0;
for(int i = 0; i < N; i++) {
if(!vis[i]) {
pair<int, int> ans = solve(i);
diams.push_back(ans.first);
radii.push_back(ans.second);
comps++;
}
}
sort(radii.begin(), radii.end(), greater<int>());
sort(diams.begin(), diams.end(), greater<int>());
if(comps == 1) {
return diams[0];
} else if (comps == 2) {
return max(diams[0], radii[0] + radii[1] + L);
} else {
return max({diams[0], radii[0] + radii[1] + L, radii[1] + radii[2] + L + L});
}
}
# | 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... |