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>
#include "dreaming.h"
using namespace std;
using ll = long long;
using ld = long double;
const int mxN = 1e5+10;
vector<array<int, 2>> adj[mxN];
bool vis[mxN];
ll ans = 0, mx_1[mxN], mx_2[mxN];
ll dfs(int node, int p) {
ll mx1 = 0, mx2 = 0;
vis[node] = true;
for(auto [it, w] : adj[node]) {
if(it == p) continue;
mx2 = max(mx2, dfs(it, node)+w);
if(mx2 > mx1) swap(mx1, mx2);
}
ans = max(ans, mx2+mx1);
mx_1[node] = mx1;
mx_2[node] = mx2;
return mx1;
}
ll dfs1(int node, int p) {
ll mn = mx_1[node];
vis[node] = true;
for(auto [it, w] : adj[node]) {
if(it == p) continue;
if(mx_1[node]-w == mx_1[it]) {
mx_2[it] = max(mx_2[it], mx_2[node]+w);
if(mx_2[it] > mx_1[it]) swap(mx_2[it], mx_1[it]);
}
else {
int init = mx_1[it];
mx_2[it] = max(mx_2[it], mx_1[node]+w);
if(mx_2[it] > mx_1[it]) swap(mx_2[it], mx_1[it]);
if(mx_2[node]-w != init) {
mx_2[it] = max(mx_2[it], mx_2[node]+w);
}
}
mn = min(mn, dfs1(it, node));
}
return mn;
}
int travelTime(int n, int m, int l, int a[], int b[], int t[]) {
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]});
}
for(int i = 0; i < n; i++) {
if(!vis[i]) dfs(i, -1);
}
if(m == n-1) return ans;
memset(vis, false, sizeof vis);
vector<ll> dists;
for(int i = 0; i < n; i++) {
if(!vis[i]) {
dists.push_back(dfs1(i, -1));
}
}
sort(dists.rbegin(), dists.rend());
ans = max(ans, dists[0]+dists[1]+l);
if(dists.size() >= 3) {
ans = max(ans, dists[1]+dists[2]+l+l);
}
return ans;
}
/*
int main()
{
int a[8] = {0,8,2,5,5,1,1,10};
int b[8] = {8,2,7,11,1,3,9,6};
int c[8] = {4,2,4,3,7,1,5,3};
cout << travelTime(12, 8, 2, a, b, c);
}*/
Compilation message (stderr)
dreaming.cpp: In function 'll dfs(int, int)':
dreaming.cpp:16:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
16 | for(auto [it, w] : adj[node]) {
| ^
dreaming.cpp: In function 'll dfs1(int, int)':
dreaming.cpp:30:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
30 | for(auto [it, w] : adj[node]) {
| ^
# | 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... |