zoukankan      html  css  js  c++  java
  • kuangbin_UnionFind A (POJ 2236)

    挺接近模板的一题 接受O操作的时候扫一遍 符合条件的merge进去 done

    #include <cstdio>
    #include <cstring>
    #include <cmath>
    
    struct Point{float x,y;};
    int father[1010];
    
    float distance(Point a, Point b){
        return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
    }
    
    int find(int x)
    {
        while(father[x] != x)
            x = father[x];
        return x;
    }
    
    void merge(int x, int y)
    {
        int tx = find(x);
        int ty = find(y);
        if(tx != ty)
            father[tx] = ty;
    }
    
    int main()
    {
        Point p[1010];
        int n, d;
        bool open[1010];
        //initiate
        memset(open, false, sizeof open);
        for(int i = 1; i < 1010; i++)
            father[i] = i;
        //save cordinates
        scanf("%d%d", &n, &d);
        for(int i = 1; i <= n; i++)
            scanf("%f%f", &p[i].x, &p[i].y);
        //accept operations
        char tmp[2];
        while(scanf("%s",tmp)!=EOF)
        {
            if(tmp[0] == 'O'){
                int node;
                scanf("%d", &node);
                open[node] = true;
                for(int i = 1; i <= n; i++)
                    if(open[i] && i != node)
                        if(distance(p[i], p[node]) <= (float)d){
                            //printf("distance(a,b) = %f
    ", distance(p[i], p[node]));
                            //printf("a = %d b = %d
    ", node, i);
                            merge(node, i);
                        }
            }
            else if (tmp[0] == 'S'){
                int a, b;
                scanf("%d%d",&a,&b);
                if(find(a) == find(b))
                    puts("SUCCESS");
                else
                    puts("FAIL");
            }
        }
        return 0;
    }
  • 相关阅读:
    编写可维护的JavaScript代码(部分)
    Canvas
    初识ES6
    vue.js入门上
    ASP.NET中的物理路径与虚拟路径
    慎用标签选择器
    PHP服务器负载判断
    mac下安装redis
    mac安装memcache
    MySQL定时检查是否宕机并邮件通知
  • 原文地址:https://www.cnblogs.com/quasar/p/5060153.html
Copyright © 2011-2022 走看看