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>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
int ans = 0;
vector<vector<pair<int, int>>> v;
vector<bool> bl;
vector<int> dis;
void dfs1(int nd, int ss){
bl[nd] = 1;
for(auto [x, w]: v[nd]) if(x != ss) dfs1(x, nd);
for(auto [x, w]: v[nd]) if(x != ss) dis[nd] = max(dis[nd], dis[x]+w);
}
int dfs2(int nd, int ss, int s = 0){
int mx = s, smx = s;
int rt = s;
for(auto [x, w]: v[nd]) if(x != ss){
rt = max(rt, dis[x]+w);
if(dis[x]+w > mx) smx = mx, mx = dis[x]+w;
else if(dis[x]+w > smx) smx = dis[x]+w;
}
ans = max(ans, rt);
for(auto [x, w]: v[nd]) if(x != ss) rt = min(rt, dfs2(x, nd, (dis[x]+w == mx?smx:mx)+w));
return rt;
}
int travelTime(int n, int m, int L, int A[], int B[], int T[]){
v.assign(n, {});
bl.assign(n, 0);
dis.assign(n, 0);
for(int i = 0; i < m; i++){
v[A[i]].pb({B[i], T[i]});
v[B[i]].pb({A[i], T[i]});
}
vector<int> sth;
for(int i = 0; i < n; i++){
if(bl[i]) continue;
dfs1(i, -1);
sth.pb(dfs2(i, -1));
}
sort(sth.rbegin(), sth.rend());
if(sth.size() == 1) return ans;
ans = max(ans, sth[0]+sth[1]+L);
if(sth.size() > 2) ans = max(ans, sth[1]+sth[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... |