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 ll long long
#define ff first
#define ss second
#define pb push_back
#define mk make_pair
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define MOD 1000000007
#define MOD1 1000000009
#define sqr(x) sqr((x)*(x))
void debug_out() { cerr << endl; }
template<typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << ' ' << H; debug_out(T...); }
#ifdef BE_DEBUG
#define debug(...) cerr << "\033[1;31m(" << #__VA_ARGS__ << "):\033[0m", debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif
const int BB = 100005;
vector<pair<int,int> > edge[BB];
bool visited[BB];
vector<int> v;
int a[BB],b[BB]; //
int tmp;
multiset<int> s;
multiset<int> :: iterator it;
vector<int> gold;
int mn;
int ans;
void dfs(int node) {
//cout << node << ' ';
visited[node] = 1;
v.pb(node);
int tmp_mx = 0;
for(auto x: edge[node]) {
if(visited[x.ff] == 1) continue;
a[x.ff] = x.ss;
dfs(x.ff);
b[node] = max(b[node], a[x.ff]);
if(node == tmp) {
s.insert(-(b[x.ff] + x.ss));
}
ans = max(ans, tmp_mx + a[x.ff]);
tmp_mx = max(tmp_mx,a[x.ff]);
}
a[node] += b[node];
}
void go(int node, int cost) {
visited[node] = 1;
if(s.size() != 0) {
it = s.begin();
mn = min(mn, max(b[node],cost - *it));
}
else {
mn = min(mn, max(b[node], cost));
}
for(auto x: edge[node]) {
if(visited[x.ff] == 1) continue;
if(tmp == node) {
it = s.lower_bound(-a[x.ff]);
s.erase(it);
}
go(x.ff, x.ss + cost);
s.insert(-a[x.ff]);
}
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
for(int i = 0; i < M; i ++) {
edge[A[i]].pb({B[i], T[i]});
edge[B[i]].pb({A[i], T[i]});
}
for(int i = 0; i < N; i ++) {
if(visited[i] == 1) continue;
s.clear();
v.clear();
tmp = i;
dfs(i);
for(auto x: v) {
visited[x] = 0;
}
mn = 1e9;
go(i,0);
gold.pb(mn);
}
/*
for(int i = 0; i < N; i ++) {
cout << a[i] << ' ';
}
cout << '\n';
for(int i = 0; i < N; i ++) {
cout << b[i] << ' ';
}
cout << '\n';
*/
sort(gold.begin(), gold.end());
if(gold.size() == 1) {
return max(ans,gold[gold.size() - 1]);
}
int q = gold.size() - 1;
if(gold.size() == 2) {
return max(ans,gold[q] + gold[q - 1] + L);
}
return max(ans, max(gold[q] + gold[q - 1] + L, gold[q - 1] + gold[q - 2] + L + L));
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... |