#include <bits/stdc++.h>
#include "dreaming.h"
using namespace std;
vector<pair<int,int>> graph[100003];
int odl[100003];
int comp[100003];
vector<int> diameter[100003];
int len[100003];
int iter=0,mx_odl,mx_ver,ans=-1;
void dfs(int v, int p){
comp[v]=iter;
if (odl[v]>mx_odl){
mx_odl=odl[v];
mx_ver=v;
}
for (auto u : graph[v]){
if (u.first==p)continue;
odl[u.first]=odl[v]+u.second;
dfs(u.first,v);
}
}
void farthest(int v){
mx_odl=mx_ver=-1;
odl[v]=0;
dfs(v,-1);
}
bool build_diameter(int v, int p, int fin){
diameter[iter].push_back(v);
if (v==fin)return true;
for (auto u : graph[v]){
if (u.first==p)continue;
if (build_diameter(u.first,v,fin))return true;
}
diameter[iter].pop_back();
return false;
}
int find_diameter(int v){
iter++;
farthest(v);
int st=mx_ver;
farthest(st);
int nd=mx_ver;
len[iter]=mx_odl;
ans=max(ans,len[iter]);
build_diameter(st,-1,nd);
int mn=1e9+1,wyn=0;
for (int i = 0; i<(int)diameter[iter].size(); i++){
if (abs(mx_odl-2*odl[diameter[iter][i]])<mn){
mn=abs(mx_odl-2*odl[diameter[iter][i]]);
wyn=odl[diameter[iter][i]];
}
}
return max(wyn,mx_odl-wyn);
}
int travelTime(int n, int m, int L, int A[], int B[], int T[]){
for (int i = 0; i<m; i++){
graph[A[i]].push_back({B[i],T[i]});
graph[B[i]].push_back({A[i],T[i]});
}
int maxy[3]={-1,-1,-1};
int val;
for (int i = 0; i<n; i++){
if (!comp[i]){
val=find_diameter(i);
if (val>maxy[0]){
maxy[2]=maxy[1];
maxy[1]=maxy[0];
maxy[0]=val;
}
else if (val>maxy[1]){
maxy[2]=maxy[1];
maxy[1]=val;
}
else if (val>maxy[2]){
maxy[2]=val;
}
}
}
if (m==1)ans=max(ans,maxy[0]+maxy[1]+L);
else if (m>1)ans=max({ans,maxy[0]+maxy[1]+L,maxy[1]+maxy[2]+2*L});
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
16876 KB |
Output is correct |
2 |
Correct |
34 ms |
16860 KB |
Output is correct |
3 |
Correct |
22 ms |
13648 KB |
Output is correct |
4 |
Correct |
6 ms |
8540 KB |
Output is correct |
5 |
Correct |
5 ms |
8028 KB |
Output is correct |
6 |
Correct |
10 ms |
9376 KB |
Output is correct |
7 |
Incorrect |
2 ms |
7260 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7260 KB |
Output is correct |
2 |
Correct |
1 ms |
7260 KB |
Output is correct |
3 |
Incorrect |
2 ms |
7256 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
16876 KB |
Output is correct |
2 |
Correct |
34 ms |
16860 KB |
Output is correct |
3 |
Correct |
22 ms |
13648 KB |
Output is correct |
4 |
Correct |
6 ms |
8540 KB |
Output is correct |
5 |
Correct |
5 ms |
8028 KB |
Output is correct |
6 |
Correct |
10 ms |
9376 KB |
Output is correct |
7 |
Incorrect |
2 ms |
7260 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
11344 KB |
Output is correct |
2 |
Correct |
16 ms |
11400 KB |
Output is correct |
3 |
Correct |
16 ms |
11100 KB |
Output is correct |
4 |
Correct |
16 ms |
11236 KB |
Output is correct |
5 |
Correct |
16 ms |
11356 KB |
Output is correct |
6 |
Correct |
17 ms |
11612 KB |
Output is correct |
7 |
Correct |
23 ms |
11424 KB |
Output is correct |
8 |
Correct |
15 ms |
11100 KB |
Output is correct |
9 |
Correct |
17 ms |
11100 KB |
Output is correct |
10 |
Correct |
25 ms |
11572 KB |
Output is correct |
11 |
Correct |
1 ms |
7260 KB |
Output is correct |
12 |
Correct |
7 ms |
10228 KB |
Output is correct |
13 |
Correct |
7 ms |
10328 KB |
Output is correct |
14 |
Correct |
7 ms |
10332 KB |
Output is correct |
15 |
Correct |
7 ms |
10332 KB |
Output is correct |
16 |
Correct |
7 ms |
10332 KB |
Output is correct |
17 |
Correct |
7 ms |
10328 KB |
Output is correct |
18 |
Correct |
7 ms |
10332 KB |
Output is correct |
19 |
Correct |
7 ms |
10332 KB |
Output is correct |
20 |
Correct |
2 ms |
7260 KB |
Output is correct |
21 |
Incorrect |
2 ms |
7260 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7260 KB |
Output is correct |
2 |
Correct |
1 ms |
7260 KB |
Output is correct |
3 |
Incorrect |
2 ms |
7256 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
16876 KB |
Output is correct |
2 |
Correct |
34 ms |
16860 KB |
Output is correct |
3 |
Correct |
22 ms |
13648 KB |
Output is correct |
4 |
Correct |
6 ms |
8540 KB |
Output is correct |
5 |
Correct |
5 ms |
8028 KB |
Output is correct |
6 |
Correct |
10 ms |
9376 KB |
Output is correct |
7 |
Incorrect |
2 ms |
7260 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |