This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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=0;
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==n-1)ans=max(ans,maxy[0]+maxy[1]+L);
else if (m<n-1)ans=max({ans,maxy[0]+maxy[1]+L,maxy[1]+maxy[2]+2*L});
return ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |