# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
976183 | Amaarsaa | 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<bits/stdc++.h>
using namespace std;
vector < pair < int , int > > adj[100005];
vector < int > v;
vector < int > q;
int mx, mx_cnt;
int d[100004], used[100004];
void Dia(int node, int parent, int dist) {
d[node] = dist;
q.push_back(node);
if ( d[node] > mx_cnt) {
mx_cnt = d[node];
mx = node;
}
v.push_back(node);
used[node] = 1;
for ( pair < int, int >& A : adj[node]) {
int child = A.first;
if ( child == parent) continue;
Dia(child, node, dist + A.second);
}
}
int Dfs(int node, int parent, int fn) {
int baiga = 0;
if ( node == fn) return 1;
for ( pair < int, int >& A : adj[node]) {
int child = A.first;
if ( child == parent) continue;
v.push_back(child);
if(Dfs(child, node, fn)) {
baiga = 1;
}
else {
v.pop_back();
}
}
return baiga;
}
vector < int > Ans;
void Go(int x) {
int y;
mx = x;
mx_cnt = 0;
Dia(x, x, 0);
for ( int X : q) d[X] = 0;
q.clear();
x = mx;
mx_cnt = 0;
Dia(mx, mx, 0);
y = mx;
int dist = mx_cnt;
v.clear();
v.push_back(x);
Dfs(x, x, y);
int s = 1e9;
for ( int X : v) {
s = min(s, max(d[X], dist -d[X]));
}
Ans.push_back(s);
}
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]});
}
for (int i = 0; i < N; i ++) {
if (!used[i]) {
Go(i);
}
}
sort(Ans.rbegin(), Ans.rend());
if ( Ans.size() == 2) return Ans[0] + Ans[1] + L;
return max(Ans[0] + Ans[1] , Ans[1] + Ans[2] + L) + L;
}