#include "towns.h"
#include <bits/stdc++.h>
using namespace std;
int dist[111][111],C[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,cnt[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)
cnt[0]++;
if (val==-R)
a.push_back(i);
if (val==R)
b.push_back(i);
if (val>R)
cnt[1]++;
}
R=(R+d(u,v))/2;
if (max(cnt[0],cnt[1])>N/2)
return -R;
if (sub==4||sub<3)
return (max(a.size(),b.size())>N/2?-R:R);
if (min(cnt[1]+b.size(),cnt[0]+a.size())>N/2)
return -R;
if (a.size()>N/2&&b.size()+cnt[1]<=N/2){
fill(C,C+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){
C[j]++;
if (C[j]>N/2)
return -R;
c=0;
break;
}
}
if (c)
tmp.push_back(i);
}
memset(cnt,0,sizeof(cnt));
tmp.clear();
}
if (b.size()>N/2&&a.size()+cnt[0]<=N/2){
fill(C,C+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){
C[j]++;
if (C[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:43:39: warning: comparison of integer expressions of different signedness: 'const long unsigned int' and 'int' [-Wsign-compare]
43 | return (max(a.size(),b.size())>N/2?-R:R);
| ~~~~~~~~~~~~~~~~~~~~~~^~~~
towns.cpp:44:45: warning: comparison of integer expressions of different signedness: 'const long unsigned int' and 'int' [-Wsign-compare]
44 | if (min(cnt[1]+b.size(),cnt[0]+a.size())>N/2)
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
towns.cpp:46:17: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
46 | if (a.size()>N/2&&b.size()+cnt[1]<=N/2){
| ~~~~~~~~^~~~
towns.cpp:46:38: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
46 | if (a.size()>N/2&&b.size()+cnt[1]<=N/2){
| ~~~~~~~~~~~~~~~^~~~~
towns.cpp:68:17: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
68 | if (b.size()>N/2&&a.size()+cnt[0]<=N/2){
| ~~~~~~~~^~~~
towns.cpp:68:38: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
68 | if (b.size()>N/2&&a.size()+cnt[0]<=N/2){
| ~~~~~~~~~~~~~~~^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
344 KB |
Output is correct |
2 |
Correct |
9 ms |
532 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
12 ms |
484 KB |
Output is correct |
5 |
Correct |
12 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
348 KB |
Output is correct |
2 |
Correct |
12 ms |
348 KB |
Output is correct |
3 |
Correct |
14 ms |
344 KB |
Output is correct |
4 |
Correct |
12 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
10 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
344 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 |
- |