#include<bits/stdc++.h>
#include"dreaming.h"
#define f first
#define s second
#define ll long long
using namespace std;
const int N = 2e5 + 500;
typedef pair<int , int> ipair;
int n , m , L;
vector<ipair> adj[N];
int component[N];
int dpD[N] , dpU[N];
bool vis[N];
int cmp;
int MN[N];
int mx1[N] , mx2[N];
void dfsD(int u , int BOSS){
vis[u] = 1;
component[u] = cmp;
for(ipair x : adj[u]){
int v = x.f , w = x.s;
if(vis[v])continue;
dfsD(v , BOSS);
dpD[u] = max(dpD[u] , dpD[v] + w);
if(u == BOSS){
if(mx2[component[v]] < dpD[v] + w) mx2[component[v]] = dpD[v] + w;
if(mx2[component[v]] > mx1[component[v]]) swap(mx2[component[v]] , mx1[component[v]]);
}
}
}
void dfsU(int u , int BOSS){
vis[u] = 1;
for(ipair x : adj[u]){
int v = x.f , w = x.s;
if(vis[v]) continue;
if(u == BOSS){
if(dpD[v] + w == mx1[component[v]]){
if(mx2[component[v]] == -1){
dpU[v] = w;
} else
dpU[v] = mx2[component[v]] + w;
} else {
dpU[v] = mx1[component[v]] + w;
}
} else
dpU[v] = dpU[u] + w;
dfsU(v , BOSS);
}
}
int travelTime(int N , int M , int L , int A[] , int B[] , int C[]){
n = N; m = M; L = L;
for(int i = 0; i < m; ++i){
adj[A[i]].emplace_back(B[i] , C[i]);
adj[B[i]].emplace_back(A[i] , C[i]);
}
for(int i = 0; i < n; ++i){
mx1[cmp] = -1 , mx2[cmp] = -1;
if(!vis[i]) {dfsD(i , i); cmp++;};
}
for(int i = 0; i < n; ++i) vis[i] = 0;
for(int i = 0; i < n; ++i){
if(!vis[i]) dfsU(i , i);
}
/*
for(int i = 0; i < n; ++i){
cerr << i << " i : " << dpD[i] << " dpD " << " : " << " dpU " << dpU[i] << endl;
}
*/
for(int i = 0; i < cmp; ++i) MN[i] = 1e18;
for(int i = 0; i < n; ++i){
MN[component[i]] = min(MN[component[i]] , max(dpD[i] , dpU[i]));
}
//for(int i = 0; i < cmp; ++i) cout << i << " cmp " << MN[i] << endl;
int MX1 = -1 , MX2 = -1;
for(int i = 0; i < cmp; ++i){
if(MX2 < MN[i]){
MX2 = MN[i];
if(MX2 > MX1) swap(MX2 , MX1);
}
}
return MX1 + MX2 + L;
}
/*
int main (){
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> m >> L;
for(int i = 0; i < m; ++i){
int u , v , w;
cin >> u >> v >> w;
adj[u].emplace_back(v , w);
adj[v].emplace_back(u , w);
}
for(int i = 0; i < n; ++i){
mx1[cmp] = -1 , mx2[cmp] = -1;
if(!vis[i]) {dfsD(i , i); cmp++;};
}
for(int i = 0; i < n; ++i) vis[i] = 0;
for(int i = 0; i < n; ++i){
if(!vis[i]) dfsU(i , i);
}
/*
for(int i = 0; i < n; ++i){
cerr << i << " i : " << dpD[i] << " dpD " << " : " << " dpU " << dpU[i] << endl;
}
for(int i = 0; i < cmp; ++i) MN[i] = 1e18;
for(int i = 0; i < n; ++i){
MN[component[i]] = min(MN[component[i]] , max(dpD[i] , dpU[i]));
}
//for(int i = 0; i < cmp; ++i) cout << i << " cmp " << MN[i] << endl;
ll MX1 = -1 , MX2 = -1;
for(int i = 0; i < cmp; ++i){
if(MX2 < MN[i]){
MX2 = MN[i];
if(MX2 > MX1) swap(MX2 , MX1);
}
}
cout << MX1 + MX2 + L << endl;
return 0;
}
*/
Compilation message
dreaming.cpp:111:5: warning: "/*" within comment [-Wcomment]
/*
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:75:42: warning: overflow in implicit constant conversion [-Woverflow]
for(int i = 0; i < cmp; ++i) MN[i] = 1e18;
^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
59 ms |
15352 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
59 ms |
15352 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
59 ms |
15352 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
9728 KB |
Output is correct |
2 |
Incorrect |
31 ms |
9700 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
59 ms |
15352 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
59 ms |
15352 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |