poj2236 938ms 并查集

#include<cstdio>
using namespace std;
//把可以连通的放到一个集合,然后再判断两台机子是不是在一个集合
const int N = 1e4;
int father[N+5];
bool vis[N+5];
int d;
struct Node
{
    int x;
    int y;
} computers[N+5];
void init(int n)
{
    for(int i = 1; i<=n; ++i)
        father[i] = i;
}
int find(int x)
{
    return x==father[x]?x:father[x]=find(father[x]);
}
/*非递归写法
int find(int x)
{
    int root = x,tmp;
    while(x!=father[x])
    {
        x = father[x];
    }
    if(root!-x)
    {
        tmp = father[root];
        father[root] = x;
        root = tmp;
    }
    return x;
}*/
void _union(int a,int b)
{
    int x = find(a);
    int y = find(b);
    if(x!=y)
        father[x] = y;
}
bool dis(int a,int b)
{
    int x = computers[a].x-computers[b].x;
    int y = computers[a].y-computers[b].y;
    if(x*x+y*y>d*d)
        return false;
    return true;
}

int main()
{
    int n;
    int x,y;
    scanf("%d%d",&n,&d);
    init(n);
    for(int i = 1; i<=n; ++i)
        scanf("%d%d",&computers[i].x,&computers[i].y);
    getchar();
    int p;
    char c;
    while(~scanf("%c",&c))
    {
        if(c=='O')
        {
            scanf("%d",&p);
            vis[p] = true;
            for(int i = 1; i<=n; ++i)
                if(i!=p&&vis[i]&&dis(i,p))
                    _union(i,p);
        }
        else
        {
            scanf("%d%d",&x,&y);
            puts(find(x)==find(y)?"SUCCESS":"FAIL");
        }
        getchar();
    }
    return 0;
}
全部评论

相关推荐

07-09 19:25
门头沟学院 Java
这是要把每一个投校招的都开盒吗?
26届之耻将大局逆转:裁人的时候一次性追回餐费
点赞 评论 收藏
分享
07-07 14:30
复旦大学 Java
遇到这种人我也不知道说啥了
无能的丈夫:但我觉得这个hr语气没什么问题啊(没有恶意
点赞 评论 收藏
分享
06-17 00:26
门头沟学院 Java
程序员小白条:建议换下项目,智能 AI 旅游推荐平台:https://github.com/luoye6/vue3_tourism_frontend 智能 AI 校园二手交易平台:https://github.com/luoye6/vue3_trade_frontend GPT 智能图书馆:https://github.com/luoye6/Vue_BookManageSystem 选项目要选自己能掌握的,然后最好能自己拓展的,分布式这种尽量别去写,不然你只能背八股文了,另外实习的话要多投,尤其是学历不利的情况下,多找几段实习,最好公司title大一点的
无实习如何秋招上岸
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务