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;
using pii = pair<int, int>;
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define fr first
#define sc second
//using ll = long long;
//#define int ll
vector<int> a, b, t, dp;
vector<vector<pii>> vec;
vector<bool> f;
int dfs1(int nod, int papa)
{
f[nod] = true;
int doi = 0, diam = 0;
for(auto i : vec[nod])
if(i.fr != papa)
{
diam = max(diam, dfs1(i.fr, nod));
if(dp[nod] <= i.sc + dp[i.fr])
doi = dp[nod], dp[nod] = i.sc + dp[i.fr];
else
doi = max(doi, i.sc + dp[i.fr]);
}
return max(diam, dp[nod] + doi);
}
int dfs2(int nod, int papa, int desus = 0)
{
int ans = desus, doi = 0;
for(auto i : vec[nod])
if(i.fr != papa)
{
if(ans <= i.sc + dp[i.fr])
doi = ans, ans = i.sc + dp[i.fr];
else
doi = max(doi, i.sc + dp[i.fr]);
}
int rasp = ans;
for(auto i : vec[nod])
if(i.fr != papa)
{
if(ans == i.sc + dp[i.fr])
rasp = min(rasp, dfs2(i.fr, nod, doi + i.sc));
else
rasp = min(rasp, dfs2(i.fr, nod, ans + i.sc));
}
return rasp;
}
int travelTime(int n, int m, int l, int A[], int B[], int T[])
{
vec.resize(n);
for(int i = 0; i < m; i ++)
a.pb(A[i]);
for(int i = 0; i < m; i ++)
b.pb(B[i]);
for(int i = 0; i < m; i ++)
t.pb(T[i]);
for(int i = 0; i < m; i ++)
vec[a[i]].pb({b[i], t[i]}), vec[b[i]].pb({a[i], t[i]});
f.resize(n, false);
dp.resize(n, 0);
vector<int> vals, diams;
for(int i = 0; i < n; i ++)
if(!f[i])
diams.pb(dfs1(i, -1)), vals.pb(dfs2(i, -1));
sort(all(vals), [&](int a, int b){return a > b;});
sort(all(diams), [&](int a, int b){return a > b;});
int ans = 0;
if(vals.size() >= 2)
ans = vals[0] + vals[1] + l;
if(vals.size() >= 3)
ans = max(ans, vals[1] + vals[2] + 2 * l);
ans = max(ans, diams[0]);
return ans;
}
/*
int main()
{
int N, M, L, A[5000], B[5000], T[5000];
cin >> N >> M >> L;
for(int i = 0; i < M; i ++)
cin >> A[i];
for(int i = 0; i < M; i ++)
cin >> B[i];
for(int i = 0; i < M; i ++)
cin >> T[i];
cout << travelTime(N, M, L, A, B, T);
}
*/
# | 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... |