#include "dreaming.h"
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
#define ull unsigned long long
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<long long, long long>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define rep(i, b) for(int i = 0; i < (b); ++i)
#define rep2(i,a,b) for(int i = a; i <= (b); ++i)
#define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c)
#define count_bits(x) __builtin_popcountll((x))
#define all(x) (x).begin(),(x).end()
#define siz(x) (int)(x).size()
#define forall(it,x) for(auto& it:(x))
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
//mt19937 mt;void random_start(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());}
//ll los(ll a, ll b) {return a + (mt() % (b-a+1));}
const int INF = 1e9+50;
const ll INF_L = 1e18+40;
const ll MOD = 1e9+7;
ll cost[100'001];
vector<pii> graph[100'001];
bool odw[100'001];
ll dp[100'001];
ll ans = 0;
ll min_dist;
void dfs_first(int v, int pop)
{
dp[v] = 0;
odw[v] = 1;
forall(it,graph[v])
{
if(it.ff != pop)
{
dfs_first(it.ff,v);
dp[v] = max(dp[v],dp[it.ff] + cost[it.ss]);
}
}
}
void dfs_reroot(int v, int pop)
{
ll old_dp = dp[v];
vl dps = {0};
forall(it,graph[v])
{
dps.pb(dp[it.ff]+cost[it.ss]);
}
sort(all(dps));
reverse(all(dps));
ans = max(ans,dps[0]);
min_dist = min(min_dist,dps[0]);
forall(it,graph[v])
{
if(it.ff != pop)
{
if(dps[v] != dp[it.ff]+cost[it.ss])
{
dp[v] = dps[0];
}
else
{
dp[v] = dps[1];
}
dfs_reroot(it.ff,v);
}
}
dp[v] = old_dp;
}
int travelTime(int n, int m, int l, int A[], int B[], int T[])
{
ll L = l;
rep(i,m)
{
graph[A[i]].pb({B[i],i});
graph[B[i]].pb({A[i],i});
cost[i] = T[i];
}
vl dists;
rep(i,n)
{
if(odw[i] == 0)
{
dfs_first(i,i);
min_dist = 1e18;
dfs_reroot(i,i);
dists.pb(min_dist);
}
}
sort(all(dists));
reverse(all(dists));
if(siz(dists) == 1) return ans;
if(siz(dists) == 2) return dists[0] + dists[1] + L;
return max(dists[0]+dists[1]+L,dists[1]+dists[2]+2*L);
}
# | 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... |