P1740 Ink on paper

本文介绍了一道关于计算墨水滴在纸上扩散后何时所有墨水连成一片的问题。通过描述了输入和输出格式,以及给出的代码示例,核心内容是求解 Ink Spill 连通问题的求解策略和时间复杂度。

题目描述

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;}
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值