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 "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
vector< vector< pair<int, int> > > g;
vector<int> was, cur;
void dfs(int u, int p, vector<int>& dist) {
was[u] = 1;
cur.push_back(u);
for (auto& v : g[u]) {
if (v.first != p) {
dist[v.first] = dist[u] + v.second;
dfs(v.first, u, dist);
}
}
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
g.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]});
}
was.assign(N, 0);
vector<int> v;
int ans = 0;
vector<int> dist_a(N, 0), dist_b(N, 0);
for (int i = 0; i < N; i++) {
if (was[i]) {
continue;
}
cur.clear();
dist_a[i] = 0;
dfs(i, -1, dist_a);
int a = cur[0];
for (auto& x : cur) {
if (dist_a[x] > dist_a[a]) {
a = x;
}
}
cur.clear();
dist_a[a] = 0;
dfs(a, -1, dist_a);
int b = cur[0];
for (auto& x : cur) {
if (dist_a[x] > dist_a[b]) {
b = x;
}
}
ans = max(ans, dist_a[b]);
cur.clear();
dist_b[b] = 0;
dfs(b, -1, dist_b);
int tmp = INF;
for (auto& x : cur) {
tmp = min(tmp, max(dist_a[x], dist_b[x]));
}
v.push_back(tmp);
}
sort(v.rbegin(), v.rend());
if ((int) v.size() >= 2) {
ans = max(ans, v[0] + v[1] + L);
if ((int) v.size() >= 3) {
ans = max(ans, v[1] + v[2] + 2 * L);
}
}
return ans;
}
# | 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... |