#include <bits/stdc++.h>
#include "dreaming.h"
using namespace std;
const int MAX_N = 1e5 + 5;
vector<vector<pair<int, int>>> g(MAX_N);
vector<vector<pair<int, int>>> tree(MAX_N);
vector<int> par(MAX_N);
bitset<MAX_N> vis;
pair<int, int> best_way = {-1, -1};
void find_diam(const vector<vector<pair<int, int>>>& graph, int node, int p, int w) {
par[node] = p;
vis[node] = 1;
best_way = max(best_way, {w, node});
for (auto [to, cost] : graph[node]) {
if (to != p) {
find_diam(graph, to, node, w + cost);
}
}
}
int big(const vector<vector<pair<int, int>>>& graph, int node, int p, int w) {
int mx = w;
for (auto [to, cost] : graph[node]) {
if (to != p) {
mx = max(mx, big(graph, to, node, w + cost));
}
}
return mx;
}
pair<int, int> find_center(const vector<vector<pair<int, int>>>& graph, int node) {
best_way = {-1, -1};
find_diam(graph, node, -1, 0);
int L = best_way.second;
best_way = {-1, -1};
find_diam(graph, L, -1, 0);
int R = best_way.second;
int longest_path = best_way.first;
vector<int> path;
while(R != L) {
path.emplace_back(R);
R = par[R];
}
path.emplace_back(R);
const int mid = (int) path.size() / 2;
if ( (int) path.size() & 1) {
return {path[mid], longest_path};
}
auto A = make_pair(big(graph, path[mid], -1, 0), path[mid]);
auto B = make_pair(big(graph, path[mid - 1], -1, 0), path[mid - 1]);
return { (A.first < B.first ? A.second : B.second), longest_path};
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
for (int i = 0; i < M; i++) {
g[A[i]].emplace_back(B[i], T[i]);
g[B[i]].emplace_back(A[i], T[i]);
tree[A[i]].emplace_back(B[i], T[i]);
tree[B[i]].emplace_back(A[i], T[i]);
}
int nxt = -1;
for (int i = 0; i < N; i++) {
if (!vis[i]) {
auto w = find_center(g, i);
if (nxt != -1) {
tree[nxt].emplace_back(w.first, L);
tree[w.first].emplace_back(nxt, L);
}
nxt = w.first;
}
}
return find_center(tree, 0).second;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
129 ms |
19976 KB |
Output is correct |
2 |
Correct |
101 ms |
19980 KB |
Output is correct |
3 |
Correct |
65 ms |
15112 KB |
Output is correct |
4 |
Correct |
15 ms |
7552 KB |
Output is correct |
5 |
Correct |
14 ms |
6656 KB |
Output is correct |
6 |
Correct |
27 ms |
8832 KB |
Output is correct |
7 |
Incorrect |
4 ms |
5504 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
129 ms |
19976 KB |
Output is correct |
2 |
Correct |
101 ms |
19980 KB |
Output is correct |
3 |
Correct |
65 ms |
15112 KB |
Output is correct |
4 |
Correct |
15 ms |
7552 KB |
Output is correct |
5 |
Correct |
14 ms |
6656 KB |
Output is correct |
6 |
Correct |
27 ms |
8832 KB |
Output is correct |
7 |
Incorrect |
4 ms |
5504 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
129 ms |
19976 KB |
Output is correct |
2 |
Correct |
101 ms |
19980 KB |
Output is correct |
3 |
Correct |
65 ms |
15112 KB |
Output is correct |
4 |
Correct |
15 ms |
7552 KB |
Output is correct |
5 |
Correct |
14 ms |
6656 KB |
Output is correct |
6 |
Correct |
27 ms |
8832 KB |
Output is correct |
7 |
Incorrect |
4 ms |
5504 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
84 ms |
15864 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
129 ms |
19976 KB |
Output is correct |
2 |
Correct |
101 ms |
19980 KB |
Output is correct |
3 |
Correct |
65 ms |
15112 KB |
Output is correct |
4 |
Correct |
15 ms |
7552 KB |
Output is correct |
5 |
Correct |
14 ms |
6656 KB |
Output is correct |
6 |
Correct |
27 ms |
8832 KB |
Output is correct |
7 |
Incorrect |
4 ms |
5504 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
129 ms |
19976 KB |
Output is correct |
2 |
Correct |
101 ms |
19980 KB |
Output is correct |
3 |
Correct |
65 ms |
15112 KB |
Output is correct |
4 |
Correct |
15 ms |
7552 KB |
Output is correct |
5 |
Correct |
14 ms |
6656 KB |
Output is correct |
6 |
Correct |
27 ms |
8832 KB |
Output is correct |
7 |
Incorrect |
4 ms |
5504 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |