# C ++ beginners record (cheese # disjoint-set)


Original title: Cattle customer network
subject description:

existing a big cheese, its height is h, its length and width we can be considered infinite, middle cheese have many of the same radius of the spherical cavity. We can build in this cheese space coordinate system in the coordinate system, the lower surface of the cheese is z = 0, the upper surface of the cheese is z = h.
Now, the lower surface of the cheese has a little mouse Jerry, it knows the coordinates of cheese in all the empty center of the sphere is located. If two or voids tangent intersects Jerry went from one cavity to another cavity, in particular, if a hole or intersects the tangent to the lower surface, Jerry can run into the cavity from the lower surface of the cheese; if a hole or a tangential surface intersecting the upper, Jerry can run on the surface of the cheese from the cavity.
Located on the lower surface of the cheese Jerry wants to know, in the case without damaging the cheese, the possibility of using the existing hole on the surface of the cheese to go?
Space two points P1 (x1, y1, z1) , P2 (x2, y2 , z2) the distance formula the following:
dist (P_1, P_2) = \ sqrt {(x_1-x_2) ^ 2 + (Y_1-Y_2) ^ 2 + (Z_1-Z_2) ^ 2}
dist (P. 1, P 2) = (x 1 -x 2) 2 + (y 1-y 2) 2 + (z 1-z 2) 2


input description:

each input file comprising a plurality of sets of data.
The first line of the input file, containing a positive integer T, represents the number of sets of data contained in the input file.
The next set of data is T, each data format is as follows:
The first line contains three positive integer n, h and r, to a space between two separate numbers, representing the height of the cavity in the cheese, and cheese empty radius.
The next n lines, each line contains three integers x, y, z, with a space between two separate numbers, showing hollow sphere center coordinates (x, y, z).

Description Output:

output file containing a T line, the answer corresponding group data T, if the i-th group of data, Jerry went from the lower surface of the upper surface, the outputs "Yes", if not, outputs "No" ( exclude the quotation marks).

Input Description:

Each input file comprising a plurality of sets of data.
The first line of the input file, containing a positive integer T, represents the number of sets of data contained in the input file.
The next set of data is T, each data format is as follows:
The first line contains three positive integer n, h and r, to a space between two separate numbers, representing the height of the cavity in the cheese, and cheese empty radius.
The next n lines, each line contains three integers x, y, z, with a space between two separate numbers, showing hollow sphere center coordinates (x, y, z).

Description Output:

output file containing a T line, the answer corresponding group data T, if the i-th group of data, Jerry went from the lower surface of the upper surface, the outputs "Yes", if not, outputs "No" ( exclude the quotation marks).

Example. 1

the AC codes

#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 1005
using namespace std;
int x[N],y[N],z[N],father[N];
bool up[N],down[N],check1[N],check2[N];
void init(int n)
{
    int i;
    for(i=1;i<=n;++i)
    {
        up[i]=false;
        down[i]=false;
        check1[i]=false;
        check2[i]=false;
        father[i]=i;
    }
}

long long dist(int i,int j)
{
    long long s1=x[i]-x[j];
    long long s2=y[i]-y[j];
    long long s3=z[i]-z[j];
    return s1*s1+s2*s2+s3*s3;
}
int find(int x)
{
    if(father[x]==x)  return x;
    return father[x]=find(father[x]);
}
void merge(int x,int y)
{
    x=find(x);
    y=find(y);
    if(x!=y)
        father[x]=y;
}
int main()
{
    int n,i,j,h,r,t,k;
    bool flag;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&n,&h,&r);
        
        init(n);
        
        flag=false;
        for(i=1;i<=n;++i)
        {
            scanf("%d%d%d",&x[i],&y[i],&z[i]);
            if(z[i]-r<=0)  down[i]=true;
            if(z[i]+r>=h)  up[i]=true;
            if(up[i]&&down[i])  flag=true;
            for(j=1;j<i;++j)
                if(dist(i,j)<=4ll*r*r)
                    merge(i,j);
        }
        
        for(i=1;i<=n;++i)
        {
            k=find(i);
            if(up[i]&&check2[k]||down[i]&&check1[k])
                flag=true;
            if(up[i])  check1[k]=true;
            if(down[i])  check2[k]=true;
        }
        if(flag)  printf("Yes\n");
        else  printf("No\n");
    }
    return 0;
}

Topic understand
the use of disjoint-set depth search or search violence can work out the problem, I use a disjoint-set method, the learned. First of all holes to set their own, and then determines whether they are tangential or intersect, they will intersect associate, and concentrated relatively simple topic search, code is also relatively easy to understand.

Guess you like

Origin www.cnblogs.com/xiaofengqaq/p/11320346.html