# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
744312 | 2023-05-18T11:53:40 Z | bane | Dreaming (IOI13_dreaming) | C++17 | 0 ms | 0 KB |
#include<bits/stdc++.h> #include "dreaming.h" using namespace std; const int maxN = (int)1e5 + 5; #define ll long long vector<vector<pair<ll,ll>>>adj(maxN); ll vis[maxN], dist[maxN], parent[maxN]; ll mx = 0, source = -1; ll ans = 0; void dfs(ll u, ll p = -1, ll d = 0){ vis[u] = 1; parent[u] = p; dist[u] = d; ans = max(ans, d); if (mx < d){ mx = d; source = u; } for (auto x : adj[u]){ if (x.first == p){ continue; } dfs(x.first, u, x.second + d); } } ll travelTime(int N, int M, int L, int A[], int B[], int T[]){ for (ll i = 0; i < M; i++){ //cout<<A[i]<<" "<<B[i]<<endl; adj[A[i]].push_back({B[i], T[i]}); adj[B[i]].push_back({A[i], T[i]}); } vector<pair<ll,ll>>centroids; for (ll i = 0; i < N; i++){ if (vis[i])continue; mx = -1, source = -1; dfs(i); ll S = source; mx = -1; dfs(S); ll E = source; vector<pair<ll,ll>>path; // cout<<S<<" "<<E<<endl; while(E != S){ path.push_back({E, dist[E]}); E = parent[E]; } path.push_back({E, dist[E]}); reverse(path.begin(), path.end()); ll diameter = path.back().second; ll best_node = path[0].first, best_dist = 1e18; for(ll j = 0; j < path.size(); j++){ if(max(path[j].second, diameter - path[j].second) < best_dist){ best_node = path[j].first; best_dist = max(path[j].second, diameter - path[j].second); } } centroids.push_back({best_dist, best_node}); } // for (auto j : centroids)cout<<j.first<<" "<<j.second<<endl; sort(centroids.rbegin(), centroids.rend()); if (centroids.size() > 2)ans = max(ans, L + centroids[0].first + centroids[1].first); if (centroids.size() > 3)ans = max(ans, L * 2 + centroids[1].first + centroids[2].first); return ans; }