F - 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

题意:

  给你n点

  给你m条直线

  问你所有点是否相连

题解:

  点在线段上、线段是否相交板子来判断

  吧相连的点加入集合

  最后判断所有点是否都在一个集合里边即可

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 5e4+, M = 1e2+, mod = 1e9+, inf = 1e9+;
typedef long long ll;
const double INF = 1E200;
const double EP = 1E-;
const int MAXV = ;
const double PI = 3.14159265;
struct POINT
{
double x;
double y;
POINT(double a=, double b=) { x=a; y=b;} //constructor
POINT operator - (const POINT &b) const {
return POINT(x - b.x , y - b.y);
}
double operator ^ (const POINT &b) const {
return x*b.y - y*b.x;
}
};
struct LINE
{
POINT s;
POINT e;
LINE(POINT a, POINT b) { s=a; e=b;}
LINE() { }
};
int sgn(double x) {if(fabs(x) < EP)return ;if(x < ) return -;else return ;}
bool inter(LINE l1,LINE l2) {
return
max(l1.s.x,l1.e.x) >= min(l2.s.x,l2.e.x) &&
max(l2.s.x,l2.e.x) >= min(l1.s.x,l1.e.x) &&
max(l1.s.y,l1.e.y) >= min(l2.s.y,l2.e.y) &&
max(l2.s.y,l2.e.y) >= min(l1.s.y,l1.e.y) &&
sgn((l2.s-l1.e) ^ (l1.s - l1.e))*sgn((l2.e-l1.e) ^ (l1.s-l1.e)) <= &&
sgn((l1.s-l2.e) ^ (l2.s - l2.e))*sgn((l1.e-l2.e) ^ (l2.s-l2.e)) <= ;
}
bool onseg(POINT P , LINE L) {
return
sgn((L.s-P)^(L.e-P)) == &&
sgn((P.x - L.s.x) * (P.x - L.e.x)) <= &&
sgn((P.y - L.s.y) * (P.y - L.e.y)) <= ;
}
//intersection
POINT p[N];
LINE dg[N];
int n,m,posa[N],posb[N],fa[N],cnt,vis[N]; int finds(int x) {return x==fa[x]?x:fa[x]=finds(fa[x]);}
void unions(int x,int y) {
int fx = finds(x);
int fy = finds(y);
if(fx != fy) fa[fx] = fy;
}
int main()
{
scanf("%d%d",&n,&m); for(int i=;i<=n;i++) fa[i] = i; 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",&posa[i],&posb[i]);
unions(posa[i],posb[i]);
dg[i] = (LINE) {p[posa[i]],p[posb[i]]};
}
//点在线段上
for(int i=;i<=n;i++) {
for(int j=;j<=m;j++) {
if(onseg(p[i],dg[j])) {
unions(i,posa[j]);
unions(i,posb[j]);
}
}
} POINT pp ;//线段交点
for(int i=;i<=m;i++) {
for(int j=;j<=m;j++) {
if(inter(dg[i],dg[j])) {
unions(posa[i],posa[j]);
unions(posa[i],posb[j]);
unions(posb[i],posa[j]);
unions(posb[i],posb[j]);
}
}
} int all = ;
int fi = finds();
for(int i=;i<=n;i++) {
if(finds(i)!=fi) {
puts("NO");return ;
}
}
puts("YES"); }

URAL 1966 Cycling Roads 点在线段上、线段是否相交、并查集的更多相关文章

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

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

  2. Ural 1966 Cycling Roads

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

  3. URAL 1966 Cycling Roads 计算几何

    Cycling Roads 题目连接: http://acm.hust.edu.cn/vjudge/contest/123332#problem/F Description When Vova was ...

  4. 【CF576E】Painting Edges 线段树按时间分治+并查集

    [CF576E]Painting Edges 题意:给你一张n个点,m条边的无向图,每条边是k种颜色中的一种,满足所有颜色相同的边内部形成一个二分图.有q个询问,每次询问给出a,b代表将编号为a的边染 ...

  5. poj 1127:Jack Straws(判断两线段相交 + 并查集)

    Jack Straws Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2911   Accepted: 1322 Descr ...

  6. BZOJ_4025_二分图_线段树按时间分治+并查集

    BZOJ_4025_二分图_线段树按时间分治+并查集 Description 神犇有一个n个节点的图.因为神犇是神犇,所以在T时间内一些边会出现后消失.神犇要求出每一时间段内这个图是否是二分图.这么简 ...

  7. hdu 1558 线段相交+并查集

    题意:要求相交的线段都要塞进同一个集合里 sol:并查集+判断线段相交即可.n很小所以n^2就可以水过 #include <iostream> #include <cmath> ...

  8. 判断线段相交(hdu1558 Segment set 线段相交+并查集)

    先说一下题目大意:给定一些线段,这些线段顺序编号,这时候如果两条线段相交,则把他们加入到一个集合中,问给定一个线段序号,求在此集合中有多少条线段. 这个题的难度在于怎么判断线段相交,判断玩相交之后就是 ...

  9. hdu 1558 (线段相交+并查集) Segment set

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1558 题意是在坐标系中,当输入P(注意是大写,我当开始就wa成了小写)的时候输入一条线段的起点坐标和终点坐 ...

随机推荐

  1. poj 1442

    一个排序的题目. 题意:给你m个数a[m],和n个数b[n]. 首先a[0]….a[b[0]]排序.输出第一个数. 然后a[0]….a[b[1]]排序.输出第二个数. 以此类推,直到输出第n个数. 思 ...

  2. Python之异常追踪模块:traceback

    正常时输出追踪信息: import traceback def stack(): print 'The python stack:' traceback.print_stack() from twis ...

  3. Appium+Robotframework实现Android应用的自动化测试-1:Appium在Windows中的安装

    让我们开始在Windows中开始安装Appium吧,Appium在OS X中的具体安装后面的文章会介绍. 另外,官网上说先要装Node.js,还要装Apache Ant和Apache Maven,Gi ...

  4. 【转】mysql的cardinality异常,导致索引不可用

    转自:http://ourmysql.com/archives/1343 前段时间,一大早上,就收到报警,警告php-fpm进程的数量超过阈值.最终发现是一条sql没用到索引,导致执行数据库查询慢了, ...

  5. MMO可见格子算法

    看注释吧,写的很清楚了 using System; using System.Collections.Generic; using System.Diagnostics; using System.L ...

  6. ios NSString 转 float的注意

    今天有一个字符串 “33.3”,用想用[valueString floatValue];得到33.3000这个值,结果得到了33.2999这个值,取前3位一个是33.3,一个是33.2,产生了错误,应 ...

  7. redis pub/sub 实战: 微信语音识别

    2015年5月22日 20:20:20 星期五 效果: 这边对微信说话,  浏览器端及时显示语音识别的文字 注意: 在连接socket.io时, 按下浏览器f12, 如果一直有请求不断的刷, 说明so ...

  8. MySQL 主键范围查找问题

    背景: 今天遇到一个主键范围查找的情况: id是主键,每次取10000.上面的这个查询id范围越往后面消耗的时间越久.通过id自增主键去查找数据应该不会出现这个现象的.以前都没有注意这个奇怪的现象,现 ...

  9. 配置IIS,Apache,PHP过程中遇到的一些问题

    下载了eclipse的最新版本,并且添加了PHP插件.为了支持多语言,决定采用UTF-8编码.但是在开发的过程中,发现代码的自动提示帮助信息显示的是乱码,PHP源文件及注释,均正常.在网上查了很多资料 ...

  10. 在java项目中使用AES256 CBC加密

    首先要注意一点,默认的JDK是不支持256位加密的,需要到Oracle官网下载加密增强文件(Java Cryptography Extension (JCE) Unlimited Strength J ...