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 ff first
#define ss second
#define pb push_back
#define ALL(x) x.begin(),x.end()
using namespace std;
using VI = vector <int>;
using PII = pair <int, int>;
const int N = 1e5+5;
vector <PII> paths[N];
bool vis[N];
int n, m, L, sizes[N], len;
PII dp[N];
PII getFarthest(int pos, int par = 0) {
dp[pos] = {0, pos};
for (auto [a, b] : paths[pos]) {
if (a == par) continue;
PII tmp = getFarthest(a, pos);
if (dp[pos].ff < tmp.ff + b) dp[pos] = {tmp.ff + b, tmp.ss};
}
return dp[pos];
}
int getSizes(int pos, int par = 0) {
vis[pos] = 1;
for (auto [a, b] : paths[pos]) {
if (a == par) continue;
sizes[pos] = max(sizes[pos], b + getSizes(a, pos));
}
return sizes[pos];
}
VI findCentroid(int pos, int par = 0, int pre = 0) {
int suf = len - pre;
VI res = {pos, pre, suf};
for (auto [a, b] : paths[pos]) {
if (a == par) continue;
int x = pre+b, y = sizes[a];
if (max(pre, suf) >= max(x, y)) {
VI tmp = findCentroid(a, pos, pre+b);
if (max(pre, suf) > max(tmp[1], tmp[2])) res = tmp;
}
}
return res;
}
VI getCentroid(int pos) {
len = getSizes(pos);
return findCentroid(pos);
}
stack <int> stk;
void dfs(int pos, int par = 0) {
vis[pos] = 1;
stk.push(pos);
for (auto [a, b] : paths[pos]) {
if (a == par) continue;
dfs(a, pos);
}
}
int dbg = 0;
PII calc(int pos) {
queue <VI> bfs; bfs.push({pos, 0, 0});
VI maxi = {0, 0, 0};
while(!bfs.empty()) {
int u, par, c; VI x = bfs.front();
bfs.pop();
u = x[0], par = x[1], c = x[2];
maxi[0] = c;
sort(ALL(maxi));
// cout << u << ' ' << par << ' ' << c << '\n';
// if (dbg++ > 10) exit(0);
for (auto [v, w] : paths[u]) {
if (par == v) continue;
bfs.push({v, u, c+w});
}
}
return {maxi[1], maxi[2]};
}
PII get() {
PII res = {1e9, 1e9};
while(!stk.empty()) {
int x = stk.top(); stk.pop();
PII tmp = calc(x);
if (tmp.ff > tmp.ss) swap(tmp.ff, tmp.ss);
res = min(res, tmp);
}
return res;
}
int travelTime(int _N, int _M, int _L, int _A[], int _B[], int _T[]) {
n = _N;
m = _M;
L = _L;
vector <PII> pts;
for (int i = 0; i < _M; i++) {
// paths[_A[i]].pb({_B[i], _T[i]});
// paths[_B[i]].pb({_A[i], _T[i]});
paths[_A[i]+1].pb({_B[i]+1, _T[i]});
paths[_B[i]+1].pb({_A[i]+1, _T[i]});
}
for (int i = 1; i <= n; i++) {
if (vis[i]) continue;
dfs(i);
PII tmp = get();
if (tmp.ff > tmp.ss) swap(tmp.ff, tmp.ss);
pts.pb(tmp);
// // cout << i-1 << ": " << point[0]-1 << ' ' << point[1] << ' ' << point[2] << '\n';
// // cout << i << ": " << point[0] << ' ' << point[1] << ' ' << point[2] << '\n';
}
sort(ALL(pts), [&](auto&a, auto&b) {
return max(a.ff, a.ss) < max(b.ff, b.ss);
});
sort(ALL(pts));
n = pts.size();
for (int i = n-2; i >= 0; i--) {
PII&a = pts[n-1];
PII&b = pts[i];
if (a.ff > a.ss) swap(a.ff, a.ss);
if (b.ff > b.ss) swap(b.ff, b.ss);
VI x = {a.ff, a.ss, b.ss+L};
VI y = {b.ff, b.ss, a.ss+L};
sort(ALL(x));
sort(ALL(y));
a = {x[1], x[2]};
b = {y[1], y[2]};
a = max(a, b);
}
return pts[n-1].ff+pts[n-1].ss;
return 0;
}
# | 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... |