#include<bits/stdc++.h>
#include "dreaming.h"
using namespace std ;
const int N = 1e5 + 7 ;
int mn ;
int vis[N] ;
int mxx[N] ;
int anss[N] ;
vector<pair<int , int > > adj[N];
int node , dst ;
int ansi , ans2;
int solve1(int x, int p){
int ret = 0 ;
for(auto u : adj[x]){
if(u.first==p)continue ;
int dfx = solve1(u.first , x) + u.second ;
ret = max(ret , dfx) ;
}
anss[x] = ret ;
return mxx[x] = ret ;
}
void dfs(int x , int p , int h){
for(auto u : adj[x]){
if(u.first==p)continue ;
dfs(u.first , x , h + u.second) ;
}
if( h > dst){
node = x ;
dst = h ;
}
ans2 = max(ans2 , h) ;
}
int dfs1(int x, int p , int dp = 0 ){
vis[x] ++ ;
anss[x] = max(anss[x] , dp ) ;
ansi = min(ansi , anss[x]) ;
vector<int> dps , pre , suf;
for(auto u : adj[x]){
if(u.first == p)continue ;
dps.push_back(mxx[u.first] + u.second) ;
int mx = pre.size() ? pre.back() : 0 ;
pre.push_back(max(mx , dps.back())) ;
}
suf.resize(pre.size()) ;
if(suf.size())
suf[suf.size()-1] = dps[suf.size()-1] ;
for(int i = suf.size() -2 ; i >=0 ;i --){
suf[i] = max(dps[i] , suf[i+1]) ;
}
int i = 0 ;
for(auto u : adj[x]){
if(u.first==p)continue ;
int subdp = 0 ;
if(i) subdp = max(subdp , pre[i-1]) ;
if(subdp < suf.size() -1 ) subdp = max(subdp , suf[i+1]) ;
dfs1(u.first , x , max(dp , subdp) + u.second) ;
i++;
}
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
for(int i =0 ; i< M ;i++){
adj[A[i] +1 ].push_back({B[i]+1 , T[i]}) ;
adj[B[i]+1].push_back({A[i]+1 , T[i]}) ;
}
vector<int> v;
multiset<int> mul ;
for(int i = 1; i <=N ; i++){
if(vis[i])continue ;
ansi = 2e9 ;
dfs(i , i , 0) ; dst = 0 ;
dfs(node , node , 0) ;
solve1(i , i ) ;
dfs1(i , i) ;
v.push_back(ansi) ;
mul.insert(ansi) ;
}
if(v.size() == 1)return ans2 ;
else if(v.size() ==2 )return max(v[0] + v[1] + L , ans2) ;
int s = v.size() ;
int ans = 2e9 ;
for(auto u : v){
mul.erase(mul.find(u)) ;
int mx1 = -1 , mx2 = -1 ;
auto e = mul.end() ;
e-- ; mx1 = *e ;
e-- ; mx2 = *e ;
int now = max(u + mx1 + L , mx1 + mx2 + L + L ) ;
ans = min(ans , now ) ;
mul.insert(u) ;
}
return max(ans , ans2) ;
}
Compilation message
dreaming.cpp: In function 'int dfs1(int, int, int)':
dreaming.cpp:62:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(subdp < suf.size() -1 ) subdp = max(subdp , suf[i+1]) ;
~~~~~~^~~~~~~~~~~~~~~
dreaming.cpp:66:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:88:9: warning: unused variable 's' [-Wunused-variable]
int s = v.size() ;
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
88 ms |
20344 KB |
Output is correct |
2 |
Correct |
85 ms |
19320 KB |
Output is correct |
3 |
Correct |
61 ms |
18808 KB |
Output is correct |
4 |
Correct |
15 ms |
5248 KB |
Output is correct |
5 |
Correct |
13 ms |
3968 KB |
Output is correct |
6 |
Correct |
25 ms |
6528 KB |
Output is correct |
7 |
Correct |
6 ms |
2688 KB |
Output is correct |
8 |
Correct |
41 ms |
9724 KB |
Output is correct |
9 |
Correct |
59 ms |
15224 KB |
Output is correct |
10 |
Correct |
6 ms |
2816 KB |
Output is correct |
11 |
Correct |
107 ms |
14968 KB |
Output is correct |
12 |
Correct |
96 ms |
17632 KB |
Output is correct |
13 |
Correct |
6 ms |
2816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
88 ms |
20344 KB |
Output is correct |
2 |
Correct |
85 ms |
19320 KB |
Output is correct |
3 |
Correct |
61 ms |
18808 KB |
Output is correct |
4 |
Correct |
15 ms |
5248 KB |
Output is correct |
5 |
Correct |
13 ms |
3968 KB |
Output is correct |
6 |
Correct |
25 ms |
6528 KB |
Output is correct |
7 |
Correct |
6 ms |
2688 KB |
Output is correct |
8 |
Correct |
41 ms |
9724 KB |
Output is correct |
9 |
Correct |
59 ms |
15224 KB |
Output is correct |
10 |
Correct |
6 ms |
2816 KB |
Output is correct |
11 |
Correct |
107 ms |
14968 KB |
Output is correct |
12 |
Correct |
96 ms |
17632 KB |
Output is correct |
13 |
Correct |
6 ms |
2816 KB |
Output is correct |
14 |
Correct |
6 ms |
2688 KB |
Output is correct |
15 |
Correct |
6 ms |
2688 KB |
Output is correct |
16 |
Incorrect |
6 ms |
2688 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
88 ms |
20344 KB |
Output is correct |
2 |
Correct |
85 ms |
19320 KB |
Output is correct |
3 |
Correct |
61 ms |
18808 KB |
Output is correct |
4 |
Correct |
15 ms |
5248 KB |
Output is correct |
5 |
Correct |
13 ms |
3968 KB |
Output is correct |
6 |
Correct |
25 ms |
6528 KB |
Output is correct |
7 |
Correct |
6 ms |
2688 KB |
Output is correct |
8 |
Correct |
41 ms |
9724 KB |
Output is correct |
9 |
Correct |
59 ms |
15224 KB |
Output is correct |
10 |
Correct |
6 ms |
2816 KB |
Output is correct |
11 |
Correct |
107 ms |
14968 KB |
Output is correct |
12 |
Correct |
96 ms |
17632 KB |
Output is correct |
13 |
Correct |
6 ms |
2816 KB |
Output is correct |
14 |
Correct |
6 ms |
2688 KB |
Output is correct |
15 |
Correct |
6 ms |
2688 KB |
Output is correct |
16 |
Incorrect |
6 ms |
2688 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
59 ms |
9332 KB |
Output is correct |
2 |
Correct |
63 ms |
9332 KB |
Output is correct |
3 |
Correct |
64 ms |
9332 KB |
Output is correct |
4 |
Correct |
68 ms |
9332 KB |
Output is correct |
5 |
Correct |
64 ms |
9332 KB |
Output is correct |
6 |
Correct |
66 ms |
9972 KB |
Output is correct |
7 |
Correct |
65 ms |
9588 KB |
Output is correct |
8 |
Correct |
72 ms |
9204 KB |
Output is correct |
9 |
Correct |
64 ms |
9076 KB |
Output is correct |
10 |
Correct |
66 ms |
9460 KB |
Output is correct |
11 |
Correct |
6 ms |
2688 KB |
Output is correct |
12 |
Correct |
45 ms |
8952 KB |
Output is correct |
13 |
Correct |
45 ms |
8952 KB |
Output is correct |
14 |
Correct |
45 ms |
9080 KB |
Output is correct |
15 |
Correct |
41 ms |
8952 KB |
Output is correct |
16 |
Correct |
43 ms |
8952 KB |
Output is correct |
17 |
Correct |
50 ms |
8952 KB |
Output is correct |
18 |
Correct |
42 ms |
8952 KB |
Output is correct |
19 |
Correct |
45 ms |
8952 KB |
Output is correct |
20 |
Correct |
6 ms |
2816 KB |
Output is correct |
21 |
Correct |
6 ms |
2688 KB |
Output is correct |
22 |
Correct |
7 ms |
2816 KB |
Output is correct |
23 |
Correct |
42 ms |
8952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
88 ms |
20344 KB |
Output is correct |
2 |
Correct |
85 ms |
19320 KB |
Output is correct |
3 |
Correct |
61 ms |
18808 KB |
Output is correct |
4 |
Correct |
15 ms |
5248 KB |
Output is correct |
5 |
Correct |
13 ms |
3968 KB |
Output is correct |
6 |
Correct |
25 ms |
6528 KB |
Output is correct |
7 |
Correct |
6 ms |
2688 KB |
Output is correct |
8 |
Correct |
41 ms |
9724 KB |
Output is correct |
9 |
Correct |
59 ms |
15224 KB |
Output is correct |
10 |
Correct |
6 ms |
2816 KB |
Output is correct |
11 |
Correct |
107 ms |
14968 KB |
Output is correct |
12 |
Correct |
96 ms |
17632 KB |
Output is correct |
13 |
Correct |
6 ms |
2816 KB |
Output is correct |
14 |
Correct |
6 ms |
2688 KB |
Output is correct |
15 |
Correct |
7 ms |
2816 KB |
Output is correct |
16 |
Correct |
8 ms |
2816 KB |
Output is correct |
17 |
Correct |
6 ms |
2816 KB |
Output is correct |
18 |
Correct |
7 ms |
2816 KB |
Output is correct |
19 |
Correct |
8 ms |
2816 KB |
Output is correct |
20 |
Correct |
6 ms |
2688 KB |
Output is correct |
21 |
Correct |
7 ms |
2816 KB |
Output is correct |
22 |
Correct |
8 ms |
2944 KB |
Output is correct |
23 |
Correct |
6 ms |
2688 KB |
Output is correct |
24 |
Correct |
6 ms |
2688 KB |
Output is correct |
25 |
Correct |
6 ms |
2688 KB |
Output is correct |
26 |
Incorrect |
6 ms |
2688 KB |
Output isn't correct |
27 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
88 ms |
20344 KB |
Output is correct |
2 |
Correct |
85 ms |
19320 KB |
Output is correct |
3 |
Correct |
61 ms |
18808 KB |
Output is correct |
4 |
Correct |
15 ms |
5248 KB |
Output is correct |
5 |
Correct |
13 ms |
3968 KB |
Output is correct |
6 |
Correct |
25 ms |
6528 KB |
Output is correct |
7 |
Correct |
6 ms |
2688 KB |
Output is correct |
8 |
Correct |
41 ms |
9724 KB |
Output is correct |
9 |
Correct |
59 ms |
15224 KB |
Output is correct |
10 |
Correct |
6 ms |
2816 KB |
Output is correct |
11 |
Correct |
107 ms |
14968 KB |
Output is correct |
12 |
Correct |
96 ms |
17632 KB |
Output is correct |
13 |
Correct |
6 ms |
2816 KB |
Output is correct |
14 |
Correct |
6 ms |
2688 KB |
Output is correct |
15 |
Correct |
6 ms |
2688 KB |
Output is correct |
16 |
Incorrect |
6 ms |
2688 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |