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;
bool viz[100005];
vector<pair<int,int>> g[100005];
int t[100005];
int dist[100005];
vector<int> cc;
void dfs(int nod)
{
cc.push_back(nod);
viz[nod] = true;
for (auto vecin : g[nod])
{
if (!viz[vecin.first])
{
t[vecin.first] = nod;
dist[vecin.first] = dist[nod] + vecin.second;
dfs(vecin.first);
}
}
}
int travelTime(int N, int M, int L, int A[], int B[], int T[])
{
for (int i = 0; i < M; i++)
{
g[A[i]].push_back({B[i],T[i]});
g[B[i]].push_back({A[i],T[i]});
}
vector<int> w;
for (int i = 0; i < N; i++)
{
if (!viz[i])
{
cc.clear();
dfs(i);
int mx = 0,cn = i;
for (auto it : cc)
{
if (dist[it] > mx)
mx = dist[it],cn = it;
}
for (auto it : cc)
dist[it] = 0,viz[it] = false,t[it] = 0;
cc.clear();
dfs(cn);
int rt = cn;
mx = 0,cn = rt;
for (auto it : cc)
{
if (dist[it] > mx)
mx = dist[it],cn = it;
}
vector<int> diam;
while (cn != rt)
diam.push_back(cn),cn = t[cn];
diam.push_back(cn);
int ans = 1e9;
for (auto it : diam)
ans = min(ans,max(dist[it],mx - dist[it]));
w.push_back(ans);
}
}
if (w.size() == 1)
return w[0];
else
{
sort(w.begin(),w.end());
int ans = w[w.size() - 1] + w[w.size() - 2] + L;
if (w.size() >= 3)
ans = max(ans,w[w.size() - 2] + w[w.size() - 3] + L + L);
return ans;
}
}
# | 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... |