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;
typedef pair<int, int> pii;
const int N = 1e5+5;
vector<pii> adj[N];
vector<int> members;
bool vis[N];
void dfs0(int nd, int pr){
vis[nd] = 1;
members.push_back(nd);
for (auto [to, w]: adj[nd])
if (to != pr)
dfs0(to, nd);
}
int max_weight, endpoint, dp[N];
void dfs(int nd, int pr, int weight){
if (weight > max_weight){
max_weight = weight;
endpoint = nd;
}
dp[nd] = max(dp[nd], weight);
for (auto [to, w]: adj[nd])
if (to != pr)
dfs(to, nd, weight+w);
}
int travelTime(int n, int m, int L, int A[], int B[], int T[]) {
for (int i = 0; i < m; i++){
int u = A[i], v = B[i], w = T[i];
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
vector<int> roots;
for (int i = 0; i < n; i++)
if (!vis[i]){
dfs0(i, -1);
roots.push_back(i);
}
vector<int> w;
int D = 0;
for (auto root: roots){
members.clear();
dfs0(root, -1);
max_weight = 0;
dfs(root, -1, 0);
int a = endpoint;
max_weight = 0;
dfs(endpoint, -1, 0);
int b = endpoint;
D = max(D, max_weight);
dfs(a, -1, 0);
dfs(b, -1, 0);
int value = INT_MAX;
for (auto member: members)
if (value > dp[member])
value = dp[member];
w.push_back(value);
}
sort(w.rbegin(), w.rend());
int c = roots.size();
int answer = D;
if (c >= 2)
answer = max(answer, w[0] + w[1] + L);
if (c >= 3)
answer = max(answer, w[1] + w[2] + 2*L);
return answer;
}
# | 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... |