This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "towns.h"
#include <bits/stdc++.h>
using namespace std;
int n,dist[111][111],u,v,R,sz[2],p[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 f(int i){
return (p[i]==i?i:p[i]=f(p[i]));
}
void unite(int i, int j){
p[f(j)]=f(i);
}
bool check(int i, int j){
int x=(d(i,u)+d(i,v)-d(u,v))/2,y=(d(j,u)+d(j,v)-d(u,v))/2;
return (d(i,j)<x+y);
}
int hubDistance(int N, int sub){
n=N;
iota(p,p+n,0);
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});
u=mx.second;
mx={0,-1};
for (int i=0;i<n;i++)
mx=max(mx,{d(u,i),i});
v=mx.second,R=1e9,sz[0]=sz[1]=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(max(sz[0],sz[1]),(int)min(sz[1]+b.size(),sz[0]+a.size()))>n/2)
return -R;
if (min(max(a.size(),sz[1]+b.size()),max(b.size(),sz[0]+a.size()))<=n/2)
return R;
if (b.size()+sz[1]>n/2)
swap(a,b);
stack <int> st,st2;
for (int i:a){
if (st.empty()||!check(st.top(),i)){
st.push(i);
if (!st2.empty()){
st.push(st2.top());
st2.pop();
}
}
else
st2.push(i);
}
int t=st.top();
while (!st.empty()){
int u=st.top();
if (check(u,t)){
if (st.size()==1){
st2.push(st.top());
st.pop();
}
else{
st.pop();
st.pop();
}
}
else{
if (st2.empty())
return R;
st.pop();
st2.pop();
}
}
return (st2.empty()?R:-R);
}
Compilation message (stderr)
towns.cpp: In function 'int d(int, int)':
towns.cpp:5:18: warning: declaration of 'v' shadows a global declaration [-Wshadow]
5 | int d(int u, int v){
| ~~~~^
towns.cpp:4:24: note: shadowed declaration is here
4 | int n,dist[111][111],u,v,R,sz[2],p[111];
| ^
towns.cpp:5:11: warning: declaration of 'u' shadows a global declaration [-Wshadow]
5 | int d(int u, int v){
| ~~~~^
towns.cpp:4:22: note: shadowed declaration is here
4 | int n,dist[111][111],u,v,R,sz[2],p[111];
| ^
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:55:71: warning: comparison of integer expressions of different signedness: 'const long unsigned int' and 'int' [-Wsign-compare]
55 | if (min(max(a.size(),sz[1]+b.size()),max(b.size(),sz[0]+a.size()))<=n/2)
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
towns.cpp:57:23: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
57 | if (b.size()+sz[1]>n/2)
| ~~~~~~~~~~~~~~^~~~
towns.cpp:73:13: warning: declaration of 'u' shadows a global declaration [-Wshadow]
73 | int u=st.top();
| ^
towns.cpp:4:22: note: shadowed declaration is here
4 | int n,dist[111][111],u,v,R,sz[2],p[111];
| ^
towns.cpp:24:28: warning: unused parameter 'sub' [-Wunused-parameter]
24 | int hubDistance(int N, int sub){
| ~~~~^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |