#include "towns.h"
#include "bits/stdc++.h"
using namespace std;
map <pair <int, int>, int> mp;
int distance(int i, int j) {
if(i > j) swap(i, j);
if(mp.find(make_pair(i, j)) != mp.end()) return mp[make_pair(i, j)];
return mp[make_pair(i, j)] = getDistance(i, j);
}
int hubDistance(int N, int sub) {
int opt = 0;
int dist = 0;
for(int i = 1; i < N; i++) {
int d = distance(0, i);
if(d > dist) {
dist = d;
opt = i;
}
}
int P = 0, Q = opt;
dist = 0;
for(int i = 0; i < N; i++) {
if(i == opt) continue;
int d = distance(opt, i);
if(d > dist) {
dist = d;
P = i;
}
}
int R = 0;
for(int i = 0; i < N; i++) {
int lca_dist = (distance(i, P) + distance(i, Q) - distance(P, Q)) / 2;
int p_dist = distance(i, P) - lca_dist;
int q_dist = distance(i, Q) - lca_dist;
if(p_dist < q_dist) {
R = max(R, p_dist);
} else {
R = max(R, q_dist);
}
}
return distance(P, Q) - R;
}
Compilation message
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:12:28: warning: unused parameter 'sub' [-Wunused-parameter]
int hubDistance(int N, int sub) {
^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
17 ms |
896 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
18 ms |
896 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
16 ms |
896 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
20 ms |
1024 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
16 ms |
768 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
16 ms |
768 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |