# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
558651 | Leo121 | 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 forn(i, a, b) for(int i = int(a); i <= int(b); ++ i)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
const int maxn = 1e5;
vector<pii> tree[maxn];
vector<int> componente;
pair<pii, pii> maximos[maxn];
bool visitado[maxn];
int res = -1;
void dfs(int u, int p){
maximos[u] = mp(mp(0, u), mp(0, u));
visitado[u] = 1;
for(auto v : tree[u]){
if(v.fi != p){
dfs(v.fi, u);
if(maximos[v.fi].fi.fi + v.se > maximos[u].fi.fi){
swap(maximos[u].fi, maximos[u].se);
maximos[u].fi = mp(maximos[v.fi].fi.fi + v.se, v.fi);
}
else if(maximos[v.fi].fi.fi + v.se > maximos[u].se.fi){
maximos[u].se = mp(maximos[v.fi].fi.fi + v.se, v.fi);
}
}
}
}
int dfsinv(int u, int p){
res = max(res, maximos[u].fi.fi + maximos[u].se.fi);
int maximo = maximos[u].fi.fi, valor;
for(auto v : tree[u]){
if(v.fi != p){
valor = (maximos[u].fi.se == v.fi) ? maximos[u].se.fi : maximos[u].fi.fi;
if(valor + v.se > maximos[v.fi].fi.fi){
swap(maximos[v.fi].fi, maximos[v.fi].se);
maximos[v.fi].fi = mp(valor + v.se, u);
}
else if(valor + v.se > maximos[v.fi].se.fi){
maximos[v.fi].se = mp(valor + v.se, u);
}
maximo = min(maximo, dfsinv(v.fi, u));
}
}
return maximo;
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
forn(i, 0, M - 1){
tree[A[i]].pb(mp(B[i], T[i]));
tree[B[i]].pb(mp(A[i], T[i]));
}
forn(i, 0, N - 1){
if(!visitado[i]){
dfs(i, i);
componente.pb(dfsinv(i, i));
}
}
sort(componente.begin(), componente.end());
int longitud = componente.size();
if(longitud > 1){
res = max(res, componente[longitud - 1] + componente[longitud - 2] + L);
if(longitud > 2){
res = max(res, componente[longitud - 3] + componente[longitud 1] + L);
res = max(res, componente[longitud - 3] + componente[longitud - 2] + 2 * L);
}
}
return res;
}