# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
110473 | ioilolcom | Dreaming (IOI13_dreaming) | C++14 | 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 "dreaming.h"
#include <bits/stdc++.h>
#define y second
#define x first
#define ll long long int
#define pb push_back
#define pii pair<int,int>
using namespace std;
const int N=1e5+7;
vector<pair<int,int> > adj[N];
bool vis[N];
void dfs(int node,int p,int length,pii &ans){
vis[node]=1;
for(auto vertice:adj[node]) {
if(vertice.x==p) {
continue;
}
dfs(vertice.x,node,length+vertice.y,ans);
}
if(length>ans.y) {
ans.x=node;
ans.y=length;
}
}
bool fn(int s,int e,int p,vector<int> &path){
if(s==e) {
return true;
}
for(auto v:adj[s]) {
if(v.x==p) continue;
if(fn(v.x,e,s,path)) {
path.push_back(v.x);
return true;
}
}
return false;
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
int n=N;
int m=M;
int l=L;
vector<int> radius;
map<pair<int,int>,int> cost;
for(int i=0; i<M; i++) {
int u=A[i];
int v=B[i];
int w=T[i];
adj[u].pb({v,w});
adj[v].pb({u,w});
cost[{u,v}]=cost[{v,u}]=w;
}
int ans=0;
vector<int> a;
for(int i=0; i<N; i++) {
if(vis[i]) continue;
pii s={-1,-1};
dfs(1, -1, 0,s );
pii e={-1,-1};
dfs(s.f,-1,0,e);
vector<int> path;
fn(s.f,e.f,-1,path);
path.push_back(s.f);
int cur=e.s;
int c=e.s;
ans=max(ans,e.s);
for(int j=1; j<(int)path.size(); j++) {
cur-=cost[{path[j],path[j-1]}];
c=min(c,max(cur,e.s-cur));
}
a.push_back(c);
}
sort(a.rbegin(),a.rend());
if(a.size()>1) {
ans=max(ans,a[0]+a[1]+L);
}
if(a.size()>2) {
ans=max(ans,a[1]+a[2]+2*L);
}
return ans;
}