# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
968274 | Hugo1729 | Dreaming (IOI13_dreaming) | C++11 | 0 ms | 0 KiB |
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;
#define f first
#define s second
vector<pair<int,int>> adj[100000];
int dp1[100000],dp2[100000],dp3[100000],dp4[100000];
bool marked[100000]={0};
int dfs_sizes1(int v, int pV){
dp1[v]=0;
dp2[v]=0;
marked[v]=1;
for(auto [w,d] : adj[v]){
if(w!=pV){
int sus=dfs_sizes1(w,v)+d;
if(sus>dp1[v]){
dp2[v]=dp1[v];
dp1[v]=sus;
}else if(sus>dp2[v]){
dp2[v]=sus;
}
}
}
return dp1[v];
}
void dfs_sizes2(int v, int pV){
for(auto [w,d] : adj[v]){
if(w!=pV){
if(dp1[w]+d==dp1[v]){
dp3[w]=max(dp2[v],dp3[v])+d;
}else{
dp3[w]=max(dp1[v],dp3[v])+d;
}
dfs_sizes2(w,v);
}
}
dp4[v]=dp1[v]+max(dp3[v],dp2[v]);
}
int dfs_final(int v, int pV){
int ans=max(dp1[v],dp3[v]);
for(auto [w,d] : adj[v]){
if(w!=pV){
ans=min(ans,dfs_final(w,v));
}
}
return ans;
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
for(int i=0;i<M;i++){
adj[A[i]].push_back({B[i],T[i]});
adj[B[i]].push_back({A[i],T[i]});
}
vector<int> sussy;
int amongus=0;
for(int i=0;i<N;i++){
if(!marked[i]){
dfs_sizes1(i,i);
dfs_sizes2(i,i);
sussy.push_back(-dfs_final(i,i));
// cout <<i << ' ' << sussy[sussy.size()-1] << '\n';
}
amongus=max(amongus,dp4[i]);
// cout << i << ' ' << dp1[i].first << ' ' << dp1[i].second << '\n';
}
for(int i=0;i<N;i++){
// cout << i << ' ' << dp1[i] << ' ' << dp2[i] << ' ' << dp3[i] << '\n';
}
sort(sussy.begin(),sussy.end());
if(sussy.size()==1)return amongus;
else if(sussy.size()==2)return max(-sussy[0]-sussy[1]+L,amongus);
return max(-sussy[0]-sussy[1]+L,-sussy[2]-sussy[1]+L*2,amongus);
}