#include <bits/stdc++.h>
#include "dreaming.h"
using namespace std;
#define REP(i, n) for (int i = 0, _b = (n); i < _b; i++)
#define FOR(i, a, b) for (int i = a, _b = (b); i <= _b; i++)
#define pb push_back
#define szx(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define pii pair<int, int>
#define ll long long
const int MAXN = 100000;
vector<pii> adj[MAXN];
vector<ll> dis(MAXN, -1);
ll ans = 0;
ll mx = 0, d = 0;
int par[MAXN];
void dfs(int u, int p = -1) {
if (mx < dis[u]) mx = dis[u], d = u;
par[u] = p;
for (auto& [v, w] : adj[u]) {
if (v != p) {
dis[v] = dis[u] + w;
dfs(v, u);
}
}
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
REP(i, M) {
adj[A[i]].pb({B[i], T[i]});
adj[B[i]].pb({A[i], T[i]});
}
vector<ll> half;
REP(i, N) if (dis[i] == -1) {
mx = -1;
d = 0;
dis[i] = 0;
dfs(i);
int d1 = d;
mx = -1, d = 0;
dis[d1] = 0;
dfs(d1);
int d2 = d;
ans = max(ans, mx);
ll ans1 = LLONG_MAX;
for (int u = d2; u != -1; u = par[u])
ans1 = min(max(dis[u], mx - dis[u]), ans1);
half.pb(ans1);
}
sort(all(half), greater<ll>());
if (szx(half) >= 2) {
ans = max(ans, half[0] + L + half[1]);
}
for (int i = 2; i < szx(half); i++) {
ans = max(ans, half[0] + 1LL * i * L + half[i]);
}
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... |