#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
int n;
const int N = 1e5+5;
bool used[N];
vector<pair<int, int>> edges[N];
// dfs for calculating maximum depth. (V[i] - distance from the root)
int mx, mxind;
vector<int> vertex;
void dfs(int u, int p, int dist, vector<int> &V, bool flag = 0){
used[u] = true;
if(flag)vertex.push_back(u);
V[u] = dist;
if(dist > mx){
mx = dist;
mxind = u;
}
for(auto X : edges[u]){
int v = X.first;
int cost = X.second;
if(v == p)continue;
dfs(v, u, dist + cost, V, flag);
}
}
//calculate best center for the tree
int max_diameter;
int calculate_center(int u){
vertex.clear();
mx = -1;
vector<int> no_need(n);
dfs(u, u, 0, no_need, 1);
mx = -1;
int ind1 = mxind;
vector<int> a(n);
dfs(ind1, ind1, 0, a);
int ind2 = mxind;
vector<int> b(n);
dfs(ind2, ind2, 0, b);
int mn = INT_MAX;
for(auto x : vertex){
mn = min(mn, max(a[x], b[x]));
max_diameter = max(max_diameter, a[x]+b[x]);
}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() == 1){
return max_diameter;
}else {
return max(max(max_diameter, v[0]+v[1]+L), ((v.size() > 2)? v[1]+v[2]+L*2 : 0));
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
15348 KB |
Output is correct |
2 |
Correct |
75 ms |
15348 KB |
Output is correct |
3 |
Correct |
46 ms |
10996 KB |
Output is correct |
4 |
Correct |
10 ms |
4480 KB |
Output is correct |
5 |
Correct |
9 ms |
3712 KB |
Output is correct |
6 |
Correct |
17 ms |
5504 KB |
Output is correct |
7 |
Correct |
2 ms |
2688 KB |
Output is correct |
8 |
Correct |
35 ms |
7064 KB |
Output is correct |
9 |
Correct |
43 ms |
8824 KB |
Output is correct |
10 |
Correct |
2 ms |
2688 KB |
Output is correct |
11 |
Correct |
74 ms |
10616 KB |
Output is correct |
12 |
Correct |
84 ms |
12912 KB |
Output is correct |
13 |
Correct |
3 ms |
2816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
15348 KB |
Output is correct |
2 |
Correct |
75 ms |
15348 KB |
Output is correct |
3 |
Correct |
46 ms |
10996 KB |
Output is correct |
4 |
Correct |
10 ms |
4480 KB |
Output is correct |
5 |
Correct |
9 ms |
3712 KB |
Output is correct |
6 |
Correct |
17 ms |
5504 KB |
Output is correct |
7 |
Correct |
2 ms |
2688 KB |
Output is correct |
8 |
Correct |
35 ms |
7064 KB |
Output is correct |
9 |
Correct |
43 ms |
8824 KB |
Output is correct |
10 |
Correct |
2 ms |
2688 KB |
Output is correct |
11 |
Correct |
74 ms |
10616 KB |
Output is correct |
12 |
Correct |
84 ms |
12912 KB |
Output is correct |
13 |
Correct |
3 ms |
2816 KB |
Output is correct |
14 |
Incorrect |
2 ms |
2688 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
15348 KB |
Output is correct |
2 |
Correct |
75 ms |
15348 KB |
Output is correct |
3 |
Correct |
46 ms |
10996 KB |
Output is correct |
4 |
Correct |
10 ms |
4480 KB |
Output is correct |
5 |
Correct |
9 ms |
3712 KB |
Output is correct |
6 |
Correct |
17 ms |
5504 KB |
Output is correct |
7 |
Correct |
2 ms |
2688 KB |
Output is correct |
8 |
Correct |
35 ms |
7064 KB |
Output is correct |
9 |
Correct |
43 ms |
8824 KB |
Output is correct |
10 |
Correct |
2 ms |
2688 KB |
Output is correct |
11 |
Correct |
74 ms |
10616 KB |
Output is correct |
12 |
Correct |
84 ms |
12912 KB |
Output is correct |
13 |
Correct |
3 ms |
2816 KB |
Output is correct |
14 |
Incorrect |
2 ms |
2688 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1092 ms |
6816 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
15348 KB |
Output is correct |
2 |
Correct |
75 ms |
15348 KB |
Output is correct |
3 |
Correct |
46 ms |
10996 KB |
Output is correct |
4 |
Correct |
10 ms |
4480 KB |
Output is correct |
5 |
Correct |
9 ms |
3712 KB |
Output is correct |
6 |
Correct |
17 ms |
5504 KB |
Output is correct |
7 |
Correct |
2 ms |
2688 KB |
Output is correct |
8 |
Correct |
35 ms |
7064 KB |
Output is correct |
9 |
Correct |
43 ms |
8824 KB |
Output is correct |
10 |
Correct |
2 ms |
2688 KB |
Output is correct |
11 |
Correct |
74 ms |
10616 KB |
Output is correct |
12 |
Correct |
84 ms |
12912 KB |
Output is correct |
13 |
Correct |
3 ms |
2816 KB |
Output is correct |
14 |
Incorrect |
3 ms |
2688 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
15348 KB |
Output is correct |
2 |
Correct |
75 ms |
15348 KB |
Output is correct |
3 |
Correct |
46 ms |
10996 KB |
Output is correct |
4 |
Correct |
10 ms |
4480 KB |
Output is correct |
5 |
Correct |
9 ms |
3712 KB |
Output is correct |
6 |
Correct |
17 ms |
5504 KB |
Output is correct |
7 |
Correct |
2 ms |
2688 KB |
Output is correct |
8 |
Correct |
35 ms |
7064 KB |
Output is correct |
9 |
Correct |
43 ms |
8824 KB |
Output is correct |
10 |
Correct |
2 ms |
2688 KB |
Output is correct |
11 |
Correct |
74 ms |
10616 KB |
Output is correct |
12 |
Correct |
84 ms |
12912 KB |
Output is correct |
13 |
Correct |
3 ms |
2816 KB |
Output is correct |
14 |
Incorrect |
2 ms |
2688 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |