#include "dreaming.h"
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
int ans = 0;
vector<vector<pair<int, int>>> v;
vector<bool> bl;
vector<int> dis;
void dfs1(int nd, int ss){
bl[nd] = 1;
for(auto [x, w]: v[nd]) if(x != ss) dfs1(x, nd);
for(auto [x, w]: v[nd]) if(x != ss) dis[nd] = max(dis[nd], dis[x]+w);
}
int dfs2(int nd, int ss, int s = 0){
int mx = s, smx = s;
int rt = s;
for(auto [x, w]: v[nd]) if(x != ss){
rt = max(rt, dis[x]+w);
if(dis[x]+w > mx) smx = mx, mx = dis[x]+w;
else if(dis[x]+w > smx) smx = dis[x]+w;
}
ans = max(ans, rt);
for(auto [x, w]: v[nd]) if(x != ss) rt = min(rt, dfs2(x, nd, (dis[x]+w == mx?smx:mx)+w));
return rt;
}
int travelTime(int n, int m, int L, int A[], int B[], int T[]){
v.assign(n, {});
bl.assign(n, 0);
dis.assign(n, 0);
for(int i = 0; i < m; i++){
v[A[i]].pb({B[i], T[i]});
v[B[i]].pb({A[i], T[i]});
}
vector<int> sth;
for(int i = 0; i < n; i++){
if(bl[i]) continue;
dfs1(i, -1);
sth.pb(dfs2(i, -1));
}
sort(sth.rbegin(), sth.rend());
if(sth.size() == 1) return ans;
int ans = max(ans, sth[0]+sth[1]+L);
if(sth.size() > 2) ans = max(ans, sth[1]+sth[2]+2*L);
return ans;
}
Compilation message
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:55:9: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
55 | int ans = max(ans, sth[0]+sth[1]+L);
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
36 ms |
10572 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
36 ms |
10572 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
5588 KB |
Output is correct |
2 |
Correct |
18 ms |
5636 KB |
Output is correct |
3 |
Correct |
21 ms |
5508 KB |
Output is correct |
4 |
Correct |
24 ms |
5592 KB |
Output is correct |
5 |
Correct |
22 ms |
5588 KB |
Output is correct |
6 |
Correct |
20 ms |
6076 KB |
Output is correct |
7 |
Correct |
22 ms |
5832 KB |
Output is correct |
8 |
Correct |
17 ms |
5464 KB |
Output is correct |
9 |
Correct |
16 ms |
5460 KB |
Output is correct |
10 |
Correct |
20 ms |
5688 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
4 ms |
3664 KB |
Output is correct |
13 |
Correct |
4 ms |
3664 KB |
Output is correct |
14 |
Correct |
5 ms |
3664 KB |
Output is correct |
15 |
Correct |
4 ms |
3664 KB |
Output is correct |
16 |
Correct |
6 ms |
3664 KB |
Output is correct |
17 |
Correct |
5 ms |
3676 KB |
Output is correct |
18 |
Correct |
5 ms |
3664 KB |
Output is correct |
19 |
Correct |
5 ms |
3664 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
4 ms |
3664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
36 ms |
10572 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |