#include "towns.h"
#include <bits/stdc++.h>
using namespace std;
int dd[200][200], d1[200], d2[200];
mt19937 rng(69420);
int GetDistance(int u, int v){
if (u==v) return 0;
if (dd[u][v]) return dd[u][v];
int tmp=getDistance(u, v);
dd[u][v]=tmp; dd[v][u]=tmp;
return tmp;
}
int hubDistance(int N, int sub) {
memset(dd, 0, sizeof dd);
memset(d1, 0, sizeof d1);
memset(d2, 0, sizeof d2);
int u=0, dis=0;
for (int i=1; i<N; ++i){
int d=GetDistance(0, i);
if (d>dis){
dis=d;
u=i;
}
}
dis=0;
int v=u;
for (int i=0; i<N; ++i) if (i!=u){
int d=GetDistance(u, i);
d1[i]=d;
if (d>dis){
dis=d;
v=i;
}
}
int ans=1e9;
vector<pair<int, int>> vv;
for (int i=0; i<N; ++i) if (i!=u && i!=v){
d2[i]=GetDistance(v, i);
int common=(d1[i]+d2[i]-dis)/2;
int dl=d1[i]-common, dr=d2[i]-common;
ans=min(ans, max(dl, dr));
vv.emplace_back(dl, dr);
}
if (sub<=2) return ans;
d1[u]=d2[v]=0;
d1[v]=d2[u]=dis;
sort(vv.begin(), vv.end());
vv.resize(unique(vv.begin(), vv.end())-vv.begin());
int cnt=0;
for (auto &ii:vv) if (max(ii.first, ii.second)==ans) ++cnt;
int mx=N/2;
for (auto &ii:vv) if (max(ii.first, ii.second)==ans){
int szl=0, szr=0;
vector<pair<int, int>> child;
for (int i=0; i<N; ++i){
int common=(d1[i]+d2[i]-dis)/2;
int dl=d1[i]-common, dr=d2[i]-common;
if (dl<ii.first) ++szl;
else if (dl>ii.first) ++szr;
else child.emplace_back(i, 1);
}
if (szl>mx || szr>mx) continue;
vector<pair<int, int>> dead;
while ((int)child.size()>=2){
if ((int)child.size()&1){
dead.push_back(child.back());
child.pop_back();
}
vector<pair<int, int>> new_child;
for (int i=0; i<(int)child.size(); i+=2){
int x=child[i].first, y=child[i+1].first;
if ((d1[x]+d1[y]-ii.first*2-GetDistance(x, y))){
new_child.emplace_back(x, child[i].second+child[i+1].second);
}else{
dead.push_back(child[i]);
dead.push_back(child[i+1]);
}
}
new_child.swap(child);
}
if (child.size()){
int x=child[0].first, sz=child[0].second;
for (auto &i:dead) if ((d1[x]+d1[i.first]-ii.first*2-GetDistance(x, i.first))) sz+=i.second;
if (sz<=mx) return ans;
}else return ans;
}
return -ans;
}
Compilation message
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:63:31: warning: unused variable 'dr' [-Wunused-variable]
63 | int dl=d1[i]-common, dr=d2[i]-common;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
1116 KB |
Output is correct |
2 |
Correct |
8 ms |
972 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
15 ms |
1148 KB |
Output is correct |
5 |
Correct |
9 ms |
1112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
1112 KB |
Output is correct |
2 |
Correct |
10 ms |
860 KB |
Output is correct |
3 |
Correct |
10 ms |
1112 KB |
Output is correct |
4 |
Correct |
10 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
856 KB |
Output is correct |
2 |
Incorrect |
10 ms |
1188 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
1112 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
600 KB |
Output is correct |
2 |
Incorrect |
8 ms |
1156 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
600 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |