URAL - 1966 - Cycling Roads(并检查集合 + 判刑线相交)
意甲冠军:n 积分,m 边缘(1 ≤ m < n ≤ 200),问:是否所有的点连接(两个边相交。该 4 点连接)。
主题链接:http://acm.timus.ru/problem.aspx?space=1&num=1966
——>>对于每条边,边上的两端点并入集合,枚举边与边。推断他们是否相交,是的话各点并入集合,最后看集合内元素的个数是否为n。。
#include <cstdio>
#include <cmath> const int MAXN = 200 + 10;
const double EPS = 1e-8; struct POINT
{
double x;
double y; POINT(){} POINT(double x, double y) : x(x), y(y){}
} p[MAXN]; int n, m;
int fa[MAXN], cnt[MAXN];
int u[MAXN], v[MAXN]; typedef POINT Vector;
Vector operator - (Vector A, Vector B)
{
return Vector(A.x - B.x, A.y - B.y);
} int Dcmp(double x)
{
if (fabs(x) < EPS) return 0;
return x > 0 ? 1 : -1;
} double Dot(Vector A, Vector B)
{
return A.x * B.x + A.y * B.y;
} double Cross(Vector A, Vector B)
{
return A.x * B.y - A.y * B.x;
} bool SegmentProperIntersection(POINT a1, POINT a2, POINT b1, POINT b2)
{
double c1 = Cross(a2 - a1, b1 - a1);
double c2 = Cross(a2 - a1, b2 - a1);
double c3 = Cross(b2 - b1, a1 - b1);
double c4 = Cross(b2 - b1, a2 - b1);
return Dcmp(c1) * Dcmp(c2) < 0 && Dcmp(c3) * Dcmp(c4) < 0;
} bool OnSegment(POINT p, POINT a1, POINT a2)
{
return Dcmp(Cross(a1 - p, a2 - p)) == 0 && Dcmp(Dot(a1 - p, a2 - p)) < 0;
} void Init()
{
for (int i = 1; i <= n; ++i)
{
fa[i] = i;
cnt[i] = 1;
}
} int Find(int x)
{
return x == fa[x] ? x : (fa[x] = Find(fa[x]));
} void Union(int x, int y)
{
int xroot = Find(x);
int yroot = Find(y); if (xroot != yroot)
{
fa[yroot] = xroot;
cnt[xroot] += cnt[yroot];
}
} void Read()
{
for (int i = 1; i <= n; ++i)
{
scanf("%lf%lf", &p[i].x, &p[i].y);
}
for (int i = 0; i < m; ++i)
{
scanf("%d%d", u + i, v + i);
Union(u[i], v[i]);
for (int j = 1; j <= n; ++j)
{
if (j != u[i] && j != v[i] && OnSegment(p[j], p[u[i]], p[v[i]]))
{
Union(j, u[i]);
}
}
}
} void Merge()
{
for (int i = 0; i < m; ++i)
{
for (int j = i + 1; j < m; ++j)
{
if (SegmentProperIntersection(p[u[i]], p[v[i]], p[u[j]], p[v[j]]))
{
Union(u[i], u[j]);
}
}
}
} void Output()
{
cnt[Find(1)] == n ? puts("YES") : puts("NO");
} int main()
{
while (scanf("%d%d", &n, &m) == 2)
{
Init();
Read();
Merge();
Output();
} return 0;
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
URAL - 1966 - Cycling Roads(并检查集合 + 判刑线相交)的更多相关文章
- URAL 1966 Cycling Roads 点在线段上、线段是否相交、并查集
F - Cycling Roads Description When Vova was in Shenzhen, he rented a bike and spent most of the ...
- Ural 1966 Cycling Roads
================ Cycling Roads ================ Description When Vova was in Shenzhen, he rented a ...
- URAL 1966 Cycling Roads 计算几何
Cycling Roads 题目连接: http://acm.hust.edu.cn/vjudge/contest/123332#problem/F Description When Vova was ...
- HDU 1272 小希迷宫(并检查集合)
意甲冠军:被判处无向图无环和连接无处不在 思考:并检查集合,trap 您可能有一个直接输入0 0 并且....合并的时候按某一个方向会爆栈,爆了好几次...下次考虑一下直接递归找祖先吧 #includ ...
- hdu1325 Is It A Tree?并检查集合
pid=1325">职务地址 试想一下,在词和话题hdu1272是一样的. 可是hdu1272的博文中我也说了.数据比較水,所以我用非并查集的方法就AC了. 可是这题的数据没那么水,要 ...
- POJ 1984 Navigation Nightmare (数据结构-并检查集合)
Navigation Nightmare Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 4072 Accepted: 1 ...
- CodeForces 277A Learning Languages (并检查集合)
A. Learning Languages time limit per test:2 seconds memory limit per test:256 megabytes The "Be ...
- HDU 1198 Farm Irrigation (并检查集合 和 dfs两种实现)
Farm Irrigation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- zoj 3659 并检查集合
http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemId=4882 现在在牡丹江,明天regional直播比赛,我会在一个月内退休.求祝福 ...
随机推荐
- 【POJ3159】Candies 裸的pqspfa模版题
不多说了.就是裸的模版题. 贴代码: <span style="font-family:KaiTi_GB2312;font-size:18px;">#include & ...
- SE 2014年5月9日
两企业接入到 Internet(A公司和B公司),企业内部的用户及服务器均能够访问到 Internet. 2. A公司规模较大,采用了接入层/汇聚层/核心层的划分模式,接入层划分了多vLan(如图), ...
- CentOS 7 命令备忘录
1 查看目录下有什么文件/目录 >ls //list 列出目录文件信息 >ls -l 或ll //以“详细信息”查看目录文件 >ls -a //-all 查看目录“全部”(包含隐藏文 ...
- Monkey源代码分析之执行流程
在<MonkeyRunner源代码分析之与Android设备通讯方式>中.我们谈及到MonkeyRunner控制目标android设备有多种方法.当中之中的一个就是在目标机器启动一个mon ...
- SAE开发一个应用(不仅仅是建站)
参考http://jingyan.baidu.com/user/npublic/?un=944615045 http://sae.sina.com.cn/ 激活sae账户 登陆新浪云计算官方网站,网址 ...
- Linux下将UTF8编码批量转换成GB2312编码的方法
Linux下将UTF8编码批量转换成GB2312编码的方法 在sqlplus中导入UTF8编码的sql脚本就会出现乱码错误,这时就需要将UTF8编码转换成GB2312编码,下面为大家介绍下在Linux ...
- 数学思想方法-分布式计算-linux/unix技术基础(5)
shell命令行参数 -bash-4.2$ cat test1.sh#!/bin/shecho "$0 "echo "$1 "echo "$2 ...
- Hadoop-2.2.0中国文献——MapReduce 下一代 —配置单节点集群
Mapreduce 包 你需从公布页面获得MapReduce tar包.若不能.你要将源代码打成tar包. $ mvn clean install -DskipTests $ cd hadoop-ma ...
- 怎么解决 ubuntu 装kde桌面遇到的汉化问题
正在读取软件包列表... 完成正在分析软件包的依赖关系树 正在读取状态信息... 完成 现在没有可用的软件包 language-pack-kde-zh,但是它被其它的软件包引用了.这可能意味着这个缺失 ...
- hdu 2074 堆放篮 好开心图纸标题
堆放篮 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...