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;
#define ff first
#define ss second
#define MP make_pair
typedef pair<int, int> pii;
const int inf = 1e9 + 10;
const int mxn = 1e5 + 10;
vector<pii> adj[mxn];
int m1[mxn], m2[mxn];
void update(int &M1, int &M2, int sus) {
if(sus > M1) {
M2 = M1;
M1 = sus;
}
else if(sus > M2) {
M2 = sus;
}
}
void maxer(int &MAX, int CMP) { if(MAX < CMP) MAX = CMP; }
int max_dfs(int u, int par = -1) {
if(adj[u].size() == 1 || adj[u].size() == 0)
m1[u] = 0;
for(pii e : adj[u]) {
int v = e.ff;
int c = e.ss;
if(v == par) continue;
if(m1[v] > -1) continue;
int t = max_dfs(v, u) + c;
update(m1[u], m2[u], t);
// update(m1[u], m2[u], m2[v] + c);
}
// cout << u << ": " << m1[u] << ' ' << m2[u] << endl;
return m1[u];
}
int fix_dfs(int u, int prv = -1) {
int opt = -1;
int sus = m1[u];
int len = -1;
// cout << "fix: " << u << endl;
for(pii e : adj[u]) {
int v = e.ff;
int c = e.ss;
if(v == prv) continue;
if(m1[v] + c < m1[u]) continue;
int NEW = max(m1[v], m2[u] + c);
// cout << v << ' ' << NEW << endl;
if(NEW < sus) {
sus = NEW;
opt = v;
len = c;
}
}
// cout << "opt = " << ' ' << opt << endl;
if(opt == -1) return u;
m1[u] = 0;
m2[u] = -inf;
for(pii e : adj[u]) {
int v = e.ff;
int c = e.ss;
// if(v == prv) continue;
if(v == opt) continue;
update(m1[u], m2[u], m1[v] + c);
}
// cout << m1[u] << ' ' << m2[u] << endl;
update(m1[opt], m2[opt], m1[u] + len);
// update(m1[opt], m2[opt], M2);
return fix_dfs(opt, u);
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
for(int i = 0; i < M; i++) {
int u = A[i];
int v = B[i];
int c = T[i];
// cout << u << ' ' << v << ": " << c << endl;
adj[u].push_back(MP(v, c));
adj[v].push_back(MP(u, c));
}
for(int i = 0; i < N; i++) {
m1[i] = m2[i] = -1;
}
vector<int> upt;
for(int i = 0; i < N; i++) {
if(m1[i] > -1) continue;
// cout << "root = " << i << endl;
max_dfs(i);
// cout << "------\n";
upt.push_back(i);
}
int ans = 0;
vector<int> roots;
for(int x : upt) {
int y = fix_dfs(x);
roots.push_back(y);
// cout << x << ": " << y << endl;
maxer(ans, m1[y] + m2[y]);
}
if(roots.size() == 1)
return ans;
pii M1 = MP(-inf, -1);
pii M2 = MP(-inf, -1);
pii M3 = MP(-inf, -1);
for(int u : roots) {
pii cur = MP(m1[u], u);
if(cur > M1) {
M3 = M2;
M2 = M1;
M1 = cur;
}
else if(cur > M2) {
M3 = M2;
M2 = cur;
}
else if(cur > M3) {
M3 = cur;
}
}
maxer(ans, M1.ff + M2.ff + L);
maxer(ans, M2.ff + M3.ff + 2 * L);
return ans;
}
# | 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... |