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>
#define N 200
using namespace std;
int dist[N][N];
int get(int i,int j){
if(i > j)swap(i,j);
if(i == j)return 0;
if(dist[i][j])return dist[i][j];
return dist[i][j] = getDistance(i,j);
}
int cnt[N];
int child_cnt[N];
int hubDistance(int n, int sub) {
for(int i = 0;i<n;i++){
for(int j = 0;j<n;j++){
dist[i][j] = 0;
}
}
int root = 0;
for(int i = 1;i<n;i++){
if(get(0,i) > get(0,root))
root = i;
}
int deepest = 0;
for(int i = 1;i<n;i++){
if(get(root,i) > get(root,deepest))
deepest = i;
}
set<int> depths;
map<int,int> mp;
for(int i = 0;i<n;i++){
depths.insert( (get(root,deepest) + get(root,i) -get(deepest,i) )/2);
mp[(get(root,deepest) + get(root,i) -get(deepest,i) )/2]++;
}
int mini = 1e9;
vector<int> v;
for(auto u:depths){
v.push_back(u);
mini = min(mini,max(u,get(root,deepest)-u));
}
vector<int> candidates;
for(int i = v.size()-1;i>=0;i--){
cnt[i] = mp[v[i]];
child_cnt[i] = 0;
if(i+1 != v.size())
child_cnt[i] = cnt[i+1];
cnt[i] += child_cnt[i];
//cout << v[i] << " "<< cnt[i] << endl;
if(max(v[i],get(root,deepest)-v[i]) == mini && child_cnt[i] <= n/2 && (n - cnt[i]) <= n/2){
candidates.push_back(i);
}
}
int sign = 1;
if(sub > 2){
if(candidates.empty())sign = -1;
if(candidates.size() == 1){
if(sub == 4){
if((n - child_cnt[candidates[0]] - (n - cnt[candidates[0]])) > n/2)
sign = -1;
}
else{
vector<int> numbers;
for(int i = 0;i<n;i++){
if((get(root,deepest) + get(root,i) -get(deepest,i) )/2 == v[candidates[0]]){
numbers.push_back(i);
}
}
int maxi = 0;
for(auto u:numbers){
int now = 0;
for(auto c:numbers){
if((get(root,u) + get(root,c) -get(u,c) )/2 != v[candidates[0]]){
now++;
}
}
maxi = max(maxi,now);
}
if(maxi > n/2){
sign = -1;
}
}
}
}
return mini * sign;
}
Compilation message (stderr)
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:43:22: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
43 | for(int i = v.size()-1;i>=0;i--){
| ~~~~~~~~^~
towns.cpp:46:10: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
46 | if(i+1 != v.size())
| ~~~~^~~~~~~~~~~
# | 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... |