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>
using namespace std;
const int MAX_N = 100000;
vector<pair<int, int> > graph[MAX_N];
int dist[MAX_N], papa[MAX_N], distpapa[MAX_N];
int diam[MAX_N], comp[MAX_N], top;
bool viz[MAX_N];
int calcDist(int nod, int p) {
int rez = nod;
viz[nod] = true;
for(auto it: graph[nod]) {
if(it.first != p) {
dist[it.first] = dist[nod] + it.second;
papa[it.first] = nod;
distpapa[it.first] = it.second;
int x = calcDist(it.first, nod);
if(dist[x] >= dist[rez])
rez = x;
}
}
return rez;
}
int getFarthest(int nod) {
int x;
dist[nod] = 0;
papa[nod] = -1;
nod = calcDist(nod, -1);
x = nod;
int d = dist[nod];
top = 0;
while(nod != -1) {
diam[top++] = d;
d -= distpapa[nod];
nod = papa[nod];
}
return x;
}
bool cmp(int a, int b) {
return a > b;
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
int tc = 0;
for(int i = 0; i < M; ++i) {
graph[A[i]].push_back(make_pair(B[i], T[i]));
graph[B[i]].push_back(make_pair(A[i], T[i]));
}
int bd = 0;
for(int i = 0; i < N; ++i) {
if(!viz[i]) {
int c1, c2;
c1 = getFarthest(i);
c2 = getFarthest(c1);
comp[tc] = 2000000000;
int d = diam[0];
bd = max(bd, d);
for(int i = 0; i < top; ++i)
comp[tc] = min(comp[tc], max(diam[i], d - diam[i]));
tc++;
}
}
sort(comp, comp + tc, cmp);
if(tc == 1)
return bd;
else if(tc == 2)
return max(bd, comp[0] + comp[1] + L);
else {
int rez = 2000000000;
for(int i = 0; i < 3; ++i) {
int xd[3], t = 1;
xd[0] = comp[i];
for(int j = 0; j < 3; ++j)
if(i != j)
xd[t++] = comp[j] + L;
sort(xd, xd + t, cmp);
rez = min(rez, xd[0] + xd[1]);
}
return max(bd, rez);
}
}
Compilation message (stderr)
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:64:12: warning: variable 'c2' set but not used [-Wunused-but-set-variable]
int c1, c2;
^~
# | 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... |