#include <bits/stdc++.h>
using namespace std;
#include "dreaming.h"
#define int long long
int ds[2][(int)1e5];
unordered_map<int,int> adj[(int)1e5];
int vis[(int)1e5];
int ind = 0;
unordered_set<int> sets[(int)1e5];
int cp[(int)1e5];
void dfs(int i , int p, int len, int x){
sets[ind].insert(i);
vis[i] = 1;
ds[x][i] = len;
for (auto const &pair: adj[i]){
int c = pair.first; int w= pair.second;
if (c==p) continue;
dfs(c,i, len+w,x);
}
}
signed travelTime(signed N, signed M, signed L, signed A[], signed B[], signed T[]){
for (int i=0; i<M; i++){
int a = A[i]; int b = B[i];
adj[a][b] = T[i]; adj[b][a] = T[i];
}
int r;
for (int i=0; i<N; i++) vis[i] = 0;
for (int i=0; i<N; i++){
if (vis[i]) continue;
dfs(i,i,0,0);
int a1 = i;
for (auto c: sets[ind]){
if (ds[0][c]>ds[0][a1]) a1 = c;
}
dfs(a1,a1,0,0);
int a2 = a1;
r = a1;
for (auto c: sets[ind]){
if (ds[0][c]>ds[0][a2]) a2 = c;
}
dfs(a2,a2,0,1);
int ans = 1e7;
for (auto c: sets[ind]){
ans = min(ans, max(ds[0][c], ds[1][c]));
}
cp[ind] = ans;
ind++;
}
if (ind == 1){
return ds[1][r];
}
sort(cp, cp+ind);
if (ind ==2 ) return L + cp[ind-1] + cp[ind-2];
return max(L + cp[ind-1] + cp[ind-2], 2*L + cp[ind-2] + cp[ind-3]);
}
// int main(){
// int n = 12; int m = 8 ;
// int l = 2;
// int a[] = {0, 8, 2, 5, 5, 1, 1, 10};
// int b[] = {8, 2, 7, 11, 1, 3, 9, 6};
// int t[] = {4, 2, 4, 3, 7, 1, 5, 3};
// cout<<travelTime(n,m,l,a,b,t);
// }
Compilation message
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:53:17: warning: 'r' may be used uninitialized in this function [-Wmaybe-uninitialized]
53 | return ds[1][r];
| ~~~~~~~^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
116 ms |
44296 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
11220 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
116 ms |
44296 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
59 ms |
32876 KB |
Output is correct |
2 |
Correct |
74 ms |
33048 KB |
Output is correct |
3 |
Correct |
64 ms |
32840 KB |
Output is correct |
4 |
Correct |
61 ms |
32948 KB |
Output is correct |
5 |
Correct |
71 ms |
32816 KB |
Output is correct |
6 |
Correct |
65 ms |
34076 KB |
Output is correct |
7 |
Correct |
67 ms |
33928 KB |
Output is correct |
8 |
Correct |
66 ms |
32412 KB |
Output is correct |
9 |
Correct |
60 ms |
32256 KB |
Output is correct |
10 |
Correct |
71 ms |
33628 KB |
Output is correct |
11 |
Correct |
5 ms |
11220 KB |
Output is correct |
12 |
Correct |
35 ms |
28508 KB |
Output is correct |
13 |
Correct |
35 ms |
28560 KB |
Output is correct |
14 |
Correct |
34 ms |
28476 KB |
Output is correct |
15 |
Correct |
40 ms |
28504 KB |
Output is correct |
16 |
Correct |
35 ms |
28408 KB |
Output is correct |
17 |
Correct |
35 ms |
28448 KB |
Output is correct |
18 |
Correct |
34 ms |
28500 KB |
Output is correct |
19 |
Correct |
40 ms |
28632 KB |
Output is correct |
20 |
Correct |
6 ms |
11168 KB |
Output is correct |
21 |
Correct |
6 ms |
11220 KB |
Output is correct |
22 |
Correct |
6 ms |
11740 KB |
Output is correct |
23 |
Correct |
36 ms |
28460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
11220 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
116 ms |
44296 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |