Cycling Roads

题目连接:

http://acm.hust.edu.cn/vjudge/contest/123332#problem/F

Description

When Vova was in Shenzhen, he rented a bike and spent most of the time cycling around the city. Vova was approaching one of the city parks when he noticed the park plan hanging opposite the central entrance. The plan had several marble statues marked on it. One of such statues stood right there, by the park entrance. Vova wanted to ride in the park on the bike and take photos of all statues. The park territory has multiple bidirectional cycling roads. Each cycling road starts and ends at a marble statue and can be represented as a segment on the plane. If two cycling roads share a common point, then Vova can turn on this point from one road to the other. If the statue stands right on the road, it doesn't interfere with the traffic in any way and can be photoed from the road.

Can Vova get to all statues in the park riding his bike along cycling roads only?

Input

The first line contains integers n and m that are the number of statues and cycling roads in the park (1 ≤ m < n ≤ 200) . Then n lines follow, each of them contains the coordinates of one statue on the park plan. The coordinates are integers, their absolute values don't exceed 30 000. Any two statues have distinct coordinates. Each of the following m lines contains two distinct integers from 1 to n that are the numbers of the statues that have a cycling road between them.

Output

Print “YES” if Vova can get from the park entrance to all the park statues, moving along cycling roads only, and “NO” otherwise.

Sample Input

4 2

0 0

1 0

1 1

0 1

1 3

4 2

Sample Output

YES

Hint

题意

平面给你n个点,以及m对直线,问你这m条直线是否能够使得所有点都在一个连通块内

题解:

用并查集去维护就好了,如果两条直线相交,就把直线端点的压进并查集就好了。

然后最后统计一下并查集的大小。

代码

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. /* 常用的常量定义 */
  4. const double INF = 1E200;
  5. const double EP = 1E-10;
  6. const int MAXV = 300;
  7. const double PI = 3.14159265;
  8. const int maxn = 300;
  9. /* 基本几何结构 */
  10. struct POINT
  11. {
  12. double x;
  13. double y;
  14. POINT(double a=0, double b=0) { x=a; y=b;} //constructor
  15. };
  16. struct LINESEG
  17. {
  18. POINT s;
  19. POINT e;
  20. int a,b;
  21. LINESEG(POINT a, POINT b) { s=a; e=b;}
  22. LINESEG() { }
  23. };
  24. struct LINE // 直线的解析方程 a*x+b*y+c=0 为统一表示,约定 a >= 0
  25. {
  26. double a;
  27. double b;
  28. double c;
  29. LINE(double d1=1, double d2=-1, double d3=0) {a=d1; b=d2; c=d3;}
  30. };
  31. double multiply(POINT sp,POINT ep,POINT op)
  32. {
  33. return((sp.x-op.x)*(ep.y-op.y)-(ep.x-op.x)*(sp.y-op.y));
  34. }
  35. // 如果线段u和v相交(包括相交在端点处)时,返回true
  36. //
  37. //判断P1P2跨立Q1Q2的依据是:( P1 - Q1 ) × ( Q2 - Q1 ) * ( Q2 - Q1 ) × ( P2 - Q1 ) >= 0。
  38. //判断Q1Q2跨立P1P2的依据是:( Q1 - P1 ) × ( P2 - P1 ) * ( P2 - P1 ) × ( Q2 - P1 ) >= 0。
  39. bool intersect(LINESEG u,LINESEG v)
  40. {
  41. return( (max(u.s.x,u.e.x)>=min(v.s.x,v.e.x))&& //排斥实验
  42. (max(v.s.x,v.e.x)>=min(u.s.x,u.e.x))&&
  43. (max(u.s.y,u.e.y)>=min(v.s.y,v.e.y))&&
  44. (max(v.s.y,v.e.y)>=min(u.s.y,u.e.y))&&
  45. (multiply(v.s,u.e,u.s)*multiply(u.e,v.e,u.s)>=0)&& //跨立实验
  46. (multiply(u.s,v.e,v.s)*multiply(v.e,u.e,v.s)>=0));
  47. }
  48. /******************************************************************************
  49. 判断点p是否在线段l上
  50. 条件:(p在线段l所在的直线上) && (点p在以线段l为对角线的矩形内)
  51. *******************************************************************************/
  52. bool online(LINESEG l,POINT p)
  53. {
  54. return( (multiply(l.e,p,l.s)==0) &&( ( (p.x-l.s.x)*(p.x-l.e.x)<=0 )&&( (p.y-l.s.y)*(p.y-l.e.y)<=0 ) ) );
  55. }
  56. int fa[maxn];
  57. int fi(int u){
  58. return u != fa[u] ? fa[u] = fi( fa[u] ) : u;
  59. }
  60. void uni(int u ,int v){
  61. int p1 = fi( u ) , p2 = fi( v );
  62. if( p1 != p2 ) fa[p1] = p2;
  63. }
  64. POINT p[maxn];
  65. LINESEG L[maxn];
  66. int main(){
  67. int n,m;
  68. scanf("%d%d",&n,&m);
  69. for(int i=1;i<=n;i++){
  70. scanf("%lf%lf",&p[i].x,&p[i].y);
  71. fa[i]=i;
  72. }
  73. for(int i=1;i<=m;i++){
  74. int x,y;
  75. scanf("%d%d",&x,&y);
  76. L[i].s=p[x],
  77. L[i].e=p[y];
  78. L[i].a=x;
  79. L[i].b=y;
  80. uni(x,y);
  81. }
  82. for(int i=1;i<=n;i++){
  83. for(int j=1;j<=m;j++){
  84. if(online(L[j],p[i])){
  85. uni(i,L[j].a);
  86. uni(i,L[j].b);
  87. }
  88. }
  89. }
  90. for(int i=1;i<=m;i++){
  91. for(int j=1;j<=m;j++){
  92. if(intersect(L[i],L[j])){
  93. uni(L[i].a,L[j].a);
  94. uni(L[i].b,L[j].b);
  95. uni(L[i].b,L[j].a);
  96. uni(L[i].a,L[j].b);
  97. }
  98. }
  99. }
  100. int tmp = fi(1);
  101. for(int i=1;i<=n;i++){
  102. if(fi(i)!=tmp){
  103. printf("NO\n");
  104. return 0;
  105. }
  106. }
  107. printf("YES\n");
  108. return 0;
  109. }

URAL 1966 Cycling Roads 计算几何的更多相关文章

  1. URAL 1966 Cycling Roads 点在线段上、线段是否相交、并查集

    F - Cycling Roads     Description When Vova was in Shenzhen, he rented a bike and spent most of the ...

  2. Ural 1966 Cycling Roads

    ================ Cycling Roads ================   Description When Vova was in Shenzhen, he rented a ...

  3. URAL - 1966 - Cycling Roads(并检查集合 + 判刑线相交)

    意甲冠军:n 积分,m 边缘(1 ≤ m < n ≤ 200),问:是否所有的点连接(两个边相交.该 4 点连接). 主题链接:http://acm.timus.ru/problem.aspx? ...

  4. Ural 2036. Intersect Until You're Sick of It 计算几何

    2036. Intersect Until You're Sick of It 题目连接: http://acm.timus.ru/problem.aspx?space=1&num=2036 ...

  5. URAL 1775 B - Space Bowling 计算几何

    B - Space BowlingTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/ ...

  6. Ural 1046 Geometrical Dreams(解方程+计算几何)

    题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1046 参考博客:http://hi.baidu.com/cloudygoose/item ...

  7. URAL 2099 Space Invader题解 (计算几何)

    啥也不说了,直接看图吧…… 代码如下: #include<stdio.h> #include<iostream> #include<math.h> using na ...

  8. URAL 1963 Kite 计算几何

    Kite 题目连接: http://acm.hust.edu.cn/vjudge/contest/123332#problem/C Description Vova bought a kite con ...

  9. 【计算几何】URAL - 2101 - Knight's Shield

    Little Peter Ivanov likes to play knights. Or musketeers. Or samurai. It depends on his mood. For pa ...

随机推荐

  1. 一个中国地图的SVG,可以带参数

    <script src="http://files.cnblogs.com/files/LoveOrHate/jquery.min.js"></script> ...

  2. MongoDB 之 手把手教你增删改查 MongoDB - 2

    我们在  MongoDB 之 你得知道MongoDB是个什么鬼 MongoDB - 1  中学习了如果安装部署一个 MongoDB 如果没看到我的金玉良言的话,就重新打开一次客户端和服务端吧 本章我们 ...

  3. js数组方法forEach,map,filter,every,some实现

    Array.prototype.map = function(fun /*, thisp*/) { var len = this.length; if (typeof fun != "fun ...

  4. Python 入门基础3 --流程控制

    今日目录: 一.流程控制 1. if 2. while 3. for 4. 后期补充内容 一.流程控制--if 1.if判断: # if判断 age = 21 weight = 50 if age & ...

  5. 【ARTS】01_04_左耳听风-20181203~1209

    ARTS: Algrothm: leetcode算法题目 Review: 阅读并且点评一篇英文技术文章 Tip/Techni: 学习一个技术技巧 Share: 分享一篇有观点和思考的技术文章 Algo ...

  6. 【CTF WEB】命令执行

    命令执行 找到题目中的KEY KEY为八位随机字符数字,例如key:1234qwer.提交1234qwer 即可. 漏洞代码 <?php system("ping -c 2 " ...

  7. python3之SQLAlchemy

    1.SQLAlchemy介绍 SQLAlchemy是Python SQL工具包和对象关系映射器,为应用程序开发人员提供了SQL的全部功能和灵活性. 它提供了一整套众所周知的企业级持久性模式,专为高效和 ...

  8. tomcat启动报错:Injection of autowired dependencies failed

    tomcat启动报错:Injectjion of autowired dependencies failed 环境: 操作系统:centos6.5 tomcat: 7.0.52 jdk:openjdk ...

  9. 大数据统计分析平台之一、Kafka单机搭建

    1.zookeeper搭建 Kafka集群依赖zookeeper,需要提前搭建好zookeeper 单机模式(7步)(集群模式进阶请移步:http://blog.51cto.com/nileader/ ...

  10. ddmlib问题总结——同步获取设备信息

    通过IDevice.getProperty(String name)得到响应的设备属性.在实际的使用过程中发现,我的manufacturer总是获取不到,为null(获取代码如下),而剩下的属性都可以 ...