传送门
思路:
prim最小生成树,克鲁斯卡尔会神奇的t掉。
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<map>
#include<queue>
#include<math.h>
#include<vector>
using namespace std;
#define ll long long
struct node{
ll x,y;
}e[5010];
ll dis[5010];
int vis[5010];
int n;
ll prim()
{
memset(dis,0,sizeof(dis));
memset(vis,0,sizeof(vis));
ll ans = 0;
vis[1] = 1;
for(int i = 1; i <= n; i++)
if(!vis[i])
dis[i] = pow(abs(e[1].x-e[i].x),2)+pow(abs(e[1].y-e[i].y), 2);
for(int i = 1; i <= n; i++)
{
int flag = 0;
ll minn = 1e18+1;
for(int j = 1; j <= n; j++)
{
if(!vis[j] && dis[j] < minn)
{
flag = j,minn = dis[j];
}
}
ans = max(ans, dis[flag]);
vis[flag] = 1;
for(int j = 1; j <= n; j++)
{
if(!vis[j])
dis[j] = min(dis[j], (ll)pow(abs(e[flag].x-e[j].x),2) + (ll)pow(abs(e[flag].y-e[j].y),2));
}
}
return ans;
}
int main()
{
int t;
cin>>t;
while(t--)
{
scanf("%d",&n);
for(int i = 1; i <= n; i++)
{
ll op1,op2;
scanf("%lld%lld",&op1,&op2);
e[i].x = op1,e[i].y = op2;
}
printf("%lld\n",prim());
}
}