#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
const int INF = 2e9;
struct arco_t{
int a, p;
};
int tmp, maxi, diff;
vector<vector<arco_t>> adj;
vector<int> dist1;
vector<int> dist2;
vector<bool> vis;
void dfs(int node, int father){
vis[node] = 1;
for(auto i : adj[node])if(i.a != father){
dist1[i.a] = dist1[node] + i.p;
dfs(i.a, node);
if(tmp < dist1[i.a]){
tmp = dist1[i.a];
maxi = i.a;
}
}
}
void findCenter(int node, int father){
for(auto i : adj[node])if(i.a != father){
dist2[i.a] = dist2[node] + i.p;
findCenter(i.a, node);
if(tmp > max(dist1[i.a], dist2[i.a])){
tmp = max(dist1[i.a], dist2[i.a]);
maxi = i.a;
}
}
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
adj.resize(N);
dist1.resize(N);
dist2.resize(N);
vis.resize(N, 0);
for(int i=0; i<M; i++){
adj[A[i]].push_back({B[i], T[i]});
adj[B[i]].push_back({A[i], T[i]});
}
vector<int> c;
for(int i=0; i<N; i++){
if(!vis[i]){
dist1[i] = 0;
tmp = 0;
maxi = i;
dfs(i, -1);
tmp = 0;
dist1[maxi] = 0;
dfs(maxi, -1);
c.push_back(maxi);
}
}
vector<int> x;
for(auto i : c){
maxi = i;
tmp = INF;
dist2[i] = 0;
findCenter(i, -1);
x.push_back(maxi);
}
tmp = 0, maxi = -1;
for(auto i : x)
if(tmp < max(dist1[i], dist2[i])){
tmp = max(dist1[i], dist2[i]);
maxi = i;
}
int ans = 0;
for(auto i : x)if(i != maxi){
ans = max(L + tmp + max(dist1[i], dist2[i]), ans);
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
69 ms |
14328 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
69 ms |
14328 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
69 ms |
14328 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
6184 KB |
Output is correct |
2 |
Incorrect |
34 ms |
6272 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
69 ms |
14328 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
69 ms |
14328 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |