#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5, INF = INT_MAX;
pair<int, int> root, leaf[2];
vector<pair<int, int>> adj[MAXN];
int ddist[MAXN][2];
bool vis[MAXN];
int comps[MAXN];
void dfs(int node, int p, int dist, int idx){
vis[node] = 1;
leaf[idx] = max(leaf[idx], {dist, node});
ddist[node][idx] = dist;
root = min(root, {max(ddist[node][0], ddist[node][1]), node});
for(auto& [v, wt] : adj[node]){
if(v == p)
continue;
dfs(v, node, dist + wt, idx);
}
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
for(int i = 0; i < M; i++){
adj[A[i]].emplace_back(make_pair(B[i], T[i]));
adj[B[i]].emplace_back(make_pair(A[i], T[i]));
}
int c = 0;
int max_d = 0;
for(int i = 0; i < N; i++){
if(vis[i] == 1)
continue;
leaf[0] = leaf[1] = {INT_MIN, INT_MIN};
dfs(i, i, 0, 0);
dfs(leaf[0].second, -1, 0, 1);
root = {INF, i};
dfs(leaf[1].second, -1, 0, 0);
max_d = max(max_d, leaf[0].first);
comps[c++] = root.first;
}
sort(comps, comps + c);
if(c == 1)
return comps[0];
if(c == 2)
return comps[0] + comps[1] + L;
return max({max_d, comps[c-1] + comps[c-2] + L, comps[c-2] + comps[c-3] + 2*L});
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
65 ms |
13164 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
65 ms |
13164 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
65 ms |
13164 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
6124 KB |
Output is correct |
2 |
Correct |
26 ms |
6124 KB |
Output is correct |
3 |
Correct |
28 ms |
6124 KB |
Output is correct |
4 |
Correct |
28 ms |
6124 KB |
Output is correct |
5 |
Correct |
28 ms |
6124 KB |
Output is correct |
6 |
Correct |
49 ms |
6252 KB |
Output is correct |
7 |
Correct |
29 ms |
6244 KB |
Output is correct |
8 |
Correct |
26 ms |
5996 KB |
Output is correct |
9 |
Correct |
26 ms |
6016 KB |
Output is correct |
10 |
Correct |
28 ms |
6252 KB |
Output is correct |
11 |
Correct |
2 ms |
2668 KB |
Output is correct |
12 |
Correct |
9 ms |
3948 KB |
Output is correct |
13 |
Correct |
9 ms |
3948 KB |
Output is correct |
14 |
Correct |
7 ms |
3948 KB |
Output is correct |
15 |
Correct |
7 ms |
3948 KB |
Output is correct |
16 |
Correct |
7 ms |
3948 KB |
Output is correct |
17 |
Correct |
7 ms |
3948 KB |
Output is correct |
18 |
Correct |
8 ms |
4060 KB |
Output is correct |
19 |
Correct |
7 ms |
3948 KB |
Output is correct |
20 |
Correct |
2 ms |
2668 KB |
Output is correct |
21 |
Correct |
2 ms |
2668 KB |
Output is correct |
22 |
Correct |
2 ms |
2796 KB |
Output is correct |
23 |
Correct |
7 ms |
3948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
65 ms |
13164 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
65 ms |
13164 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |