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 "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
vector<vector<pair<int,int>>> adjlist; //ind,val
int n;
vector<bool> visited;
vector<int> parent;
vector<int> depth;
int fdist = 0;
int f_ind = 0;
int INF = 1e9;
void getFurthest(int ind, int par){
//cout<<ind<<' '<<par<<'\n';
visited[ind]=true;
parent[ind]=par;
for (auto p : adjlist[ind]){
//cout<<"proc "<<p.first<<' '<<p.second<<'\n';
if (p.first==par){continue;}
depth[p.first]=depth[ind]+p.second;
if (depth[p.first]>fdist){
fdist=depth[p.first];
f_ind=p.first;
}
getFurthest(p.first,ind);
}
}
pair<int,int> getDiamDepth(int ind){
//cout<<"check "<<ind<<'\n';
fdist=0;f_ind=ind;
parent[ind]=-1;
getFurthest(ind,-1);
int ftmp = f_ind;
fdist=0;f_ind=ftmp;
parent[ftmp]=-1;depth[ftmp]=0;
getFurthest(ftmp,-1);
//find min depth
int cur = f_ind;
int md = INF;
while (cur!=-1){
md = min(md, max(depth[cur],fdist-depth[cur]));
cur=parent[cur];
}
//cout<<ind<<": "<<fdist<<' '<<md<<'\n';
return {fdist,md};
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
n=N;
adjlist.resize(n);
for (int i = 0; i<M; i++){
adjlist[A[i]].push_back({B[i],T[i]});
adjlist[B[i]].push_back({A[i],T[i]});
}
visited.resize(n);
parent.resize(n,-1);
depth.resize(n);
int maxdist = -1;
int maxd = -1;
int smaxd = -1;
int tmaxd = -1;
for (int i = 0; i<n; i++){
if (visited[i]){continue;}
auto pii = getDiamDepth(i);
maxdist = max(maxdist, pii.first);
if (pii.second>=maxd){
tmaxd=smaxd;
smaxd=maxd;
maxd=pii.second;
} else if (pii.second>=smaxd){
tmaxd=smaxd;
smaxd=pii.second;
} else if (pii.second>=tmaxd){
tmaxd=pii.second;
}
}
if (tmaxd!=-1){
maxdist=max(maxdist,tmaxd+smaxd+2*L);
}
if (smaxd!=-1){
maxdist=max(maxdist,maxd+smaxd+L);
}
return maxdist;
}
# | 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... |