【并查集】POJ2236-Wireless Network

【题目大意】

已知每一台电脑只能与它距离为d的电脑相连通,但是两台电脑间可以以第三台作为媒介连接。现在电脑全被损坏。每次可以进行两个操作中的一个,或是修好一台电脑,或是查询两台电脑是否连通。

【思路】

显然是并查集。每次修好一台新电脑,就与之前修好的每一台电脑进行判断,距离在d以内就合并。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 using namespace std;
 5 const int MAXN=1001+50;
 6 int n,d;
 7 int x[MAXN],y[MAXN],rep[MAXN];
 8 int h[MAXN],pa[MAXN];
 9
10 int ind(int a,int b)
11 {
12     return ((x[a]-x[b])*(x[a]-x[b])+(y[a]-y[b])*(y[a]-y[b])<=d*d);
13 }
14
15 void newset()
16 {
17     for (int i=0;i<n;i++)
18     {
19         h[i]=0;
20         pa[i]=i;
21     }
22 }
23
24 int find(int x)
25 {
26     int r=x;
27     while (pa[r]!=r) r=pa[r];
28     int p=x;
29     while (pa[p]!=r)
30     {
31         int temp=pa[p];
32         pa[p]=r;
33         p=temp;
34     }
35     return r;
36 }
37
38 void unionset(int fa,int fb)
39 {
40     if (h[fa]>=h[fb])
41     {
42         pa[fb]=fa;
43         if (h[fa]==h[fb]) h[fa]++;
44     }
45     else
46         pa[fa]=fb;
47 }
48
49 int main()
50 {
51     scanf("%d%d",&n,&d);
52     for (int i=0;i<n;i++)
53     {
54         scanf("%d%d",&x[i],&y[i]);
55     }
56
57     char c;
58     int repd=0;//ÒѾ­Ð޺õĵçÄÔÊýÁ¿
59     newset();
60
61     getchar();
62     while (scanf("%c",&c)!=EOF)
63     {
64         if (c==‘S‘)
65         {
66             int a,b;
67             scanf("%d%d",&a,&b);
68             a--;
69             b--;
70             if (find(a)==find(b)) cout<<"SUCCESS"<<endl;else cout<<"FAIL"<<endl;
71         }
72
73         else
74         {
75             int a;
76             scanf("%d",&a);
77             a--;
78             for (int i=0;i<repd;i++)
79             {
80                 if (ind(a,rep[i]))
81                 {
82                     int fa=find(a),fb=find(rep[i]);
83                     unionset(fa,fb);
84                 }
85             }
86             rep[repd]=a;
87             repd++;
88         }
89         getchar();
90     }
91     return 0;
92 }
时间: 2024-10-06 05:16:38

【并查集】POJ2236-Wireless Network的相关文章

(并查集) Wireless Network --POJ --2236

链接: http://poj.org/problem?id=2236 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82830#problem/A 代码: #include<stdio.h> #include<string.h> #include<stdlib.h> #include<math.h> #include<algorithm> #include<iostream>

POJ2236 wireless network 【并查集水题】

前端开发whqet,csdn,王海庆,whqet,前端开发专家 今天是个好日子,2014年5月20日,表白的最佳时机,虽说孩子已经四岁.结婚已经五年,但是也不可以偷懒,于是有了这个效果. 在线研究点这里,下载收藏点这里.程序猿and程序媛,大胆秀出你的爱吧. 利用html5 canvas实现动态的文字粒子效果,效果如下. OK,简单看看原理,首先我们需要在canvas里面实现描边文字,然后利用getImageData获得描边文字的像素矩阵,将粒子效果绑定在描边文章上. 整个效果如下. html文

poj2236 Wireless Network 并查集简单应用

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

poj2236(Wireless Network)并查集

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

POJ2236 Wireless Network 【并查集】

Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 16885   Accepted: 7091 Description An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap computer

poj-2236 Wireless Network (基础并查集)

http://poj.org/problem?id=2236 由于发生了地震,有关组织组把一圈电脑一个无线网,但是由于余震的破坏,所有的电脑都被损坏,随着电脑一个个被修好,无线网也逐步恢复工作,但是由于硬件的限制,一台电脑和另一台电脑能够相连当他们之间的距离小于d,或者还有一台电脑当中介,分别与两台电脑相连. 在修复的过程中,工作者会有两种操作,修复电脑和询问电脑a和电脑b是否相连.当询问的时候输出答案. 因为输入数据很大,需要快速判断电脑a和电脑b相连,所以自然想到用并查集. 初始时候 全部电

POJ2236 Wireless Network 并查集 好题

Wireless Network 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 broke

并查集——poj2236(带权并查集)

题目:Wireless Network 题意:给定n台已损坏计算机的位置和计算机最远通信距离d,然后分别根据命令执行以下两种操作: "O p" (1 <= p <= N) :表示修理计算机p: "S p q" (1 <= p, q <= N) :表示检测计算机p和计算机q能否通信. 输出:能通信则输出"SUCCESS",否则输出"FAIL" 题解: 带权并查集还是那个重要的知识点--关系. 此题,我们使

POJ2236 Wireless Network (easy)

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

并查集 poj2236

网址:http://poj.org/problem?id=2236 题意:有n台坏的电脑,如果每两台电脑的距离不能超过d,那么这两台电脑有联系,用字符串O 表示标记第x台电脑维修了,用S判断从X到y是否有联系... 题解:用并查集记录和查找每个点的父亲节点,每次输入的同时遍历该点和其他点是否有联系(即距离小于等于的)..... 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using name