# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
400275 | bigg | Towns (IOI15_towns) | C++14 | 0 ms | 0 KiB |
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;
const int MAXN = 115;
const int INF = 1e6;
int dista[MAXN][MAXN];
pair<int, int> p1, p2;
map<int, int > AB;
map<int, vector<int> > AB2;
int get(int a, int b){
if(dista[a][b] != -1) return dista[a][b];
dista[a][b] = dista[b][a] = getDistance(a,b);
return dista[a][b];
}
int disttosv[MAXN];
int pai[MAXN], peso[MAXN];
int find(int x){
if(pai[x] == x) return x;
return pai[x] = find(pai[x]);
}
void join(int u, int v){
u = find(u), v = find(v);
if(u == v) return;
if(peso[u] < peso[v]) swap(u,v);
pai[v] = u;
peso[u] += peso[v];
}
int hubDistance(int N, int sub) {
p1 = {0,0};
p2 = {0,0};
AB.clear();
AB2.clear();
memset(dista, -1, sizeof(dista));
memset(disttosv, 0, sizeof(disttosv));
for(int i = 0; i < N; i++) pai[i] = i, peso[i] = 1, distta[i][i] = 0;
for(int i = 0; i < N; i++){
int d = get(0, i);
if(d > p1.second) p1 = {i,d};
}
for(int i = 0; i < N; i++){
int d = get(i, p1.first);
if(d > p2.second) p2 = { i, d};
}
int ans = INF;
int diam = get(p1.first, p2.first);
for(int i = 0; i < N; i++) ans = min(ans, (diam + abs(get(p1.first, i) - get(p2.first, i)))/2);
if(sub <= 2) return ans;
for(int i = 0; i < N; i++){
if(i == p1.first || i == p2.first) continue;
int i1 = get(i, p1.first), i2 = get(i, p2.first);
AB[i1 - (i1+i2-diam)/2]++;
AB2[i1- (i1+i2-diam)/2].push_back(i);
disttosv[i] = (i1+i2-diam)/2;
}
bool iscentroid = 0;
if(sub == 4){
int jacontei = 1;
for(map<int,int> :: iterator it = AB.begin(); it != AB.end(); it++){
if(it->first == ans || it->first == diam - ans){
if(jacontei <= N/2 && N - jacontei - it->second <= N/2 && it->second <= N/2) iscentroid = 1;
}
jacontei += it->second;
}
if(!iscentroid) ans = -ans;
return ans;
}
int jacontei= 1;
for(auto it : AB2){
if(it.first != ans && it.first != diam - ans){
jacontei += it.second.size();
continue;
}
if(jacontei <= (N/2) && N - jacontei - it.second.size() <= N/2) iscentroid = 1;
for(int i = 0; i < it.second.size(); i++){
for(int j = i + 1; j < it.second.size(); j++){
if(get(it.second[i], it.second[j]) != disttosv[it.second[i]] + disttosv[it.second[j]])
join(it.second[i], it.second[j]);
}
}
for(int i = 0; i < it.second.size(); i++) iscentroid &= (peso[find(it.second[i])] <= N/2);
if(iscentroid) return ans;
jacontei += it.second.size();
}
if(!iscentroid) ans = -ans;
return ans;
}