#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));
}
}
sort(v.begin(), v.end());
reverse(v.begin(), v.end());
if(v.size() == 0){
return 0;
}
else if(v.size() == 1){
return v[0];
}else {
return v[0]+v[1]+L;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
126 ms |
17016 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
126 ms |
17016 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
126 ms |
17016 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
10996 KB |
Output is correct |
2 |
Incorrect |
63 ms |
10996 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
126 ms |
17016 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
126 ms |
17016 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |