#include "towns.h"
#include <bits/stdc++.h>
using namespace std;
int dist[111][111],cnt[111];
int d(int u, int v){
if (u==v)
return 0;
if (u>v)
swap(u,v);
if (dist[u][v])
return dist[u][v];
return dist[u][v]=getDistance(u,v);
}
int hubDistance(int N, int sub){
memset(dist,0,sizeof(dist));
pair <int, int> mx={0,-1};
for (int i=0;i<N;i++)
mx=max(mx,{d(0,i),i});
int u=mx.second;
mx={0,-1};
for (int i=0;i<N;i++)
mx=max(mx,{d(u,i),i});
int v=mx.second;
int R=1e9,sz[2]={0,0};
for (int i=0;i<N;i++)
R=min(R,abs(d(i,u)-d(i,v)));
vector <int> a,b,tmp;
for (int i=0;i<N;i++){
int val=d(i,u)-d(i,v);
if (val<-R)
sz[0]++;
if (val==-R)
a.push_back(i);
if (val==R)
b.push_back(i);
if (val>R)
sz[1]++;
}
if (!R)
b.clear();
R=(R+d(u,v))/2;
if (max(sz[0],sz[1])>N/2)
return -R;
if (min(sz[1]+b.size(),sz[0]+a.size())>N/2)
return -R;
if (sub==4||sub<3)
return (max(a.size(),b.size())>N/2?-R:R);
if (max(a.size(),sz[1]+b.size())<=N/2)
return R;
if (max(b.size(),sz[0]+a.size())<=N/2)
return R;
if (b.size()+sz[1]<=N/2){
fill(cnt,cnt+N,1);
tmp.push_back(a.back());
a.pop_back();
for (int i:a){
int c=1;
for (int j:tmp){
int x=(d(i,u)+d(i,v)-d(u,v))/2,y=(d(j,u)+d(j,v)-d(u,v))/2;
if (d(i,j)<x+y){
cnt[j]++;
if (cnt[j]>N/2)
return -R;
c=0;
break;
}
}
if (c)
tmp.push_back(i);
}
tmp.clear();
}
else{
fill(cnt,cnt+N,1);
tmp.push_back(b.back());
b.pop_back();
for (int i:b){
int c=1;
for (int j:tmp){
int x=(d(i,u)+d(i,v)-d(u,v))/2,y=(d(j,u)+d(j,v)-d(u,v))/2;
if (d(i,j)<x+y){
cnt[j]++;
if (cnt[j]>N/2)
return -R;
c=0;
break;
}
}
if (c)
tmp.push_back(i);
}
tmp.clear();
}
return R;
}
Compilation message
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:44:43: warning: comparison of integer expressions of different signedness: 'const long unsigned int' and 'int' [-Wsign-compare]
44 | if (min(sz[1]+b.size(),sz[0]+a.size())>N/2)
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
towns.cpp:47:39: warning: comparison of integer expressions of different signedness: 'const long unsigned int' and 'int' [-Wsign-compare]
47 | return (max(a.size(),b.size())>N/2?-R:R);
| ~~~~~~~~~~~~~~~~~~~~~~^~~~
towns.cpp:48:37: warning: comparison of integer expressions of different signedness: 'const long unsigned int' and 'int' [-Wsign-compare]
48 | if (max(a.size(),sz[1]+b.size())<=N/2)
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
towns.cpp:50:37: warning: comparison of integer expressions of different signedness: 'const long unsigned int' and 'int' [-Wsign-compare]
50 | if (max(b.size(),sz[0]+a.size())<=N/2)
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
towns.cpp:52:23: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
52 | if (b.size()+sz[1]<=N/2){
| ~~~~~~~~~~~~~~^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
348 KB |
Output is correct |
2 |
Correct |
9 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
12 ms |
532 KB |
Output is correct |
5 |
Correct |
12 ms |
540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
348 KB |
Output is correct |
2 |
Correct |
9 ms |
348 KB |
Output is correct |
3 |
Correct |
12 ms |
528 KB |
Output is correct |
4 |
Correct |
12 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
344 KB |
Output is correct |
2 |
Correct |
12 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
13 ms |
536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
492 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |