#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
const int MAXN = 1e5 + 10;
vector<pii> adj[MAXN];
int n, distante;
int dist[MAXN];
pii pai[MAXN];
vector<vector<pii>> chains;
bool mrk_1[MAXN], mrk_2[MAXN];
vector<int> st;
void dfs(int v) {
// cout << "dfs " << v << endl;
mrk_2[v] = true;
mrk_1[v] = true;
if (dist[distante] < dist[v]) {
distante = v;
}
for (auto [w, nc]: adj[v]) {
if (pai[v].first == w) continue;
pai[w] = {v, nc};
dist[w] = dist[v] + nc;
dfs(w);
}
}
int compute_radius() {
int v = distante;
vector<int> all_c;
int tot = 0;
while (v != -1) {
// cout << "processed node " << v << endl;
tot += pai[v].second;
all_c.push_back(pai[v].second);
v = pai[v].first;
}
int l = 0, r = tot;
int resp = tot;
for (int x: all_c) {
// cout << "looking at x " << x << endl;
l += x;
r -= x;
// cout << "l = " << l << " " << "r = " << r << endl;
resp = min(resp, max(l, r));
}
// cout << "resp is " << resp << endl;
return resp;
}
int find_radius(int v) {
// cout << "find_radius " << v << endl;
for (int i = 0; i < n; i++) {
mrk_1[i] = false;
dist[i] = 0;
pai[i] = {-1, 0};
}
distante = v;
dfs(v);
// cout << "distante do v == " << distante << endl;
for (int i = 0; i < n; i++) {
mrk_1[i] = false;
dist[i] = 0;
pai[i] = {-1, 0};
}
dfs(distante);
// cout << "distante do distante == " << distante << endl;
return compute_radius();
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
// subtask 3
assert(M == N - 2);
n = N;
for (int i = 0; i < M; i++) {
adj[A[i]].push_back({B[i], T[i]});
adj[B[i]].push_back({A[i], T[i]});
}
int r1 = find_radius(0);
int root = 0;
while (mrk_2[root]) root++;
int r2 = find_radius(root);
return r1 + r2 + 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... |