#include "dreaming.h"
#include <bits/stdc++.h>
#define ll long long
#define mp make_pair
#define pb push_back
using namespace std;
vector<pair<int,long long> > G[100100];
bool vis[100100];
ll ans, help = 0;
void dfs(int at, long long dist, vector<pair<int, long long> >& paths){
vis[at] = true;
paths.push_back(make_pair(at, dist));
for(auto next : G[at]){
if(!vis[next.first]){
dfs(next.first, dist + next.second, paths);
}
}
}
void solve(int start){
vector<pair<int, ll> > d, d2;
dfs(start, 0, d);
ll len1 = -1;
int node1;
for(auto t : d){
if(len1 < t.second){
len1 = t.second;
node1 = t.first;
}
}
d.clear();
dfs(node1, 0, d);
ll len2 = -1;
int node2;
map<int,ll> store;
for(auto t : d){
store[t.first] = t.second;
if(len2 < t.second){
len2 = t.second;
node2 = t.first;
}
}
help = max(help, len2);
dfs(node2, 0, d2);
for(auto t : d2){
ans = min(ans, max(t.second, store[t.first]));
}
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
memset(vis, false, sizeof vis);
for(int i = 0; i < M; i++){
G[A[i]].pb(mp(B[i], T[i]));
G[B[i]].pb(mp(A[i], T[i]));
}
vector<ll> arr;
for(int i = 0; i < N; i++){
if(!vis[i]){
ans = 2e9;
solve(i);
arr.pb(ans);
}
else
continue;
}
sort(arr.rbegin(), arr.rend());
if(arr.size() == 1) return max(arr[0], help);
else if(arr.size() == 2)
return max(help, arr[0] + arr[1] + L);
else
return max(max(arr[0] + arr[1] + L, arr[1] + arr[2] + 2 * L), help);
}
Compilation message
dreaming.cpp: In function 'void solve(int)':
dreaming.cpp:43:5: warning: 'node2' may be used uninitialized in this function [-Wmaybe-uninitialized]
43 | dfs(node2, 0, d2);
| ~~~^~~~~~~~~~~~~~
dreaming.cpp:31:5: warning: 'node1' may be used uninitialized in this function [-Wmaybe-uninitialized]
31 | dfs(node1, 0, d);
| ~~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
46 ms |
14140 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
2772 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
46 ms |
14140 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
26 ms |
5732 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
2772 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
46 ms |
14140 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |