#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
int n;
const int N = 1e5+5;
bool used[N];
int depth[N];
int dp[N];
vector<pair<int, int>> edges[N];
vector<int> a[N];
// dfs for calculating dp. (dp[i] - maximum depth)
void dfs(int u, int p){
used[u] = true;
dp[u] = 0;
a[u].push_back(0);a[u].push_back(0);
for(auto X : edges[u]){
int v = X.first;
int cost = X.second;
if(v == p)continue;
dfs(v, u);
dp[u] = max(dp[u], dp[v]+cost);
a[u].push_back(dp[v]+cost);
}
sort(a[u].begin(), a[u].end());
reverse(a[u].begin(), a[u].end());
}
//dfs for rerooting
int mn;
void dfs2(int u, int p, int T){
int dpu = dp[u];
for(auto X : edges[u]){
int v = X.first;
int cost = X.second;
if(v == p)continue;
int tmp = dp[v];
if(dp[v]+cost == a[u][0])dp[u] = a[u][1];
if(u != p)dp[u] = max(dp[u], dp[p] + T);
dp[v] = max(dp[v], dp[u] + cost);
mn = min(mn, dp[v]);
dfs2(v, u, cost);
dp[v] = tmp;
dp[u] = dpu;
}
}
//calculate best center for the tree
int calculate_center(int u){
dfs(u, u);
mn = dp[u];
dfs2(u, u ,0);
return mn;
}
int travelTime(int _N, int M, int L, int A[], int B[], int T[]) {
n = _N;
for(int i=0;i<M;i++){
edges[A[i]].push_back({B[i], T[i]});
edges[B[i]].push_back({A[i], T[i]});
}
vector<int> v;
for(int i=0;i<n;i++){
if(!used[i]){
v.push_back(calculate_center(i));
}
}v.push_back(0);
sort(v.begin(), v.end());
reverse(v.begin(), v.end());
if(v.size() == 0){
return 0;
}
else if(v.size() == 1){
return a[0][0] + a[0][1];
}
else {
int m = 0;
for(int i=0;i<n;i++){
m = max(m, a[i][0]);
}
return max(max(v[0]+v[1]+L, v[1]+v[2]+L*2), m);
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
99 ms |
17176 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
99 ms |
17176 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
99 ms |
17176 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
10996 KB |
Output is correct |
2 |
Correct |
52 ms |
10996 KB |
Output is correct |
3 |
Correct |
51 ms |
11380 KB |
Output is correct |
4 |
Correct |
48 ms |
11380 KB |
Output is correct |
5 |
Correct |
49 ms |
11424 KB |
Output is correct |
6 |
Correct |
52 ms |
12020 KB |
Output is correct |
7 |
Correct |
52 ms |
11764 KB |
Output is correct |
8 |
Correct |
47 ms |
11268 KB |
Output is correct |
9 |
Correct |
49 ms |
11252 KB |
Output is correct |
10 |
Correct |
52 ms |
11636 KB |
Output is correct |
11 |
Correct |
4 ms |
4992 KB |
Output is correct |
12 |
Correct |
21 ms |
9080 KB |
Output is correct |
13 |
Correct |
21 ms |
9208 KB |
Output is correct |
14 |
Correct |
20 ms |
9080 KB |
Output is correct |
15 |
Correct |
21 ms |
9080 KB |
Output is correct |
16 |
Correct |
21 ms |
9080 KB |
Output is correct |
17 |
Correct |
23 ms |
9080 KB |
Output is correct |
18 |
Correct |
21 ms |
9072 KB |
Output is correct |
19 |
Correct |
20 ms |
9080 KB |
Output is correct |
20 |
Incorrect |
4 ms |
4992 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
99 ms |
17176 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
99 ms |
17176 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |