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"
#include "dreaming.h"
using namespace std;
const int sz = 1e5 + 5;
vector <vector <int>> aj[sz];
int f[sz], s[sz], c[sz];
bool us[sz];
int mn, cn;
void dfs1(int v, int p) {
us[v] = true;
for (auto& u : aj[v]) {
if (u[0] == p) continue;
dfs1(u[0], v);
if (f[u[0]] + u[1] > f[v]) {
s[v] = f[v];
f[v] = f[u[0]] + u[1];
c[v] = u[0];
} else {
s[v] = max(s[v], f[u[0]] + u[1]);
}
}
}
void dfs2(int v, int p) {
if (f[v] < mn) {
mn = f[v];
cn = v;
}
for (auto& u : aj[v]) {
if (u[0] == p) continue;
if (c[v] == u[0]) {
if (s[v] + u[1] > f[u[0]]) {
s[u[0]] = f[u[0]];
f[u[0]] = s[v] + u[1];
c[u[0]] = v;
} else {
s[u[0]] = max(s[u[0]], s[v] + u[1]);
}
} else {
s[u[0]] = f[u[0]];
f[u[0]] = f[v] + u[1];
c[u[0]] = v;
}
dfs2(u[0], v);
}
}
void dfs3(int v, int p) {
f[v] = s[v] = 0;
for (auto& u : aj[v]) {
if (u[0] == p) continue;
dfs3(u[0], v);
if (f[u[0]] + u[1] > f[v]) {
s[v] = f[v];
f[v] = f[u[0]] + u[1];
} else {
s[v] = max(s[v], f[u[0]] + u[1]);
}
}
// cout << v << ' ' << f[v] << ' ' << s[v] << '\n';
s[v] = f[v] + s[v];
mn = max({mn, f[v], s[v]});
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
for (int i = 0; i < M; i ++) {
aj[A[i]].push_back({B[i], T[i]});
aj[B[i]].push_back({A[i], T[i]});
}
vector <vector <int>> rs;
for (int i = 0; i < N; i ++) {
if (!us[i]) {
mn = INT_MAX;
dfs1(i, 0);
dfs2(i, 0);
rs.push_back({mn, cn});
}
}
sort(begin(rs), end(rs));
int mx = rs.back()[1];
for (int i = 0; i < (int) rs.size() - 1; i ++) {
int cur = rs[i][1];
aj[mx].push_back({cur, L});
aj[cur].push_back({mx, L});
// cout << "CONNECT " << mx << ' ' << cur << '\n';
}
mn = 0;
dfs3(0, -1);
return mn;
}
# | 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... |