#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+10;
vector<pair<int, int>> g[N];
int vis[N], n, m, l, dis[N], par[N];
int mx=-1;
void dfs(int u){
vis[u]=1;
if (mx==-1 || dis[mx]<dis[u]) mx=u;
for (auto &e:g[u]){
int v=e.first, w=e.second;
if (v==par[u]) continue;
par[v]=u; dis[v]=dis[u]+w;
dfs(v);
}
}
pair<int, int> find_diameter(int u){
mx=-1; dis[u]=0; par[u]=-1; dfs(u);
u=mx; mx=-1; dis[u]=0; par[u]=-1; dfs(u);
int v=mx;
pair<int, int> ans;
ans.first=dis[v];
ans.second=1e9;
while (v!=-1){
ans.second=min(ans.second, max(dis[v], dis[mx]-dis[v]));
v=par[v];
}
return ans;
}
int travelTime(int _N, int M, int L, int A[], int B[], int T[]) {
n=_N, m=M, l=L;
for (int i=0; i<m; ++i) g[A[i]].emplace_back(B[i], T[i]), g[B[i]].emplace_back(A[i], T[i]);
vector<pair<int, int>> v;
for (int i=0; i<n; ++i) if (!vis[i]){
v.push_back(find_diameter(i));
}
if ((int)v.size()==1) return v[0].first;
if ((int)v.size()==2) return v[0].second+v[1].second+l;
sort(v.begin(), v.end(), [&](pair<int, int> x, pair<int, int> y){ return x.second>y.second; });
return max(v[0].second+v[1].second+l, v[1].second+v[2].second+l+l);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
28 ms |
14672 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
4952 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
28 ms |
14672 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
7904 KB |
Output is correct |
2 |
Correct |
12 ms |
7896 KB |
Output is correct |
3 |
Correct |
14 ms |
7904 KB |
Output is correct |
4 |
Correct |
14 ms |
7968 KB |
Output is correct |
5 |
Correct |
14 ms |
8020 KB |
Output is correct |
6 |
Correct |
16 ms |
8664 KB |
Output is correct |
7 |
Correct |
13 ms |
8160 KB |
Output is correct |
8 |
Correct |
13 ms |
7996 KB |
Output is correct |
9 |
Correct |
13 ms |
8144 KB |
Output is correct |
10 |
Correct |
12 ms |
8168 KB |
Output is correct |
11 |
Correct |
1 ms |
4956 KB |
Output is correct |
12 |
Correct |
4 ms |
6096 KB |
Output is correct |
13 |
Correct |
4 ms |
6096 KB |
Output is correct |
14 |
Correct |
4 ms |
6136 KB |
Output is correct |
15 |
Correct |
4 ms |
6100 KB |
Output is correct |
16 |
Correct |
4 ms |
6100 KB |
Output is correct |
17 |
Correct |
4 ms |
6100 KB |
Output is correct |
18 |
Correct |
4 ms |
6352 KB |
Output is correct |
19 |
Correct |
4 ms |
6096 KB |
Output is correct |
20 |
Correct |
1 ms |
4952 KB |
Output is correct |
21 |
Correct |
1 ms |
4956 KB |
Output is correct |
22 |
Correct |
1 ms |
4956 KB |
Output is correct |
23 |
Correct |
3 ms |
6000 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
4952 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
28 ms |
14672 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |