POJ2236 Wireless Network

Wireless Network
Time Limit: 10000MS Memory Limit: 65536K
Total Submissions: 35815 Accepted: 14853

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

并查集水题。

题意:有一个网络中心,但由于事故里面的电脑都出了故障。并正在进行紧急抢修。已知一台电脑能和它距离小于等于距离d的电脑通讯,且此通讯具有传递性,及A和B能连接,B和C能连接,则A和C能连接。

现在有一系列的查询query,O表示有一台电脑能修复,S则要求你判断两台电脑是否联通。

读入数据并建立并查集。如果是O则对所有电脑进行遍历,如果距离小于d并且该电脑被修复则进行union(一开始没注意此wa)

然后就没有了。cin要3000+ms,scanf可以优化到1000ms以内

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#define vi vector<int>
#define P pair<int,int>
using namespace std;
const int MAX_N = 4e3 + 10;
int par[MAX_N];
int Rank[MAX_N];
bool repair[MAX_N];
P p[MAX_N];
int n,d;
void init()
{
    memset(Rank, 0,sizeof(Rank));
    for(int i = 1; i <= n; i++)
        par[i] = i;
}
int Find(int i)
{
    if(par[i] == i)
        return i;
    return par[i] = Find(par[i]);
}
void unite(int x, int y)
{
    x = Find(x);
    y = Find(y);
    if(Rank[x] < Rank[y])
        par[x] = y;
    else
    {
        par[y] = x;
        if(Rank[x] == Rank[y])
            Rank[x]++;
    }
}
bool same(int x, int y)
{
    return Find(x) == Find(y);
}
double dis(int x, int y)
{
    double dist = (p[x].first - p[y].first) * (p[x].first - p[y].first);
    dist += (p[x].second - p[y].second) * (p[x].second - p[y].second);
    return sqrt(dist);
}
int main()
{
    // freopen("out.txt","w",stdout);
    // ios::sync_with_stdio(false);
    //cin.tie(0);

    scanf("%d%d", &n, &d);
    for(int i = 1; i <= n; i++)
    {
        scanf("%d%d", &p[i].first, &p[i].second);
    }
    char query[5];
    init();
    while(~scanf("%s",query))
    {
        int x, y;
        if(query[0] == 'O')
        {
            scanf("%d", &x);
            repair[x] = 1;
            for(int i = 1; i <= n; i++)
            {
                if(i != x)
                    if(repair[i] == 1 && dis(i, x) <= (double)d)
                        unite(i,x);
            }
        }
        else
        {
            scanf("%d%d", &x, &y);
            if(same(x, y))
                puts("SUCCESS");
            else puts("FAIL");
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
探索全栈前端技术的魅力:HTML+CSS+JS+JQ+Bootstrap网站源码深度解析 在这个数字化时代,构建一个既美观又功能强大的网站成为了许多开发者和企业追逐的目标。本份资源精心汇集了一套完整网站源码,融合了HTML的骨架搭建、CSS的视觉美化、JavaScript的交互逻辑、jQuery的高效操作以及Bootstrap的响应式设计,全方位揭秘了现代网页开发的精髓。 HTML,作为网页的基础,它构建了信息的框架;CSS则赋予网页生动的外观,让设计创意跃然屏上;JavaScript的加入,使网站拥有了灵动的交互体验;jQuery,作为JavaScript的强力辅助,简化了DOM操作与事件处理,让编码更为高效;而Bootstrap的融入,则确保了网站在不同设备上的完美呈现,响应式设计让访问无界限。 通过这份源码,你将: 学习如何高效组织HTML结构,提升页面加载速度与SEO友好度; 掌握CSS高级技巧,如Flexbox与Grid布局,打造适应各种屏幕的视觉盛宴; 理解JavaScript核心概念,动手实现动画、表单验证等动态效果; 利用jQuery插件快速增强用户体验,实现滑动效果、Ajax请求等; 深入Bootstrap框架,掌握移动优先的开发策略,响应式设计信手拈来。 无论是前端开发新手渴望系统学习,还是资深开发者寻求灵感与实用技巧,这份资源都是不可多得的宝藏。立即深入了解,开启你的全栈前端探索之旅,让每一个网页都成为技术与艺术的完美融合!
探索全栈前端技术的魅力:HTML+CSS+JS+JQ+Bootstrap网站源码深度解析 在这个数字化时代,构建一个既美观又功能强大的网站成为了许多开发者和企业追逐的目标。本份资源精心汇集了一套完整网站源码,融合了HTML的骨架搭建、CSS的视觉美化、JavaScript的交互逻辑、jQuery的高效操作以及Bootstrap的响应式设计,全方位揭秘了现代网页开发的精髓。 HTML,作为网页的基础,它构建了信息的框架;CSS则赋予网页生动的外观,让设计创意跃然屏上;JavaScript的加入,使网站拥有了灵动的交互体验;jQuery,作为JavaScript的强力辅助,简化了DOM操作与事件处理,让编码更为高效;而Bootstrap的融入,则确保了网站在不同设备上的完美呈现,响应式设计让访问无界限。 通过这份源码,你将: 学习如何高效组织HTML结构,提升页面加载速度与SEO友好度; 掌握CSS高级技巧,如Flexbox与Grid布局,打造适应各种屏幕的视觉盛宴; 理解JavaScript核心概念,动手实现动画、表单验证等动态效果; 利用jQuery插件快速增强用户体验,实现滑动效果、Ajax请求等; 深入Bootstrap框架,掌握移动优先的开发策略,响应式设计信手拈来。 无论是前端开发新手渴望系统学习,还是资深开发者寻求灵感与实用技巧,这份资源都是不可多得的宝藏。立即深入了解,开启你的全栈前端探索之旅,让每一个网页都成为技术与艺术的完美融合!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值