#include "dreaming.h"
//#include "grader.cpp"
#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];
// dfs for calculating dp. (dp[i] - maximum depth)
void dfs(int u, int p){
used[u] = true;
dp[u] = 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);
}
}
//dfs for rerooting
int mn;
void dfs2(int u, int p){
for(auto X : edges[u]){
int v = X.first;
int cost = X.second;
if(v == p)continue;
int tmp = dp[v];
dp[v] = max(dp[v], dp[u] + cost);
mn = min(mn, dp[v]);
dfs2(v, u);
dp[v] = tmp;
}
}
//calculate best center for the tree
int calculate_center(int u){
dfs(u, u);
mn = dp[u];
dfs2(u, u);
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 |
60 ms |
12280 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
60 ms |
12280 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
60 ms |
12280 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
6292 KB |
Output is correct |
2 |
Incorrect |
29 ms |
6400 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
60 ms |
12280 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
60 ms |
12280 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |