poj2236

Wireless Network

Time Limit: 10000MS Memory Limit: 65536K
Total Submissions: 10043 Accepted: 4245

Description

An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap computers, but an unexpected aftershock attacked, all computers in the network were all broken. The computers are repaired one by one, and the network gradually began to work again. Because of the hardware restricts, each computer can only directly communicate with the computers that are not farther than d meters from it. But every computer can be regarded as the intermediary of the communication between two other computers, that is to say computer A and computer B can communicate if computer A and computer B can communicate directly or there is a computer C that can communicate with both A and B. 

In the process of repairing the network, workers can take two kinds of operations at every moment, repairing a computer, or testing if two computers can communicate. Your job is to answer all the testing operations. 

Input

The first line contains two integers N and d (1 <= N <= 1001, 0 <= d <= 20000). Here N is the number of computers, which are numbered from 1 to N, and D is the maximum distance two computers can communicate directly. In the next N lines, each contains two integers xi, yi (0 <= xi, yi <= 10000), which is the coordinate of N computers. From the (N+1)-th line to the end of input, there are operations, which are carried out one by one. Each line contains an operation in one of following two formats: 
1. "O p" (1 <= p <= N), which means repairing computer p. 
2. "S p q" (1 <= p, q <= N), which means testing whether computer p and q can communicate. 

The input will not exceed 300000 lines. 

Output

For each Testing operation, print "SUCCESS" if the two computers can communicate, or "FAIL" if not.

Sample Input

4 1
0 1
0 2
0 3
0 4
O 1
O 2
O 4
S 1 4
O 3
S 1 4

Sample Output

FAIL
SUCCESS

Source


很水的一道并查集,发现自己写的效率还是不是很高。贴一个自己ac的代码和别人的效率高一点的代码
#include <iostream>
#include <cstdio>

using namespace std;

typedef struct
{
   int x;
   int y;
}Node;

const int N=1001;

int parent[N+1];
Node com[N+1];
bool isOk[N+1];

void createSet(int x)
{
    parent[x]=x;
    isOk[x]=false;
}

int find(int x)
{
    int r, temp;

    r=x;
    while(r!=parent[r])
    {
         r=parent[r];
    }
    while(x!=r)
    {
        temp=parent[x];
        parent[x]=r;
        x=temp;
    }
    return r;
}

void unionSet(int pa, int pb)
{
    parent[pb]=pa;
}

int main()
{
    int n,d;
    char op[2];
    int a,b;
    long dis;
    int pa, pb;

    scanf("%d%d",&n,&d);
    for(int i=1; i<=n; i++)
    {
        scanf("%d%d",&com[i].x, &com[i].y);
        createSet(i);
    }
    while(scanf("%s",op)!=EOF)
    {
        if(op[0]=='O')  //repair
        {
            scanf("%d",&a);
            isOk[a]=true;
            for(int i=1; i<=n; i++)
            {
                if(isOk[i])
                {
                    dis=(com[a].x-com[i].x)*(com[a].x-com[i].x)+(com[a].y-com[i].y)*(com[a].y-com[i].y);
                    if(dis<=d*d)
                    {
                        pa=find(a);
                        pb=find(i);
                        if(pa!=pb)
                        {
                            unionSet(pa,pb);
                        }
                    }
                }
            }

        }
        else if(op[0]=='S')
        {
           scanf("%d%d",&a,&b);
           if(find(a)==find(b))
               printf("SUCCESS\n");
           else
               printf("FAIL\n");
        }
    }
    return 0;
}
    
Source Code

Problem: 2236	 	User: bingshen
Memory: 184K	 	Time: 1110MS
Language: C++	 	Result: Accepted
Source Code
#include<stdio.h>
#include<algorithm>
#include<math.h>
#define eps 1e-6

using namespace std;

int father[1005],n;
int COUNT[1005],num;
struct POINT
{
	double x;
	double y;
};
POINT pt[1005];

void init()
{
	int i;
	num=0;
	memset(COUNT,0,sizeof(COUNT));
	for(i=0;i<1005;i++)
		father[i]=i;
}

int find(int p)
{
	if(p==father[p])
		return p;
	return father[p]=find(father[p]);
}

double dis(POINT a,POINT b)
{
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

void Union(int a,int b)
{
	int x,y;
	x=find(a);
	y=find(b);
	if(x!=y)
		father[y]=x;
}

int main()
{
	int i,x,y;
	double d;
	char control[2];
	scanf("%d%lf",&n,&d);
	init();
	for(i=1;i<=n;i++)
		scanf("%lf%lf",&pt[i].x,&pt[i].y);
	while(scanf("%s",control)!=EOF)
	{
		if(control[0]=='O')
		{
			scanf("%d",&x);
			for(i=0;i<num;i++)
			{
				if(dis(pt[COUNT[i]],pt[x])<=d+eps)
					Union(COUNT[i],x);
			}
			COUNT[num++]=x;
		}
		if(control[0]=='S')
		{
			scanf("%d%d",&x,&y);
			if(find(x)==find(y))
				printf("SUCCESS\n");
			else
				printf("FAIL\n");
		}
	}
	return 0;
}

你可能感兴趣的:(poj)