# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
942390 | emad234 | Dreaming (IOI13_dreaming) | C++17 | 0 ms | 0 KiB |
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 F first
#define S second
#define pii pair<int,int>
const int mod = 1e9 + 7;
const int mxN = 2e5 + 5;
using namespace std;
vector<vector<pii>>v;
vector<int>nums;
int mx[mxN];
bool vis[mxN];
int path;
int dpD[mxN],dpU[mxN];
void dfs(int u){
vis[u] = 1;
for(auto x : v[u]){
if(vis[x.F]) continue;
dfs(x.F);
dpD[u] = max(dpD[x.F] + x.S,dpD[u]);
}
}
void hfs(int u,int par){
multiset<int>s;
for(auto x : v[u]){
if(x.F == par) continue;
s.insert(dpD[x.F] + x.S);
}
for(auto x : v[u]){
if(x.F == par) continue;
dpU[x.F] = dpU[u] + x.S;
s.erase(s.find(dpD[x.F] + x.S));
if(s.size()) dpU[x.F] = max(dpU[x.F], *(s.rbegin()) + x.S);
s.insert(dpD[x.F] + x.S);
}
for(auto x : v[u]) if(x.F != par) hfs(x.F,u);
path = min(path,max(dpD[u],dpU[u]));
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
v.resize(N + 2);
for(int i = 0;i < M;i++){
v[A[i]].push_back({B[i],T[i]});
v[B[i]].push_back({A[i],T[i]});
}
int mx = 0;
for(int i = 0;i < N;i++){
if(!vis[i]){
path = 2e9;
dfs(i);
hfs(i,-1);
if(path > mx){
if(i)
nums.push_back(mx + L);
mx = path;
} else nums.push_back(path + L);
}
}
sort(nums.begin(),nums.end());
// for(auto x : nums) cout<<x<<' ';
// cout<<'\n';
int ans = mx;
for(int i = 0;i < nums.size();i++){
int num = 0;
if(i == nums.size() - 1){
if(i != 0){
num = nums[i] + nums[i - 1];
}
} else num = nums[i] + nums[nums.size() - 1];
num = max(num, nums[i] + mx);
ans = max(num,ans);
}
return ans;
}
// #define MAX_N 100000
// static int A[MAX_N];
// static int B[MAX_N];
// static int T[MAX_N];
// int main() {
// int N, M, L, i;
// int res;
// cin >>N>>M>>L;
// for (i = 0; i < M; i++)
// cin>>A[i]>>B[i]>>T[i];
// int answer = travelTime(N, M, L, A, B, T);
// printf("%d\n", answer);
// return 0;
// }