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

时间:2022-11-04 15:11:49
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(并检查集合 &plus; 判刑线相交)

    意甲冠军: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 线段树按时间分治&plus;并查集

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

  5. poj 1127&colon;Jack Straws(判断两线段相交 &plus; 并查集)

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

  6. BZOJ&lowbar;4025&lowbar;二分图&lowbar;线段树按时间分治&plus;并查集

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

  7. hdu 1558 线段相交&plus;并查集

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

  8. 判断线段相交&lpar;hdu1558 Segment set 线段相交&plus;并查集&rpar;

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

  9. hdu 1558 &lpar;线段相交&plus;并查集&rpar; Segment set

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

随机推荐

  1. Lesson 15 Good news

    Text The secretary told me that Mr. Harmsworth would see me. I felt very nervous when I went into hi ...

  2. 【开源】OSharp3&period;0框架解说系列:新版本说明及新功能规划预览

    OSharp是什么? OSharp是个快速开发框架,但不是一个大而全的包罗万象的框架,严格的说,OSharp中什么都没有实现.与其他大而全的框架最大的不同点,就是OSharp只做抽象封装,不做实现.依 ...

  3. Hbase之更新单条数据

    import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; impo ...

  4. intent&period;addFlags&lpar;Intent&period;FLAG&lowbar;ACTIVITY&lowbar;NO&lowbar;HISTORY&rpar;&semi;

    ActivityA到ActivityBintent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);//ActivityB不加入后退栈android:noHisto ...

  5. HW3&period;21

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  6. poj1573&amp&semi;amp&semi;&amp&semi;amp&semi;hdu1035 Robot Motion(模拟)

    转载请注明出处:http://blog.csdn.net/u012860063? viewmode=contents 题目链接: HDU:pid=1035">http://acm.hd ...

  7. 如何使用 Weave 网络?- 每天5分钟玩转 Docker 容器技术(63)

    weave 是 Weaveworks 开发的容器网络解决方案.weave 创建的虚拟网络可以将部署在多个主机上的容器连接起来.对容器来说,weave 就像一个巨大的以太网交换机,所有容器都被接入这个交 ...

  8. hdu 1542 线段树 求矩形并

    Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  9. Django搭建博客网站&lpar;四&rpar;

    Django搭建博客网站(四) 最后一篇主要讲讲在后台文章编辑加入markdown,已经在文章详情页对markdown的解析. Django搭建博客网站(一) Django搭建博客网站(二) Djan ...

  10. UnzipUtil

    public class UnzipUtil { private static final Logger logger = LoggerFactory.getLogger(CopyFileUtil.c ...