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 <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define sz(x) (int)x.size()
#define ALL(v) v.begin(),v.end()
#define MASK(k) (1LL << (k))
#define BIT(x, i) (((x) >> (i)) & 1)
#define oo (ll)1e18 + 10
#define INF (ll)1e9
#define MOD (ll)(1e9 + 7)
#include "dreaming.h"
using namespace std;
template<class T1, class T2>
bool maximize(T1 &a, T2 b){if(a < b){a = b; return true;} return false;}
template<class T1, class T2>
bool minimize(T1 &a, T2 b){if(a > b){a = b; return true;} return false;}
template<class T1, class T2>
void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;}
template<class T1, class T2>
void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;}
template<class T>
void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());}
const int MAX = 2e5 + 10;
int n, m, l;
vector<pair<int, int>> adj[MAX];
int dp_in[MAX], dp_out[MAX];
bool visited[MAX];
void dfs1(vector<int> &save, int u, int p = -1){
visited[u] = true;
save.push_back(u);
for(auto o: adj[u]){
int v = o.fi, c = o.se;
if(v == p) continue;
dfs1(save, v, u);
maximize(dp_in[u], dp_in[v] + c);
}
}
void dfs2(int u, int p = -1){
int first = dp_out[u], second = 0;
for(auto o: adj[u]){
int v = o.fi, c = o.se;
if(v == p) continue;
if(dp_in[v] + c > first){
second = first;
first = dp_in[v] + c;
}
else maximize(second, dp_in[v] + c);
}
for(auto o: adj[u]){
int v = o.fi, c = o.se;
if(v == p) continue;
if(dp_in[v] + c == first)
dp_out[v] = second + c;
else
dp_out[v] = first + c;
dfs2(v, u);
}
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]){
n = N;
m = M;
l = L;
for(int i = 0; i < m; i++){
adj[A[i]].push_back({B[i], T[i]});
adj[B[i]].push_back({A[i], T[i]});
}
vector<int> ord;
for(int i = 0; i < n; i++) if(!visited[i]){
vector<int> save;
dfs1(save, i);
dfs2(i);
int mi = INF * 2;
for(int x: save) minimize(mi, max(dp_in[x], dp_out[x]));
ord.push_back(mi);
}
sort(ALL(ord), greater<int>());
int ans = 0;
if(m + 1 == n) ans = ord[0];
else{
maximize(ans, ord[0] + ord[1] + l);
if(sz(ord) > 2) maximize(ans, ord[1] + ord[2] + 2 * l);
}
return ans;
}
//void solve(){
//
//}
//
//int main(){
// ios_base::sync_with_stdio(0); cin.tie(0);
//// freopen("bdbang.inp","r",stdin);
//// freopen("bdbang.out","w",stdout);
//
// cout << travelTime(12, 8, 2, {0,8,2,5,5,1,1,10}, {8,2,7,11,1,3,9,6}, {4,2,4,3,7,1,5,3});
//
// return 0;
//}
# | 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... |