• POJ 2236(并查集裸)


    POJ2236

    #include <iostream>
    #include <cmath>
    #include <cstdio>
    #include <cstring>
    #include <string>
    #include <map>
    #include <iomanip>
    #include <algorithm>
    #include <queue>
    #include <stack>
    #include <set>
    #include <vector>
    //const int maxn = 1e5+5;
    #define ll long long
    ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
    ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
    
    #define MAX INT_MAX
    #define FOR(i,a,b) for( int i = a;i <= b;++i)
    #define bug cout<<"--------------"<<endl
    using namespace std;
    struct node
    {
        int x,y;
    }v[1100];
    int d,n;
    int dp[1100][1100];
    int vis[1100],pre[1100];
    int dis(int a,int b)
    {
        double temp=(v[a].x-v[b].x)*(v[a].x-v[b].x)+(v[a].y-v[b].y)*(v[a].y-v[b].y);
        temp=sqrt(temp);
        if(temp<=d) return 1;
        return 0;
    }
    int Find(int x)
    {
        if(pre[x]!=x) return Find(pre[x]);
        return  x;
    }
    void  howhead(int x,int y)
    {
        int headx=Find(x);
        int heady=Find(y);
        if(headx!=heady)
        {
            pre[headx]=heady;
        }
    }
    int main()
    {
    //   freopen("C:\Users\方瑞\Desktop\input.txt","r",stdin);
    //    freopen("C:\Users\方瑞\Desktop\output.txt","w",stdout);
        cin>>n>>d;
        FOR(i,1,n)
        {
            cin>>v[i].x>>v[i].y;
        }
        FOR(i,1,n)
        {
            FOR(j,i,n)
            {
                if(dis(i,j))
                {
                    dp[i][j]=1;
                    dp[j][i]=1;
                }
            }
        }
        FOR(i,1,n) pre[i]=i;
        char c;
        while(cin>>c)
        {
            if(c=='O')
            {
                int hhh ;
                cin>>hhh;
                vis[hhh]=1;   //注意这个位置================================
                for(int i=1;i<=n;++i)
                {
                    if(vis[i]==1 && dp[hhh][i]==1)
                    {
                        howhead(i,hhh);
    
                    }
                }
            }
            else
            {
                int a,b;
                cin>>a>>b;
    
                if(Find(a)==Find(b))
                    cout<<"SUCCESS"<<endl;
                else cout<<"FAIL"<<endl;
            }
        }
    
    }
  • 相关阅读:
    [ProjectEuler.net] 14
    Design patterns
    初等函数定义
    Design patterns 1.6 Design for change
    Programming In Haskell chapter3
    桌面恶心的无法删除的图标之 淘宝购物 删除办法
    程序员学英语三部曲
    e的给定精度求法
    Summary about Architect Visit
    再谈“我是怎么招聘程序员的”(上)
  • 原文地址:https://www.cnblogs.com/jrfr/p/11260069.html
Copyright © 2020-2023  润新知