#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]++;
}
if (!R)
b.clear();
R=(R+d(u,v))/2;
if (max(cnt[0],cnt[1])>N/2)
return -R;
if (min(cnt[1]+b.size(),cnt[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(),cnt[1]+b.size())<=N/2)
return R;
if (max(b.size(),cnt[0]+a.size())<=N/2)
return R;
if (b.size()+cnt[1]<=N/2){
int ch=1;
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)
ch=0;
c=0;
break;
}
}
if (c)
tmp.push_back(i);
}
if (ch)
return R;
tmp.clear();
}
if (a.size()+cnt[0]<=N/2){
int ch=1;
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)
ch=0;
c=0;
break;
}
}
if (c)
tmp.push_back(i);
}
if (ch)
return R;
tmp.clear();
}
return -R;
}
Compilation message
towns.cpp: In function 'int hubDistance(int, int)':
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: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:38: warning: comparison of integer expressions of different signedness: 'const long unsigned int' and 'int' [-Wsign-compare]
48 | if (max(a.size(),cnt[1]+b.size())<=N/2)
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
towns.cpp:50:38: warning: comparison of integer expressions of different signedness: 'const long unsigned int' and 'int' [-Wsign-compare]
50 | if (max(b.size(),cnt[0]+a.size())<=N/2)
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
towns.cpp:52:24: 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()+cnt[1]<=N/2){
| ~~~~~~~~~~~~~~~^~~~~
towns.cpp:76:24: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
76 | if (a.size()+cnt[0]<=N/2){
| ~~~~~~~~~~~~~~~^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
348 KB |
Output is correct |
2 |
Correct |
8 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
11 ms |
348 KB |
Output is correct |
5 |
Correct |
17 ms |
348 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 |
11 ms |
344 KB |
Output is correct |
4 |
Correct |
16 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
348 KB |
Output is correct |
2 |
Correct |
12 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
11 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
348 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 |
- |