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>
//~ #ifndef EVAL
//~ #include "grader.cpp"
//~ #endif
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define all(v) (v).begin(),(v).end()
#define sz(v) int((v).size())
#define do_not_disturb ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
const int N = 1e5+7;
int used[N];
int n, compN, comp[N], compLowest[N];
pii compEnds[N];
vector <pii> graph[N];
void dfs(int v, int p) {
used[v] = 1;
comp[v] = compN;
for (auto to : graph[v]) {
if (to.first == p) continue;
dfs(to.first, v);
}
}
int df, vf;
int distFrom[N];
void get_furthest(int v, int p, int dist = 0) {
if (dist > df) {
df = dist;
vf = v;
}
for (auto to : graph[v]) {
if (to.first == p) continue;
get_furthest(to.first, v, dist+to.second);
}
}
void calc_dist(int v, int p = -1, int d = 0) {
distFrom[v] = max(distFrom[v], d);
for (auto to : graph[v]) {
if (to.first == p) continue;
calc_dist(to.first, v, d+to.second);
}
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
n = N;
for (int i = 0; i < M; i++) {
graph[A[i]].pb(mp(B[i], T[i]));
graph[B[i]].pb(mp(A[i], T[i]));
}
int ans = 0;
for (int i = 1; i <= n; i++) {
if (!used[i]) {
compLowest[++compN] = 2e9;
dfs(i, i);
df = vf = 0;
get_furthest(i, i);
compEnds[compN].first = vf;
int q = vf;
df = vf = 0;
get_furthest(q, q);
compEnds[compN].second = vf;
ans = max(ans, df);
}
}
for (int i = 1; i <= compN; i++) {
calc_dist(compEnds[i].first);
calc_dist(compEnds[i].second);
}
for (int i = 1; i <= n; i++) {
if (compLowest[comp[i]] > distFrom[i]) {
compLowest[comp[i]] = distFrom[i];
}
}
vector <int> tmp;
for (int i = 1; i <= compN; i++) {
tmp.pb(compLowest[i]);
}
sort(tmp.rbegin(), tmp.rend());
if (sz(tmp) > 1) {
ans = max(ans, tmp[0]+L+tmp[1]);
}
if (sz(tmp) > 2) {
ans = max(ans, tmp[1]+L+L+tmp[2]);
}
return ans;
}
//~ int main() {
//~ int n, m, l;
//~ cin >> n >> m >> l;
//~ int x[m], y[m], t[m];
//~ for (int i = 0; i < m; i++) {
//~ cin >> x[i] >> y[i] >> t[i];
//~ }
//~ cout << travelTime(n, m, l, x, y, t);
//~ }
/*
12 8 2
0 8 4
8 2 2
2 7 4
5 11 3
5 1 7
1 3 1
1 9 5
10 6 3
*/
# | 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... |