이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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;
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]));
vec2.push_back(mn);
}
sort(vec2.begin(), vec2.end(), greater<int>());
if (vec2.size() == 1) {
return vec2[0];
}
if (vec2.size() == 2) {
return vec2[0] + vec2[1] + L;
}
return 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... |