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 FOR(i, a, b) for (int i = (a); i<= (b); i++)
#define REP(n) FOR(O, 1, (n))
#define f first
#define s second
#define pb push_back
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef long long ll;
typedef vector<ll> vl;
const int MAXN = 100100;
const ll INF = 1e15;
int n, m;
ll l;
vii adj[MAXN];
ll dp[MAXN][2];
bool vis[MAXN];
ll mx, sm;
void dfs1 (int v, int p = -1) {
vis[v] = true;
for (auto pp : adj[v]) {
int u = pp.f;
ll w = pp.s;
if (u == p) continue;
dfs1(u, v);
ll cr = dp[u][0] + w;
if (cr > dp[v][1]) dp[v][1] = cr;
if (dp[v][1] > dp[v][0]) swap(dp[v][0], dp[v][1]);
}
mx = max(mx, dp[v][0]+dp[v][1]);
//cout << " v = " << v << " dp0 = " << dp[v][0] << " dp1 = " << dp[v][1] << endl;
}
void dfs2 (int v, int p = -1, ll prevW = 0) {
if (p != -1) {
ll cr = 0;
if (dp[p][0] != dp[v][0] + prevW)
cr = dp[p][0] + prevW;
else
cr = dp[p][1] + prevW;
if (cr > dp[v][1]) dp[v][1] = cr;
if (dp[v][1] > dp[v][0]) swap(dp[v][0], dp[v][1]);
}
for (auto pp : adj[v]) {
int u = pp.f;
ll w = pp.s;
if (u == p) continue;
dfs2(u, v, w);
}
sm = min(sm, dp[v][0]);
//cout << " dfs2 v = " << v << " dp0 = " << dp[v][0] << " dp1 = " << dp[v][1] << endl;
}
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) {
adj[A[i]].pb({B[i], T[i]});
adj[B[i]].pb({A[i], T[i]});
}
ll ans = 0;
vl sms;
FOR(i, 0, n-1) {
if (vis[i]) continue;
mx = 0;
dfs1 (i);
ans = max(mx, ans);
sm = INF;
dfs2 (i);
sms.pb(sm);
}
ll cur = 0;
if ((int)sms.size() > 1) {
sort(sms.begin(), sms.end());
reverse(sms.begin(), sms.end());
cur = sms[0] + sms[1] + l;
if ((int)sms.size() > 2) {
cur = max(cur, sms[1] + sms[2] + 2*l);
}
}
ans = max(ans, cur);
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... |