#include <bits/stdc++.h>
#include <dreaming.h>
using namespace std;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#define mp make_pair
#define fi first
#define se second
#define all(v) v.begin(),v.end()
#define allarr(a) a , a + n
#define ll long long
#define pb push_back
#define fastio ios_base::sync_with_stdio(false) ; cin.tie(NULL); cout.tie(NULL)
typedef pair<int, int> pi;
typedef pair<ll,ll> pll ;
typedef pair<int,pi> trp ;
typedef vector<pi> vpi;
typedef priority_queue< int, vector <int> , greater<int> > spq;
const int NN = 1e5+5 ;
vector<pll> adj[NN] ;
ll dp1[NN],dp2 [NN] , chil[NN] , chil1[NN], pass[NN] ,ans , ans1 ;
void dfs0(int u , int p ){
pass[u]=1;
int v ; ll w ;
// cout << "dfs0"<<endl;
for(pi pr : adj[u]){
v = pr.fi ;
w = pr.se ;
if( p != v ){
dfs0(v,u) ;
if( dp1[v] + w > dp1[u]){
dp1[u] = dp1[v] + w ;
chil[u] = v ;
}
}
}
}
void dfs1(int u , int p ){
int v ; ll w ;
pass[u]=2;
// cout << "dfs1"<<endl;
for(pi pr : adj[u]){
v = pr.fi ;
w = pr.se ;
if(p!=v)
dfs1(v,u);
if( p != v && v != chil[u] ){
if( dp1[v]+w > dp2[u] ){
chil1[u] = v ;
dp2[u] = dp1[v]+w ;
}
}
}
}
void dfs2(int u , int p ){
int v ; ll w ;
pass[u]=3;
// cout << "dfs2"<<endl;
for(pi pr : adj[u]){
v = pr.fi ;
w = pr.se ;
if( v != p && v != chil[u] ){
if( dp1[u] + w > dp1[v] ){
dp2[v] = dp1[v] ;
dp1[v] = dp1[u] + w;
chil[v] = u ;
}else if( dp1[u] + w > dp2[v]){
dp2[v] = dp1[u] + w;
chil1[v] = u ;
}
}else if( v != p && v != chil1[u]){
if( dp2[u] + w > dp1[v] ){
dp2[v] = dp1[v] ;
dp1[v] = dp2[u] + w;
chil[v] = u ;
}else if( dp1[u] + w > dp2[v]){
dp2[v] = dp2[u] + w;
chil1[v] = u ;
}
}
if( v!= p )
dfs2(v,u) ;
}
}
void dfs4(int u , int p , ll& Min ){
pass[u]=4;
Min = min(Min,dp1[u]);
ans1 = max(ans1,dp1[u]);
for(pi v : adj[u]){
if(v.fi!=p){
dfs4(v.fi,u,Min);
}
}
}
void edge(int u , int v , int w ){
adj[u].pb({v,1LL*w}) ;
}
int travelTime(int N,int M,int L ,int A[],int B[],int T[]){
// cout <<"x"<<endl;
for(int i = 0 ; i < M ; i++ ){
edge(A[i],B[i],T[i]) ;
edge(B[i],A[i],T[i]) ;
}
for(int i = 0 ; i < N ; i++ )if( pass[i] == 0 ) dfs0(i,-1) ;
for(int i = 0 ; i < N ; i++ )if( pass[i] == 1 ) dfs1(i,-1) ;
for(int i = 0 ; i < N ; i++ )if( pass[i] == 2 ) dfs2(i,-1) ;
ll Min ;
vector<ll> longest_paths;
for(int i = 0 ; i < N ; i++ ){
// cout << i << " " << dp1[i] <<" " <<dp2[i] << endl;
Min = 1e18;
if( pass[i] == 3 ){
dfs4(i,-1,Min);
longest_paths.pb(Min);
}
}
sort(all(longest_paths));
reverse(all(longest_paths)) ;
ans = 0;
// if( N == 2 )return L ;
// for(int p : longest_paths)cout << p << " ";
if( longest_paths.size() > 1 ){
ans = max(ans,L+longest_paths[0]+longest_paths[1]);
if( longest_paths.size() > 2 ){
ans = max(ans,2*L+longest_paths[1]+longest_paths[2]);
}
}
ans = max (ans,ans1);
return ans ;
}
// int main(){
// int n , m , l ;
// cin >> n >> m >> l ;
// int a[n+1] , b[n+1] , t[n+1] ;
// for(int i = 0 ; i < n ;i++ )cin >> a[i]>>b[i]>>t[i] ;
// cout << travelTime(n,m,l,a,b,t) << endl;
// }
Compilation message
dreaming.cpp:5:0: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
#pragma GCC optimization ("O3")
dreaming.cpp:6:0: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
#pragma GCC optimization ("unroll-loops")
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
86 ms |
16948 KB |
Output is correct |
2 |
Correct |
85 ms |
16436 KB |
Output is correct |
3 |
Correct |
55 ms |
13560 KB |
Output is correct |
4 |
Correct |
14 ms |
4728 KB |
Output is correct |
5 |
Correct |
15 ms |
4088 KB |
Output is correct |
6 |
Correct |
26 ms |
5952 KB |
Output is correct |
7 |
Correct |
5 ms |
2808 KB |
Output is correct |
8 |
Correct |
64 ms |
8952 KB |
Output is correct |
9 |
Correct |
84 ms |
11724 KB |
Output is correct |
10 |
Correct |
5 ms |
2808 KB |
Output is correct |
11 |
Correct |
78 ms |
13560 KB |
Output is correct |
12 |
Correct |
113 ms |
15360 KB |
Output is correct |
13 |
Correct |
4 ms |
2808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
86 ms |
16948 KB |
Output is correct |
2 |
Correct |
85 ms |
16436 KB |
Output is correct |
3 |
Correct |
55 ms |
13560 KB |
Output is correct |
4 |
Correct |
14 ms |
4728 KB |
Output is correct |
5 |
Correct |
15 ms |
4088 KB |
Output is correct |
6 |
Correct |
26 ms |
5952 KB |
Output is correct |
7 |
Correct |
5 ms |
2808 KB |
Output is correct |
8 |
Correct |
64 ms |
8952 KB |
Output is correct |
9 |
Correct |
84 ms |
11724 KB |
Output is correct |
10 |
Correct |
5 ms |
2808 KB |
Output is correct |
11 |
Correct |
78 ms |
13560 KB |
Output is correct |
12 |
Correct |
113 ms |
15360 KB |
Output is correct |
13 |
Correct |
4 ms |
2808 KB |
Output is correct |
14 |
Correct |
5 ms |
2680 KB |
Output is correct |
15 |
Correct |
4 ms |
2680 KB |
Output is correct |
16 |
Incorrect |
4 ms |
2680 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
86 ms |
16948 KB |
Output is correct |
2 |
Correct |
85 ms |
16436 KB |
Output is correct |
3 |
Correct |
55 ms |
13560 KB |
Output is correct |
4 |
Correct |
14 ms |
4728 KB |
Output is correct |
5 |
Correct |
15 ms |
4088 KB |
Output is correct |
6 |
Correct |
26 ms |
5952 KB |
Output is correct |
7 |
Correct |
5 ms |
2808 KB |
Output is correct |
8 |
Correct |
64 ms |
8952 KB |
Output is correct |
9 |
Correct |
84 ms |
11724 KB |
Output is correct |
10 |
Correct |
5 ms |
2808 KB |
Output is correct |
11 |
Correct |
78 ms |
13560 KB |
Output is correct |
12 |
Correct |
113 ms |
15360 KB |
Output is correct |
13 |
Correct |
4 ms |
2808 KB |
Output is correct |
14 |
Correct |
5 ms |
2680 KB |
Output is correct |
15 |
Correct |
4 ms |
2680 KB |
Output is correct |
16 |
Incorrect |
4 ms |
2680 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
9060 KB |
Output is correct |
2 |
Correct |
41 ms |
9080 KB |
Output is correct |
3 |
Correct |
36 ms |
9084 KB |
Output is correct |
4 |
Correct |
44 ms |
9080 KB |
Output is correct |
5 |
Correct |
64 ms |
8952 KB |
Output is correct |
6 |
Correct |
64 ms |
9932 KB |
Output is correct |
7 |
Correct |
40 ms |
9332 KB |
Output is correct |
8 |
Correct |
43 ms |
8956 KB |
Output is correct |
9 |
Correct |
41 ms |
8876 KB |
Output is correct |
10 |
Correct |
44 ms |
9204 KB |
Output is correct |
11 |
Correct |
4 ms |
2680 KB |
Output is correct |
12 |
Correct |
12 ms |
6660 KB |
Output is correct |
13 |
Correct |
12 ms |
6896 KB |
Output is correct |
14 |
Correct |
12 ms |
6640 KB |
Output is correct |
15 |
Correct |
12 ms |
6764 KB |
Output is correct |
16 |
Correct |
12 ms |
6500 KB |
Output is correct |
17 |
Correct |
11 ms |
5232 KB |
Output is correct |
18 |
Correct |
13 ms |
6896 KB |
Output is correct |
19 |
Correct |
12 ms |
6508 KB |
Output is correct |
20 |
Correct |
4 ms |
2680 KB |
Output is correct |
21 |
Correct |
4 ms |
2680 KB |
Output is correct |
22 |
Correct |
4 ms |
2808 KB |
Output is correct |
23 |
Correct |
12 ms |
6520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
86 ms |
16948 KB |
Output is correct |
2 |
Correct |
85 ms |
16436 KB |
Output is correct |
3 |
Correct |
55 ms |
13560 KB |
Output is correct |
4 |
Correct |
14 ms |
4728 KB |
Output is correct |
5 |
Correct |
15 ms |
4088 KB |
Output is correct |
6 |
Correct |
26 ms |
5952 KB |
Output is correct |
7 |
Correct |
5 ms |
2808 KB |
Output is correct |
8 |
Correct |
64 ms |
8952 KB |
Output is correct |
9 |
Correct |
84 ms |
11724 KB |
Output is correct |
10 |
Correct |
5 ms |
2808 KB |
Output is correct |
11 |
Correct |
78 ms |
13560 KB |
Output is correct |
12 |
Correct |
113 ms |
15360 KB |
Output is correct |
13 |
Correct |
4 ms |
2808 KB |
Output is correct |
14 |
Correct |
5 ms |
2936 KB |
Output is correct |
15 |
Correct |
6 ms |
2936 KB |
Output is correct |
16 |
Correct |
6 ms |
2936 KB |
Output is correct |
17 |
Correct |
7 ms |
2808 KB |
Output is correct |
18 |
Correct |
6 ms |
2936 KB |
Output is correct |
19 |
Correct |
7 ms |
2984 KB |
Output is correct |
20 |
Correct |
6 ms |
2808 KB |
Output is correct |
21 |
Correct |
5 ms |
2936 KB |
Output is correct |
22 |
Correct |
6 ms |
3064 KB |
Output is correct |
23 |
Correct |
4 ms |
2680 KB |
Output is correct |
24 |
Correct |
4 ms |
2680 KB |
Output is correct |
25 |
Correct |
4 ms |
2680 KB |
Output is correct |
26 |
Incorrect |
4 ms |
2808 KB |
Output isn't correct |
27 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
86 ms |
16948 KB |
Output is correct |
2 |
Correct |
85 ms |
16436 KB |
Output is correct |
3 |
Correct |
55 ms |
13560 KB |
Output is correct |
4 |
Correct |
14 ms |
4728 KB |
Output is correct |
5 |
Correct |
15 ms |
4088 KB |
Output is correct |
6 |
Correct |
26 ms |
5952 KB |
Output is correct |
7 |
Correct |
5 ms |
2808 KB |
Output is correct |
8 |
Correct |
64 ms |
8952 KB |
Output is correct |
9 |
Correct |
84 ms |
11724 KB |
Output is correct |
10 |
Correct |
5 ms |
2808 KB |
Output is correct |
11 |
Correct |
78 ms |
13560 KB |
Output is correct |
12 |
Correct |
113 ms |
15360 KB |
Output is correct |
13 |
Correct |
4 ms |
2808 KB |
Output is correct |
14 |
Correct |
5 ms |
2680 KB |
Output is correct |
15 |
Correct |
4 ms |
2680 KB |
Output is correct |
16 |
Incorrect |
4 ms |
2680 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |