#include <bits/stdc++.h>
using namespace std;
#ifndef LOCAL
#include "dreaming.h"
#endif
const int N = 100'000 + 10;
vector<pair<int, int>> ad[N];
int f[N], trace[N];
bool mk[N];
vector<int> node;
void dfs(int u, int p = -1) {
mk[u] = true;
node.push_back(u);
trace[u] = p;
for (const auto& [v, w] : ad[u]) {
if (v == p) continue;
f[v] = f[u] + w;
dfs(v, u);
}
}
pair<int, int> findMid(int u) {
int v = u;
for (int t = 0; t < 2; ++t) {
for (const auto& x : node) f[x] = 0;
node.clear();
dfs(u);
for (const auto& x : node) if (f[v] < f[x]) v = x;
u = v;
}
int ret = v;
while (v != -1) {
if (max(f[v], f[u] - f[v]) < max(f[ret], f[u] - f[ret])) ret = v;
v = trace[v];
}
return {max(f[ret], f[u] - f[ret]), ret};
}
void tree(int u, int p = -1) {
for (const auto& [v, w] : ad[u]) {
if (v == p) continue;
cout << u << " " << v << " " << w << "\n";
tree(v, u);
}
}
int travelTime(int n, int m, int l, int A[],int B[],int T[]) {
for (int i = 1; i <= m; ++i) {
int u = A[i], v = B[i], w = T[i];
ad[u].push_back({v, w});
ad[v].push_back({u, w});
}
vector<pair<int, int>> mid;
for (int i = 0; i < n; ++i) {
if (mk[i]) continue;
mid.push_back(findMid(i));
}
sort(mid.begin(), mid.end(), greater<>());
memset(trace, -1, sizeof trace);
for (const auto& x : mid) {
if (x == mid[0]) continue;
int u = mid[0].second, v = x.second;
ad[u].push_back({v, l});
ad[v].push_back({u, l});
}
// tree(0);
memset(f, 0, sizeof f);
memset(trace, -1, sizeof trace);
findMid(0);
cerr << 1.0 * clock() / CLOCKS_PER_SEC << "\n";
return *max_element(f, f + n);
}
#ifdef LOCAL
int n, m, l;
int A[N], B[N], T[N];
mt19937_64 rng(chrono::steady_clock().now().time_since_epoch().count());
int rnd(int l, int r) { return l + rng() % (r - l + 1); }
int id[N];
int root(int u) { return id[u] < 0 ? u : id[u] = root(id[u]); }
void add(int u, int v) {
u = root(u); v = root(v);
if (u == v) return;
if (id[u] > id[v]) swap(u, v);
id[u] += id[v];
id[v] = u;
}
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
// memset(id, -1, sizeof id);
// n = rnd(1e5, 1e5), m = rnd(0, n - 1), l = rnd(1, 1e4);
// for (int i = 1; i <= m; ++i) {
// int u = rnd(0, n - 1), v = rnd(u + 1, u + 1);
// while (root(u) == root(v)) u = rnd(0, n - 1), v = rnd(u + 1, u + 1);
// add(u, v);
// A[i] = u; B[i] = v; T[i] = rnd(1, 1e4);
// }
cin >> n >> m >> l;
for (int i = 1; i <= m; ++i) cin >> A[i] >> B[i] >> T[i];
cout << travelTime(n, m, l, A, B, T) << "\n";
}
#endif
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1029 ms |
15052 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1048 ms |
4440 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1029 ms |
15052 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1034 ms |
6492 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1048 ms |
4440 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1029 ms |
15052 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |