题目地址:http://poj.org/problem?id=2236

题目大意:n台电脑都坏了,只有距离小于d且被修好的电脑才可以互相联系,联系可传递。输入n和d,n个点的坐标x y。两个操作:O x表示把电脑x修好了,S x y表示询问x和y能否联系。

思路:并查集把能互相联系的电脑都放到同一个集合里(联系可以互相传递),查询的时候只要看x和y是否在一个集合就行了。

   另设置两个数组visit[x]表示x是否被修好,dis[x][y]表示x和y之间的距离是否符合要求【先预处理任意两个点之间的距离<=d的标记为1】。

   每修好一台电脑记得改变visit标记且把所有和他能联系的电脑合并(距离合适且修好了)。

 #include <cstdio>

 using namespace std;

 const int maxn = ;
int fa[maxn], visit[maxn], dis[maxn][maxn]; struct node
{
int x, y;
}node[maxn]; //先预处理所有点间的距离
void pre(int n, int d)
{
for(int i = ; i <= n; i++)
for(int j = ; j <= n; j++)
{
int dx = node[i].x - node[j].x;
int dy = node[i].y - node[j].y;
if(dx*dx + dy*dy <= d*d)
dis[i][j] = ;
else
dis[i][j] = ;
}
} void init(int n)
{
for(int i = ; i <= n; i++)
{
fa[i] = i;
visit[i] = ;
}
} int found(int x)
{
if(fa[x] == x)
return x;
return fa[x] = found(fa[x]);
} void unite(int x, int y)
{
int px = found(x);
int py = found(y);
if(px == py)
return;
else
fa[px] = py;
} int main()
{
int n, d, x, y;
char opr;
scanf("%d%d", &n, &d);
init(n);
for(int i = ; i <= n; i++)
scanf("%d%d", &node[i].x, &node[i].y);
pre(n, d);//pre要写在输入坐标之后!!!!不然dis就全是1!!!
getchar();
while(scanf("%c", &opr) == )
{
if(opr == 'O')
{
scanf("%d", &x);
visit[x] = ;
for(int i = ; i <= n; i++)
if(visit[i] && dis[x][i])//距离合适且修好了的就可以互相联络
unite(x, i);//能互相联络的都放一个集合里
}
else
{
scanf("%d%d", &x, &y);
if(found(x) == found(y))
printf("SUCCESS\n");
else
printf("FAIL\n");
}
getchar();
}
return ;
}

【pre(n, d)要放到输入坐标之后!!!!!!因为要用到坐标!!!!!!!!不然dis全是1!!!!!!!!!!!!!!!!!!!!!!!】

poj2236 Wireless Network(并查集直接套模板的更多相关文章

  1. POJ2236 Wireless Network 并查集简单应用

    Description An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have ...

  2. POJ2236 Wireless Network 并查集

    水题 #include<cstdio> #include<cstring> #include<queue> #include<set> #include ...

  3. POJ 2236 Wireless Network (并查集)

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 18066   Accepted: 761 ...

  4. Wireless Network 并查集

    An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wi ...

  5. poj 2236 Wireless Network (并查集)

    链接:http://poj.org/problem?id=2236 题意: 有一个计算机网络,n台计算机全部坏了,给你两种操作: 1.O x 修复第x台计算机 2.S x,y 判断两台计算机是否联通 ...

  6. POJ 2236 Wireless Network [并查集+几何坐标 ]

    An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wi ...

  7. poj-2236 Wireless Network &&poj-1611 The Suspects && poj-2524 Ubiquitous Religions (基础并查集)

    http://poj.org/problem?id=2236 由于发生了地震,有关组织组把一圈电脑一个无线网,但是由于余震的破坏,所有的电脑都被损坏,随着电脑一个个被修好,无线网也逐步恢复工作,但是由 ...

  8. POJ-2236 Wireless Network 顺便讨论时间超限问题

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 26131   Accepted: 108 ...

  9. [LA] 3027 - Corporative Network [并查集]

    A very big corporation is developing its corporative network. In the beginning each of the N enterpr ...

随机推荐

  1. Hibernate学习(六)

    Hibernate的三种查询方式 1.Criteria 查询 ,Query  By Criteria ( QBC )JPA 规范中定义的一种查询方法,但是不推荐使用 2.HQL : Hibernate ...

  2. StringUtils中isEmpty方法和isBlank方法的区别

    1.StringUtils.isEmpty没有忽略空格参数,是以是否为空和是否存在为判断依据. 示例: StringUtils.isEmpty("yyy") = false Str ...

  3. ANSYS-APDL施加扭转载荷CERIG命令

    目录 1. 要求 2. ANSYS有限元分析 2.1 APDL建模 2.2 APDL施加载荷 2.3 APDL查看结果 3. 举一反三 1. 要求 一块0.8m*0.4m*0.04m厚的钢板,在板的两 ...

  4. leetcode 0206

    目录 ✅ 292. Nim 游戏 ✅ 933. 最近的请求次数 ✅ 942. 增减字符串匹配 仍旧有需要思考的地方 py尝试 ✅ 977. 有序数组的平方 ✅ 292. Nim 游戏 https:// ...

  5. C++98常用特性介绍——mutable关键字

    讲mutable前,先讲一下const函数,讲const函数前,先讲一下函数前后加const的区别 一.C++函数前后加const的区别 1)函数前加const:普通函数或非静态成员函数前均可加con ...

  6. Linux查看当前系统32位还是64位

    getconf LONG_BIT 此方法会直接返回32或64

  7. C/C++网络编程6——实现基于UDP的服务器端/客户端

    通过前面几节的内容,我们已经可以实现基本的C/S结构的程序了,但是当多个客户端同时向服务器端请求服务时,服务器端只能按顺序一个一个的服务,这种情况下,客户端的用户是无法忍受的.所以虚实现并发的服务器端 ...

  8. Servlet读取xml文件的配置参数

    web.xml中数据库连接配置: <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns: ...

  9. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 显示代码:电脑程序输出: Sample output

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  10. Linux环境查看Java应用消耗资源情况

    linux线上资源耗时定位 https://www.cnblogs.com/wuchanming/p/7766994.html 1. jps -ml 查看服务器上运行的Java程序 2. jmap 查 ...