Submission #1028004

#TimeUsernameProblemLanguageResultExecution timeMemory
1028004Dan4LifeTowns (IOI15_towns)C++17
48 / 100
13 ms540 KiB
#include "towns.h" #include <bits/stdc++.h> using namespace std; using ar2 = array<int,2>; const int mxN = 115; int n, vis[mxN]; int mem[mxN][mxN]; map<int,int> M; int dis(int a, int b){ if(a==b) return 0; if(mem[a][b]!=-1) return mem[a][b]; if(mem[b][a]!=-1) return mem[b][a]; return mem[a][b]=mem[b][a]=getDistance(a,b); } int p[mxN], sz[mxN]; int findSet(int i) { return p[i]==i?i:p[i]=findSet(p[i]); } bool isSameSet(int i, int j) { return findSet(i)==findSet(j); } void unionSet(int i, int j){ int x = findSet(i), y = findSet(j); if(x==y) return; if(sz[x]<sz[y]) swap(x,y); p[y]=x; sz[x]+=sz[y]; } int hubDistance(int N, int sub) { n = N; ar2 mx = {0,0}; int a = 0, b = 0; M.clear(); memset(mem,-1,sizeof(mem)); for(int i = 0; i < n; i++) mx=max(mx,{dis(a,i),i}); a = mx[1]; mx = {0,0}; for(int i = 0; i < n; i++) mx=max(mx,{dis(a,i),i}); b = mx[1]; int diameter = dis(a,b); int ans = diameter; for(int i = 0; i < n; i++){ if(i==a or i==b) continue; int branch = (dis(a,i)+dis(b,i)-diameter)/2; int lpos = dis(a,i)-branch; int rpos = diameter-lpos; M[lpos]=max({M[lpos],branch,lpos,rpos}); } for(auto u : M) ans=min(ans,u.second); if(sub<=2) return ans; for(auto u : M){ int lpos = u.first, mx_dis = u.second; int rpos = diameter-lpos; if(mx_dis!=ans) continue; bool ok = 1; for(int i = 0; i < n; i++) p[i]=i, sz[i]=1, vis[i]=0; for(int i = 0; i < n; i++){ if(vis[findSet(i)]) continue; if(i!=a and i!=b and i!=0) continue; int i_u = dis(a,i)-lpos; if(dis(a,i)<dis(b,i)) i_u = dis(b,i)-rpos; for(int j = 0; j < n; j++){ int u_j = dis(a,j)-lpos; if(dis(a,j)<dis(b,j)) u_j = dis(b,j)-rpos; if(i_u+u_j != dis(i,j)) unionSet(i,j); } vis[findSet(i)]=1; } int x = findSet(a), y = findSet(b), z = findSet(0); int tot = sz[x]+sz[y]+sz[z]*(z!=x and z!=y); int left = n-tot; if(sub==4 and max({sz[x],sz[y],sz[z]})>n/2) return -ans; if(max({sz[x],sz[y],sz[z],left})<=n/2) return ans; if(sub==4) return -ans; for(int i = 0; i < n; i++) vis[i]=0; vis[x]=vis[y]=vis[z]=1; for(int i = 0; i < n; i++){ if(vis[findSet(i)]) continue; int i_u = dis(a,i)-lpos; if(dis(a,i)<dis(b,i)) i_u = dis(b,i)-rpos; for(int j = 0; j < n; j++){ int u_j = dis(a,j)-lpos; if(dis(a,j)<dis(b,j)) u_j = dis(b,j)-rpos; if(i_u+u_j != dis(i,j)) unionSet(i,j); } vis[findSet(i)]=1; } for(int i = 0; i < n; i++) ok&=(sz[findSet(i)]<=n/2); if(ok) return ans; } return -ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...