#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]+a[i][1]);
}
return max(max(v[0]+v[1]+L, v[1]+v[2]+L*2), m);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
101 ms |
17024 KB |
Output is correct |
2 |
Correct |
101 ms |
17912 KB |
Output is correct |
3 |
Correct |
71 ms |
15480 KB |
Output is correct |
4 |
Correct |
14 ms |
6912 KB |
Output is correct |
5 |
Correct |
13 ms |
6144 KB |
Output is correct |
6 |
Correct |
22 ms |
7936 KB |
Output is correct |
7 |
Incorrect |
5 ms |
5120 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
101 ms |
17024 KB |
Output is correct |
2 |
Correct |
101 ms |
17912 KB |
Output is correct |
3 |
Correct |
71 ms |
15480 KB |
Output is correct |
4 |
Correct |
14 ms |
6912 KB |
Output is correct |
5 |
Correct |
13 ms |
6144 KB |
Output is correct |
6 |
Correct |
22 ms |
7936 KB |
Output is correct |
7 |
Incorrect |
5 ms |
5120 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
101 ms |
17024 KB |
Output is correct |
2 |
Correct |
101 ms |
17912 KB |
Output is correct |
3 |
Correct |
71 ms |
15480 KB |
Output is correct |
4 |
Correct |
14 ms |
6912 KB |
Output is correct |
5 |
Correct |
13 ms |
6144 KB |
Output is correct |
6 |
Correct |
22 ms |
7936 KB |
Output is correct |
7 |
Incorrect |
5 ms |
5120 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
10996 KB |
Output is correct |
2 |
Correct |
62 ms |
10952 KB |
Output is correct |
3 |
Correct |
50 ms |
10976 KB |
Output is correct |
4 |
Correct |
52 ms |
10996 KB |
Output is correct |
5 |
Correct |
65 ms |
10996 KB |
Output is correct |
6 |
Correct |
68 ms |
11636 KB |
Output is correct |
7 |
Correct |
55 ms |
11380 KB |
Output is correct |
8 |
Correct |
50 ms |
10876 KB |
Output is correct |
9 |
Correct |
49 ms |
10868 KB |
Output is correct |
10 |
Correct |
57 ms |
11256 KB |
Output is correct |
11 |
Correct |
4 ms |
4992 KB |
Output is correct |
12 |
Correct |
20 ms |
9080 KB |
Output is correct |
13 |
Correct |
21 ms |
9080 KB |
Output is correct |
14 |
Correct |
21 ms |
9080 KB |
Output is correct |
15 |
Correct |
20 ms |
9080 KB |
Output is correct |
16 |
Correct |
20 ms |
9080 KB |
Output is correct |
17 |
Correct |
20 ms |
9080 KB |
Output is correct |
18 |
Correct |
21 ms |
9080 KB |
Output is correct |
19 |
Correct |
21 ms |
9112 KB |
Output is correct |
20 |
Incorrect |
4 ms |
4992 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
101 ms |
17024 KB |
Output is correct |
2 |
Correct |
101 ms |
17912 KB |
Output is correct |
3 |
Correct |
71 ms |
15480 KB |
Output is correct |
4 |
Correct |
14 ms |
6912 KB |
Output is correct |
5 |
Correct |
13 ms |
6144 KB |
Output is correct |
6 |
Correct |
22 ms |
7936 KB |
Output is correct |
7 |
Incorrect |
5 ms |
5120 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
101 ms |
17024 KB |
Output is correct |
2 |
Correct |
101 ms |
17912 KB |
Output is correct |
3 |
Correct |
71 ms |
15480 KB |
Output is correct |
4 |
Correct |
14 ms |
6912 KB |
Output is correct |
5 |
Correct |
13 ms |
6144 KB |
Output is correct |
6 |
Correct |
22 ms |
7936 KB |
Output is correct |
7 |
Incorrect |
5 ms |
5120 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |