#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define pb push_back
#define mp make_pair
#define f first
#define s second
#include "dreaming.h"
vector<vector<pi>> adj(1e5+1);
vector<bool> visited(1e5+1);
vi parent(1e5+1), dists(1e5+1);
int maxe = -1, maxnode = -1;
void dfs(int x, int p=-1, int dist=0){
visited[x] = 1;
parent[x] = p;
dists[x] = dist;
if(dist > maxe){
maxe = dist;
maxnode = x;
}
for(auto i : adj[x]){
if(i.f != p)
dfs(i.f, x, dist + i.s);
}
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]){
for(int i = 0; i < M; i++){
adj[A[i]].pb({B[i], T[i]});
adj[B[i]].pb({A[i], T[i]});
}
vector<pi> centers;
for(int i = 0; i < N; i++){
if(!visited[i]){
maxe = -1, maxnode = -1;
dfs(i);
int n1 = maxnode;
maxe = -1, maxnode = -1;
dfs(n1);
int n2 = maxnode;
vector<pi> path;
while(n2 != n1){
path.pb({n2, dists[n2]});
n2 = parent[n2];
}
path.pb({n2, dists[n2]});
reverse(path.begin(), path.end());
ll diameter = path.back().second;
ll best_diff = 1e18, best_node = -1, best_dist = -1;
for(int i = 0; i < path.size(); i++){
if(abs(diameter - 2 * path[i].s) < best_diff){
best_diff = abs(diameter - 2 * path[i].s);
best_node = path[i].f;
best_dist = max((ll)path[i].s, diameter - path[i].s);
}
}
centers.pb({best_dist, best_node});
}
}
sort(centers.begin(), centers.end(), greater<pi>());
for(int i = 1; i < centers.size(); i++){
adj[centers[0].s].pb({centers[i].s, L});
adj[centers[i].s].pb({centers[0].s, L});
}
dfs(0);
maxe = -1;
dfs(maxnode);
return dists[maxnode];
}
Compilation message
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:59:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for(int i = 0; i < path.size(); i++){
| ~~^~~~~~~~~~~~~
dreaming.cpp:70:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | for(int i = 1; i < centers.size(); i++){
| ~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
15432 KB |
Output is correct |
2 |
Correct |
49 ms |
15436 KB |
Output is correct |
3 |
Correct |
34 ms |
11336 KB |
Output is correct |
4 |
Correct |
9 ms |
5248 KB |
Output is correct |
5 |
Correct |
8 ms |
4436 KB |
Output is correct |
6 |
Correct |
13 ms |
6156 KB |
Output is correct |
7 |
Correct |
2 ms |
3412 KB |
Output is correct |
8 |
Correct |
30 ms |
7556 KB |
Output is correct |
9 |
Correct |
36 ms |
9496 KB |
Output is correct |
10 |
Correct |
2 ms |
3540 KB |
Output is correct |
11 |
Correct |
67 ms |
11180 KB |
Output is correct |
12 |
Correct |
75 ms |
13396 KB |
Output is correct |
13 |
Correct |
3 ms |
3540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3464 KB |
Output is correct |
2 |
Correct |
2 ms |
3412 KB |
Output is correct |
3 |
Correct |
2 ms |
3412 KB |
Output is correct |
4 |
Correct |
2 ms |
3472 KB |
Output is correct |
5 |
Correct |
2 ms |
3412 KB |
Output is correct |
6 |
Incorrect |
2 ms |
3412 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
15432 KB |
Output is correct |
2 |
Correct |
49 ms |
15436 KB |
Output is correct |
3 |
Correct |
34 ms |
11336 KB |
Output is correct |
4 |
Correct |
9 ms |
5248 KB |
Output is correct |
5 |
Correct |
8 ms |
4436 KB |
Output is correct |
6 |
Correct |
13 ms |
6156 KB |
Output is correct |
7 |
Correct |
2 ms |
3412 KB |
Output is correct |
8 |
Correct |
30 ms |
7556 KB |
Output is correct |
9 |
Correct |
36 ms |
9496 KB |
Output is correct |
10 |
Correct |
2 ms |
3540 KB |
Output is correct |
11 |
Correct |
67 ms |
11180 KB |
Output is correct |
12 |
Correct |
75 ms |
13396 KB |
Output is correct |
13 |
Correct |
3 ms |
3540 KB |
Output is correct |
14 |
Correct |
3 ms |
3464 KB |
Output is correct |
15 |
Correct |
2 ms |
3412 KB |
Output is correct |
16 |
Correct |
2 ms |
3412 KB |
Output is correct |
17 |
Correct |
2 ms |
3472 KB |
Output is correct |
18 |
Correct |
2 ms |
3412 KB |
Output is correct |
19 |
Incorrect |
2 ms |
3412 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
48 ms |
8104 KB |
Output is correct |
2 |
Correct |
47 ms |
8160 KB |
Output is correct |
3 |
Correct |
46 ms |
8120 KB |
Output is correct |
4 |
Correct |
44 ms |
8072 KB |
Output is correct |
5 |
Correct |
32 ms |
8124 KB |
Output is correct |
6 |
Correct |
49 ms |
8940 KB |
Output is correct |
7 |
Correct |
55 ms |
8340 KB |
Output is correct |
8 |
Correct |
40 ms |
8012 KB |
Output is correct |
9 |
Correct |
48 ms |
7912 KB |
Output is correct |
10 |
Correct |
39 ms |
8276 KB |
Output is correct |
11 |
Correct |
2 ms |
3412 KB |
Output is correct |
12 |
Correct |
17 ms |
8120 KB |
Output is correct |
13 |
Correct |
18 ms |
8136 KB |
Output is correct |
14 |
Correct |
18 ms |
8156 KB |
Output is correct |
15 |
Correct |
20 ms |
8136 KB |
Output is correct |
16 |
Correct |
17 ms |
8136 KB |
Output is correct |
17 |
Correct |
22 ms |
8132 KB |
Output is correct |
18 |
Correct |
22 ms |
8144 KB |
Output is correct |
19 |
Correct |
18 ms |
8108 KB |
Output is correct |
20 |
Correct |
2 ms |
3412 KB |
Output is correct |
21 |
Correct |
2 ms |
3412 KB |
Output is correct |
22 |
Correct |
3 ms |
3540 KB |
Output is correct |
23 |
Correct |
17 ms |
8064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3464 KB |
Output is correct |
2 |
Correct |
2 ms |
3412 KB |
Output is correct |
3 |
Correct |
2 ms |
3412 KB |
Output is correct |
4 |
Correct |
2 ms |
3472 KB |
Output is correct |
5 |
Correct |
2 ms |
3412 KB |
Output is correct |
6 |
Incorrect |
2 ms |
3412 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
15432 KB |
Output is correct |
2 |
Correct |
49 ms |
15436 KB |
Output is correct |
3 |
Correct |
34 ms |
11336 KB |
Output is correct |
4 |
Correct |
9 ms |
5248 KB |
Output is correct |
5 |
Correct |
8 ms |
4436 KB |
Output is correct |
6 |
Correct |
13 ms |
6156 KB |
Output is correct |
7 |
Correct |
2 ms |
3412 KB |
Output is correct |
8 |
Correct |
30 ms |
7556 KB |
Output is correct |
9 |
Correct |
36 ms |
9496 KB |
Output is correct |
10 |
Correct |
2 ms |
3540 KB |
Output is correct |
11 |
Correct |
67 ms |
11180 KB |
Output is correct |
12 |
Correct |
75 ms |
13396 KB |
Output is correct |
13 |
Correct |
3 ms |
3540 KB |
Output is correct |
14 |
Correct |
3 ms |
3464 KB |
Output is correct |
15 |
Correct |
2 ms |
3412 KB |
Output is correct |
16 |
Correct |
2 ms |
3412 KB |
Output is correct |
17 |
Correct |
2 ms |
3472 KB |
Output is correct |
18 |
Correct |
2 ms |
3412 KB |
Output is correct |
19 |
Incorrect |
2 ms |
3412 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |