================
Cycling Roads
================
 

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

input output
4 2
0 0
1 0
1 1
0 1
1 3
4 2
YES
4 3
0 0
1 0
1 1
0 1
1 2
2 1
3 4
NO
3 2
0 0
1 0
1 1
1 3
3 2
YES

这道题主要是判相交,只要相交就把它压入并查集,一开始我是用了cnt去记录已经相交的节点,后来发现不行,因为新加如的一条线如果加进去了,它的另外一个端点也会加入,导致cnt记录的数值不准。于是用了另外一个数组c[i]去记录以i为根的所有子节点的个数。

在判断相交这里,一开始没有注意到新加入一条线段时,应该判断所有点是否在该线段上,如果端点在该线段上,则把它加入,加了OnSegment()判断之后就AC了。

#include<cstdio>
#include<cmath>
#include<iostream>
using namespace std;
#define maxn 205
struct point
{
double x,y;
point(double x = ,double y = ):x(x),y(y){}
}p[maxn]; struct Line
{
point a,b;
int pos1,pos2;
Line(){}
Line(point x,point y,int ppos1,int ppos2){ a = x; b = y; pos1 = ppos1; pos2 = ppos2;}
}line[maxn]; int n,m,cnt;
int par[maxn];
int c[maxn]; typedef point Vector;
Vector operator +(Vector A,Vector B){ return Vector(A.x+B.x, A.y+B.y); }
Vector operator -(Vector A,Vector B) { return Vector(A.x-B.x,A.y-B.y); }
Vector operator *(Vector A,double p) { return Vector(A.x*p,A.y*p); }
Vector operator /(Vector A,double p){ return Vector(A.x/p,A.y/p); }
const double eps = 1e-;
int dcmp(double x)
{
if(fabs(x) < eps) return ;
else return x < ? -:;
}
bool operator == (const point &a,const point &b)
{
return dcmp(a.x-b.x) == && dcmp(a.y-b.y) == ;
}
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 OnSegment(point p,Line l)
{
return dcmp(cross(l.a-p,l.b-p)) == && dcmp(dot(l.a-p,l.b-p)) < ;
}
bool SegmentProperIntersection(Line l1,Line l2)
{
point a1 = l1.a;
point a2 = l1.b;
point b1 = l2.a;
point b2 = l2.b;
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) < && dcmp(c3)*dcmp(c4) < ;
} void init()
{
for(int i = ; i <= n; i++)
c[i] = ;
for(int i = ; i < maxn;i++)
par[i] = i;
}
int Find(int x)
{
if(par[x] != x)
{
return par[x]=Find(par[x]);
}
else return x;
} void Merge(int a,int b)
{
int t1 = Find(a);
int t2 = Find(b);
if(t1 != t2)
{
par[t2] = t1;
c[t1] += c[t2];
//printf("%d %d merge\n",a,b);
//return 1;
}
//return 0;
} void input()
{
int x,y; for(int i = ; i <= n; i++)
{
double x,y;
scanf("%lf%lf",&x,&y);
p[i] = point(x,y);
}
for(int i = ; i < m; i++)
{
scanf("%d%d",&x,&y);
line[i] = Line(p[x],p[y],x,y);
for(int j = ; j <= n; j++)
{
if(OnSegment(p[j],line[i])) Merge(j,x);
}
Merge(x,y);
}
} void deal()
{
for(int i = ; i < m; i++)
{
for(int j = i + ; j < m; j++)
{
if(SegmentProperIntersection(line[i],line[j]))
{
Merge(line[j].pos1,line[i].pos1);
//Merge(line[j].pos2,line[i].pos1);
}
}
} } int main()
{
//freopen("input.txt","r",stdin);
while(scanf("%d%d",&n,&m) == )
{
init();
input();
deal();
if(c[Find()] == n) printf("YES\n");
else printf("NO\n");
} return ;
}

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 题目连接: http://acm.hust.edu.cn/vjudge/contest/123332#problem/F Description When Vova was ...

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

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

  4. Cycling

    Cycling Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  5. Ural 1004 Sightseeing Trip

    Sightseeing Trip Time Limit: 2000ms Memory Limit: 16384KB This problem will be judged on Ural. Origi ...

  6. poj 1251 Jungle Roads (最小生成树)

    poj   1251  Jungle Roads  (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...

  7. Jungle Roads[HDU1301]

    Jungle Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  8. POJ1947 Rebuilding Roads[树形背包]

    Rebuilding Roads Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 11495   Accepted: 5276 ...

  9. Constructing Roads——F

    F. Constructing Roads There are N villages, which are numbered from 1 to N, and you should build som ...

随机推荐

  1. 桥接模式和nat模式的区别

    桥接模式:VMware虚拟的系统就想局域网中独立的主机一样(有独立的IP)它可以访问网内任何一台机器 Nat模式:可以通过宿主机访问互联网(宿主机联网,虚拟机就能联网)它不能和本局域网中的其他主机进行 ...

  2. python jieba 库分词结合Wordcloud词云统计

    import jieba jieba.add_word("福军") jieba.add_word("少安") excludes={"一个", ...

  3. Python开发【初始篇】:Linux下安装Python3

    Linux系统默认自带python2.6的版本,这个版本被系统很多程序所依赖,所以建议不要轻易删除,除非你能解决其他程序的依赖问题.如果使用最新的Python3需要进行编译安装源码包,这样就对系统默认 ...

  4. python day03_ 文件处理

    一.文件操作的基本流程 计算机操作文件的过程 #1. 打开文件,得到文件句柄并赋值给一个变量 #2. 通过句柄对文件进行操作 #3. 关闭文件 1.文件的打开过程 # f被程序持有,文件被操作系统持有 ...

  5. linux下搭建Jenkins环境

    前提:Tomcat.jdk已安装并配置成功,具体安装和配置可参考我的其他随笔,在此不再详述 1.官网下载Jenkins最新war包,jenkins.war 2.进入Tomcat安装目录,创建Jenki ...

  6. Linux服务器上监控网络带宽的18个常用命令 zz

    Linux服务器上监控网络带宽的18个常用命令 本文介绍了一些可以用来监控网络使用情况的Linux命令行工具.这些工具可以监控通过网络接口传输的数据,并测量目前哪些数据所传输的速度.入站流量和出站流量 ...

  7. 选择文件,显示其路径在ListBox控件里

    private void btnSelect_Click(object sender, EventArgs e)        {            lbxFiles.Items.Clear(); ...

  8. odoo8 元素简介

    一:模型module: 1. 字段类型 (1)可控字段: fileds.char() fileds.Boolean() fileds.Date() (2)保留字段:(系统自动生成) id (Id) t ...

  9. PHP配置文件详解php.ini

    [PHP] ; PHP还是一个不断发展的工具,其功能还在不断地删减 ; 而php.ini的设置更改可以反映出相当的变化, ; 在使用新的PHP版本前,研究一下php.ini会有好处的 ;;;;;;;; ...

  10. 迭代加深搜索 C++解题报告 :[SCOI2005]骑士精神

    题目 此题根据题目可知是迭代加深搜索. 首先应该枚举空格的位置,让空格像一个马一样移动. 但迭代加深搜索之后时间复杂度还是非常的高,根本过不了题. 感觉也想不出什么减枝,于是便要用到了乐观估计函数(O ...