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;
int const NN=1e5+5;
vector<int> adj[NN];
map<pair<int,int>,int> wei;
int n;
multiset<int> dist[NN];
bool vis[NN];
int max_d[NN];
void dfs(int node){
vis[node]=1;
dist[node].insert(0);
dist[node].insert(0);
for (auto i:adj[node])
if(vis[i]==0){
dfs(i);
dist[node].insert(*(dist[i].rbegin())+wei[{node,i}]);
}
}
void change_root(int u,int v){
dist[v].erase(dist[v].find(*(dist[u].rbegin())+wei[{u,v}]));
dist[u].insert(*(dist[v].rbegin())+wei[{u,v}]);
}
int find_r=1e9;
int ans1=0;
void reroot(int node,int par){
max_d[node]=*(dist[node].rbegin());
find_r=min(find_r,max_d[node]);
auto p=dist[node].end();
p--;p--;
ans1=max(ans1,max_d[node]+(*(p)));
for (auto i:adj[node])
if(i!=par){
change_root(i,node);
reroot(i,node);
change_root(node,i);
}
}
int travelTime(int nn, int mm, int L, int A[], int B[], int T[]){
n=nn;
for (int i = 0; i < mm; ++i)
{
int u=A[i];
int v=B[i];
adj[u].push_back(v);
adj[v].push_back(u);
wei[{u,v}]=T[i];
wei[{v,u}]=T[i];
}
int ans=L;
int cnt=0;
vector<int> vec;
for (int i = 0; i < n; ++i)
if(vis[i]==0){
dfs(i);
find_r=*(dist[i].rbegin());
reroot(i,-1);
vec.push_back(find_r);
cnt++;
}
sort(vec.begin(), vec.end());
if(cnt==1)
return ans1;
ans+=vec[cnt-1]+vec[cnt-2];
if(cnt==2)
return max(ans,ans1);
ans=max(ans,vec[cnt-2]+vec[cnt-3]+L+L);
// for (int i = 0; i < n; ++i)
// cout<<max_d[i]<<endl;
return max(ans,ans1);
}
# | 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... |