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 forn(i,n) for(int i=0;i<n;i++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(),v.rend()
#define pb push_back
#define sz(a) (int)a.size()
const int N = 1e5 + 10;
vector<pair<int,int>> adj[N];
vector<int> component;
int farthest;
bool vis[N];
map<int,int> bfs(int u)
{
map<int,int> dist;
dist[u] = 0;
vector<bool> pushed(N, false);
pushed[u] = true;
queue<int> q;
q.push(u);
int mx = 0;
while(!q.empty())
{
int i = q.front();
vis[i] = true;
component.pb(i);
q.pop();
for(auto x : adj[i])
{
if(!pushed[x.first])
{
dist[x.first] = dist[i] + x.second;
q.push(x.first);
pushed[x.first] = true;
if(mx < dist[x.first]){
mx = dist[x.first];
farthest = x.first;
}
}
}
}
return dist;
}
int travelTime(int n, int m, int l, int a[], int b[], int t[]){
for(int i = 0;i < n; ++i){
adj[i].clear();
vis[i] = false;
}
for(int i = 0;i < m; ++i){
adj[a[i]].pb({b[i], t[i]});
adj[b[i]].pb({a[i], t[i]});
}
vector<pair<int,int>> v;
for(int i = 0;i < n; ++i)
{
if(vis[i])continue;
farthest = i;
bfs(i);
map<int, int> A = bfs(farthest);
component.clear();
map<int, int> B = bfs(farthest);
int ans = farthest, mx = max(B[ans], A[ans]);
for(auto x: component){
if(max(B[x], A[x]) < mx){
mx = max(B[x], A[x]);
ans = x;
}
}
v.pb({mx, ans});
}
sort(rall(v));
for(int i = 1;i < sz(v); ++i){
adj[v[i].second].pb({v[0].second, l});
adj[v[0].second].pb({v[i].second, l});
}
farthest = 0;
bfs(0);
map<int, int> A = bfs(farthest);
map<int, int> B = bfs(farthest);
int ans = 0;
for(int i = 0;i < n; ++i){
ans = max({ans, A[i], B[i]});
}
return ans;
}
/*
void solve()
{
int n, m, l;
cin >> n >> m >> l;
int a[m], b[m], t[m];
forn(i, m)cin >> a[i] >> b[i] >> t[i];
cout << travelTime(n, m, l, a, b, t);
}
int32_t main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int t = 1;
//cin >> t;
while(t--)
{
solve();
}
}
*/
# | 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... |