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"
using namespace std;
vector<vector<array<int, 2>>> g;
vector<long long> cost;
vector<bool> vis;
vector<array<int, 2>> par;
array<int, 2> dfs(int v, int p, int d) {
vis[v] = true;
par[v] = {p, d};
array<int, 2> ret = {d, v};
for(auto tx : g[v]) {
int u = tx[0], w = tx[1];
if(u == p) continue;
ret = max(ret, dfs(u, v, d + w));
}
return ret;
}
int travelTime(int N,int M,int L, int A[],int B[],int T[]) {
g.resize(N);
vis.resize(N);
par.resize(N);
for(int i = 0; i < M; i++) {
g[A[i]].push_back({B[i], T[i]});
g[B[i]].push_back({A[i], T[i]});
}
long long answer = 0;
for(int i = 0; i < N; i++) {
if(vis[i]) continue;
array<int, 2> x = dfs(i, -1, 0);
array<int, 2> d = dfs(x[1], -1, 0);
vector<array<int, 2>> diam;
array<int, 2> v = {d[1], d[0]};
//cout << d[0] << ' ' << d[1] << ' ' << x[1] << '\n';
while(v[0] != -1) {
diam.push_back({v[0], par[v[0]][1]});
v = par[v[0]];
}
//for(auto x : diam) cout << x[0] << ' ' << x[1] << '\n'; cout << '\n';
array<int, 3> ans = {d[0], d[0], 0};
for(auto x : diam) ans = min(ans, {abs(d[0] - 2 * x[1]), d[0] - x[1], x[1]});
//cout << ans[1] << ' ' << ans[2] << '\n';
answer = max(answer, (long long) d[0]);
cost.push_back(ans[1]); cost.push_back(ans[2]);
}
sort(cost.begin(), cost.end(), greater<int>());
if(cost.size() >= 2) answer = max(answer, cost[0] + L + cost[1]);
return answer;
}
# | 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... |