A - Wireless Network-poj2236(简单并查集)
#include<stdio.h> const int maxn = ; struct node
{
int x, y, ok;
}p[maxn];//保存所有的村庄,ok表示是否已经修复 int f[maxn]; int Len(node a, node b)//求两个村庄的距离
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
int Find(int x)
{
if(f[x] != x)
f[x] = Find(f[x]);
return f[x];
} int main()
{
int i, N, D, u, v;
char s[]; scanf("%d%d", &N, &D); D = D*D;//因为距离只做判断,所以直接用平方数判断更准确,避免小数 for(i=; i<=N; i++)
{
scanf("%d%d", &p[i].x, &p[i].y);
p[i].ok = ;
f[i] = i;
} while(scanf("%s", s) != EOF)
{
if(s[] == 'O')
{
scanf("%d", &u); if(p[u].ok == )
{
p[u].ok = ;
for(i=; i<=N; i++)
{
if(u != i && p[i].ok && Len(p[i], p[u]) <= D)
{
v = Find(i);
int k = Find(u);
f[k] = v;
}
}
} }
else
{
scanf("%d%d", &u, &v);
u = Find(u);
v = Find(v); if(u != v)
printf("FAIL\n");
else
printf("SUCCESS\n");
}
} return ;
}
重构了一下代码,试图弄出来并查集的模版,不过不是太理想
#include<stdio.h> const int maxn = 1e3+; struct FindSets
{
int *Father, size; FindSets(int size)
: size(size)
{
Father = new int[size+];
for(int i=; i<=size; i++)
Father[i] = i;
}
~FindSets()
{
delete[] Father;
}
int Find(int x)
{
if(Father[x] != x)
Father[x] = Find(Father[x]);
return Father[x];
}
bool connect(int u, int v, bool need)
{///判断两个点是否相连,如果需要相连则连接
u = Find(u);
v = Find(v);
if(need == true)
Father[v] = u;
return u == v;
}
}; struct point
{
int x, y, fine;
}; bool Dis(point &a, point &b, int D)
{///判断两点之间的距离是否大于D
return ((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)) <= D*D;
} void Repair(int u, int D, point p[], FindSets &fs)
{///修复点u
for(int i=; i<=fs.size; i++)
{
if(p[i].fine == true && Dis(p[i], p[u], D))
fs.connect(i, u, true);
}
p[u].fine = true;
} int main()
{
int N, D; scanf("%d%d", &N, &D); FindSets fs(N);
point *p = new point[N+](); for(int i=; i<=N; i++)
scanf("%d%d", &p[i].x, &p[i].y); int u, v;
char op[]; while(scanf("%s", op) != EOF)
{
if(op[] == 'O')
{
scanf("%d", &u);
Repair(u, D, p, fs);
}
else
{
scanf("%d%d", &u, &v);
if(fs.connect(u, v, false) == true)
printf("SUCCESS\n");
else
printf("FAIL\n");
}
} delete[] p; return ;
}
A - Wireless Network-poj2236(简单并查集)的更多相关文章
- Wireless Network POJ - 2236 (并查集)
#include<iostream> #include<vector> #include<string> #include<cmath> #includ ...
- POJ 2236 Wireless Network 第一次做并查集,第一次写博客
题意是判断两台电脑是否能通讯,两台修好的电脑距离在指定距离内可直接通讯,且两台修好的电脑能通过一台修好的电脑间接通讯.代码如下: #include <iostream> #include ...
- POJ-2236(并查集)
Wireless NetWork POJ-2236 需要注意这里的树的深度需要初始化为0. 而且,find函数需要使用路径压缩,这里的unint合并函数也使用了优化(用一开始简单的合并过不了). #i ...
- POJ 2524 (简单并查集) Ubiquitous Religions
题意:有编号为1到n的学生,然后有m组调查,每组调查中有a和b,表示该两个学生有同样的宗教信仰,问最多有多少种不同的宗教信仰 简单并查集 //#define LOCAL #include <io ...
- poj1611 简单并查集
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 32781 Accepted: 15902 De ...
- 1213 How Many Tables(简单并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 简单并查集,统计单独成树的数量. 代码: #include <stdio.h> #i ...
- 【简单并查集】Farm Irrigation
Farm Irrigation Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Tot ...
- ACM_“打老虎”的背后(简单并查集)
“打老虎”的背后 Time Limit: 2000/1000ms (Java/Others) Problem Description: “习大大”自担任国家主席以来大力反腐倡廉,各地打击贪腐力度也逐步 ...
- POJ-2236.WireleseNetwork.(并查集)
Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 43199 Accepted: 178 ...
随机推荐
- runnable和thread的区别
一是写一个类继承自Thread类,然后重写里面的run方法,用start方法启动线程二是写一个类实现Runnable接口,实现里面的run方法,用new Thread(Runnable target) ...
- 最简单的基于FFmpeg的移动端例子:IOS 推流器
转至:http://blog.csdn.net/leixiaohua1020/article/details/47072519 ================================== ...
- JAVA 环境变量
Java是由Sun公司开发的一种应用于分布式网络环境的程序设计语言,Java语言拥有跨平台的特性,它编译的程序能够运行在多种操作系统平台上,可以实现“一次编写,到处运行”的强大功能. 工具/原料 JD ...
- 342. Power of Four
题目: Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example ...
- 关于考虑浏览器兼容性时间的工具demo
//支持跨浏览器的添加事件. var btn = document.getElementById("btn"); function showMes() { alert(" ...
- Java包详解
背景: 在java中要求文件名和类名相同,所以如果把多个类放在一起,就可能出现文件名冲突 所以用包来解决,一个包中可以包含多个类 包是java提供的一种用于区别类的名字空间的机制,是类的组织方式,是一 ...
- bzoj2260: 商店购物 && 4349: 最小树形图
Description Grant是一个个体户老板,他经营的小店因为其丰富的优惠方案深受附近居民的青睐,生意红火.小店的优惠方案十分简单有趣.Grant规定:在一次消费过程中,如果您在本店购买了精制油 ...
- Linux——搭建PHP开发环境第四步:composer
原文链接:https://my.oschina.net/jiangbianwanghai/blog/473249 1.下载composer.phar [root#localhost opt]# cur ...
- VS快捷键和技巧
1. 怎样调整代码排版的格式? 选择:编辑->高级->设置文档的格式或编辑->高级->设置选中代码的格式. 格式化cs代码:Ctrl+k+f 格式化aspx代码:Ctrl+k+ ...
- IIS短文件名漏洞修补方法之一改注册表一个注意项
1)1.png 为漏洞存在没有做任何修复的时候的扫描 修复:2) 修改注册表键值: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSy ...