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 N = 100005;
const int INF = 1e9;
int f[N], g[N];
bool visit[N];
vector<int> vec;
vector< pair<int, int> > G[N];
void dfs1(int u, int p) {
visit[u] = 1, vec.push_back(u);
for (auto i : G[u]) {
int v = i.first, w = i.second;
if (v == p) continue;
dfs1(v, u);
f[u] = max(f[u], f[v] + w);
}
}
void dfs2(int u, int p) {
int v0 = -INF, v1 = -INF;
for (auto i : G[u]) {
int v = i.first, w = i.second;
if (v == p) continue;
int tmp = f[v] + w;
if (v0 < tmp) v1 = v0, v0 = tmp;
else if (v1 < tmp) v1 = tmp;
}
for (auto i : G[u]) {
int v = i.first, w = i.second;
if (v == p) continue;
g[v] = g[u] + w;
if (v0 == f[v] + w) g[v] = max(g[v], v1 + w);
else g[v] = max(g[v], v0 + w);
dfs2(v, u);
}
}
int travelTime(int n, int m, int L, int A[], int B[], int T[]) {
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]});
}
vector<int> vec2;
int res = -INF;
for (int i = 0; i < n; ++i) {
if (visit[i]) continue;
vec.clear(), dfs1(i, i), dfs2(i, i);
int mn = INF;
for (auto j : vec) {
mn = min(mn, max(f[j], g[j]));
res = max(res, max(f[j], g[j]));
}
vec2.push_back(mn);
}
sort(vec2.begin(), vec2.end(), greater<int>());
if (vec2.size() == 1) {
return max(res, vec2[0]);
}
if (vec2.size() == 2) {
return max(res, vec2[0] + vec2[1] + L);
}
return max(res, max(vec2[0] + vec2[1] + L, vec2[1] + vec2[2] + 2 * 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... |