#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> > d1, d2;
dfs(start, 0, d1);
ll len1 = -1, node1;
for(auto t : d1){
if(len1 < t.second){
len1 = t.second;
node1 = t.first;
}
}
d1.clear();
dfs(node1, 0, d1);
map<int, ll> store;
ll len2 = -1, node2;
for(auto t : d1){
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
return max(max(help, arr[0] + arr[1] + L), arr[1] + arr[2] + 2 * L);
}
Compilation message
dreaming.cpp: In function 'void solve(int)':
dreaming.cpp:41:5: warning: 'node2' may be used uninitialized in this function [-Wmaybe-uninitialized]
41 | dfs(node2, 0, d2);
| ~~~^~~~~~~~~~~~~~
dreaming.cpp:30:5: warning: 'node1' may be used uninitialized in this function [-Wmaybe-uninitialized]
30 | dfs(node1, 0, d1);
| ~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
37 ms |
14280 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2772 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
37 ms |
14280 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
25 ms |
5696 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2772 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
37 ms |
14280 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |