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;
#ifdef LOCAL
#include <debug.h>
#include "grader.c"
#endif
#define st first
#define nd second
#define pb push_back
#define pf push_front
#define _pb pop_back
#define _pf pop_front
#define lb lower_bound
#define ub upper_bound
#define mtp make_tuple
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
typedef long long ll; typedef unsigned long long ull;
typedef double db; typedef long double ldb;
typedef pair<int, int> pi; typedef pair<ll, ll> pll;
typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll;
typedef string str;
template<typename T> T gcd(T a, T b) { return (b == 0? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
#define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
#define EACH(i, x) for (auto &(i) : (x))
#define WHILE while
#define file "TEST"
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); }
/*
Tran The Bao
CTL - Da Lat
Practising for VOI23 gold medal
*/
const int oo = 1e9;
const int N = 1e5 + 1;
int n, m, l, c[N], d[N];
vpi adj[N];
void dfs(int u, int p) {
EACH(j, adj[u]) {
int v = j.st, w = j.nd;
if (v == p) continue;
dfs(v, u);
d[u] = max(d[u], d[v] + w);
}
}
void dfs1(int u, int p, int dp, int &rs) {
c[u] = 1;
rs = min(rs, max(d[u], dp));
int Max1 = 0, Max2 = 0;
EACH(j, adj[u]) {
int v = j.st, w = j.nd;
if (v == p) continue;
if (Max1 < d[v] + w) {
Max2 = Max1;
Max1 = d[v] + w;
}
else Max2 = max(Max2, d[v] + w);
}
EACH(j, adj[u]) {
int v = j.st, w = j.nd;
if (v == p) continue;
int Max = (d[v] + w == Max1? Max2 : Max1);
dfs1(v, u, max(dp, Max) + w, rs);
}
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
n = N;
m = M;
l = L;
FOR(i, 0, m - 1) {
int a = A[i], b = B[i], t = T[i];
++a;
++b;
adj[a].pb({b, t});
adj[b].pb({a, t});
}
vi a;
FOR(i, 1, N) {
if (c[i]) continue;
int rs = oo;
dfs(i, 0);
dfs1(i, 0, 0, rs);
a.pb(rs);
}
int rs = 0;
sort(all(a));
rs = max(rs, a.back());
if (sz(a) > 1) rs = max(rs, a[sz(a) - 1] + a[sz(a) - 2] + l);
if (sz(a) > 2) rs = max(rs, a[sz(a) - 2] + a[sz(a) - 3] + 2 * l);
return rs;
}
Compilation message (stderr)
dreaming.cpp: In function 'void dfs(int, int)':
dreaming.cpp:28:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
28 | #define EACH(i, x) for (auto &(i) : (x))
| ^
dreaming.cpp:43:5: note: in expansion of macro 'EACH'
43 | EACH(j, adj[u]) {
| ^~~~
dreaming.cpp: In function 'void dfs1(int, int, int, int&)':
dreaming.cpp:28:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
28 | #define EACH(i, x) for (auto &(i) : (x))
| ^
dreaming.cpp:54:5: note: in expansion of macro 'EACH'
54 | EACH(j, adj[u]) {
| ^~~~
dreaming.cpp:28:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
28 | #define EACH(i, x) for (auto &(i) : (x))
| ^
dreaming.cpp:63:5: note: in expansion of macro 'EACH'
63 | EACH(j, adj[u]) {
| ^~~~
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:26:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
26 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
dreaming.cpp:74:5: note: in expansion of macro 'FOR'
74 | FOR(i, 0, m - 1) {
| ^~~
dreaming.cpp:26:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
26 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
dreaming.cpp:82:5: note: in expansion of macro 'FOR'
82 | FOR(i, 1, N) {
| ^~~
# | 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... |