题目描述
Bob accidentally spilled some drops of ink on the paper. The initial position of the i-th drop of ink is (xi, yi), which expands outward by 0.5 centimeter per second, showing a circle. The curious Bob wants to know how long it will take for all the inks to become connected. In order to facilitate the output, please output the square of the time.
输入描述
The first line of input contains one integer T (1≤T≤5), indicating the number of test cases. For each test case, the first line contains one integer nn (2≤n≤5000), indicating the number of ink on the paper. Each of the next n lines contains 2 integers indicating that xx and yy coordinates of the ink.
输出描述
For each test case, output one line containing one decimal, denoting the answer.
样例输入
Copy to Clipboard
2 3 0 0 1 1 0 1 5 1 1 4 5 1 4 2 6 3 10
样例输出
Copy to Clipboard
1 17
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=5e3+10;
int fa[N],tot=0,Tot=0;
int find(int x){
if(fa[x]==x) return x;
else return fa[x]=find(fa[x]);
}
void Union(int x,int y){
int fx=find(x),fy=find(y);
if(fx!=fy) fa[fy]=fx,Tot++;
}
int T,n;ll x[N],y[N];
struct node{
int x,y;ll dis;
bool operator <(const node &b)const
{
return dis<b.dis;
}
}Node[N*N];
int main(){
scanf("%d",&T);
while(T--){
scanf("%d",&n);tot=0;
for(int i=1;i<=n;i++) scanf("%lld%lld",&x[i],&y[i]);
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++) Node[++tot]={i,j,(x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])};
}
for(int i=0;i<=n;i++) fa[i]=i;
sort(Node+1,Node+tot+1);
Tot=0;
for(int i=1;i<=tot;i++){
Union(Node[i].x,Node[i].y);
if(Tot==n-1) {printf("%lld\n",Node[i].dis);break;}
}
}
}
本文介绍了一道关于计算墨水滴在纸上扩散后何时所有墨水连成一片的问题。通过描述了输入和输出格式,以及给出的代码示例,核心内容是求解 Ink Spill 连通问题的求解策略和时间复杂度。
990

被折叠的 条评论
为什么被折叠?



