URAL 1966 Cycling Roads 点在线段上、线段是否相交、并查集
Description
Input
Output
Sample Input
input | output |
---|---|
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 点在线段上、线段是否相交、并查集的更多相关文章
- URAL - 1966 - Cycling Roads(并检查集合 + 判刑线相交)
意甲冠军:n 积分,m 边缘(1 ≤ m < n ≤ 200),问:是否所有的点连接(两个边相交.该 4 点连接). 主题链接:http://acm.timus.ru/problem.aspx? ...
- 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 ...
- 【CF576E】Painting Edges 线段树按时间分治+并查集
[CF576E]Painting Edges 题意:给你一张n个点,m条边的无向图,每条边是k种颜色中的一种,满足所有颜色相同的边内部形成一个二分图.有q个询问,每次询问给出a,b代表将编号为a的边染 ...
- poj 1127:Jack Straws(判断两线段相交 + 并查集)
Jack Straws Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2911 Accepted: 1322 Descr ...
- BZOJ_4025_二分图_线段树按时间分治+并查集
BZOJ_4025_二分图_线段树按时间分治+并查集 Description 神犇有一个n个节点的图.因为神犇是神犇,所以在T时间内一些边会出现后消失.神犇要求出每一时间段内这个图是否是二分图.这么简 ...
- hdu 1558 线段相交+并查集
题意:要求相交的线段都要塞进同一个集合里 sol:并查集+判断线段相交即可.n很小所以n^2就可以水过 #include <iostream> #include <cmath> ...
- 判断线段相交(hdu1558 Segment set 线段相交+并查集)
先说一下题目大意:给定一些线段,这些线段顺序编号,这时候如果两条线段相交,则把他们加入到一个集合中,问给定一个线段序号,求在此集合中有多少条线段. 这个题的难度在于怎么判断线段相交,判断玩相交之后就是 ...
- hdu 1558 (线段相交+并查集) Segment set
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1558 题意是在坐标系中,当输入P(注意是大写,我当开始就wa成了小写)的时候输入一条线段的起点坐标和终点坐 ...
随机推荐
- ios NSString 转 float的注意
今天有一个字符串 “33.3”,用想用[valueString floatValue];得到33.3000这个值,结果得到了33.2999这个值,取前3位一个是33.3,一个是33.2,产生了错误,应 ...
- 细看INNODB数据落盘
本文来自:沃趣科技 http://www.woqutech.com/?p=1459 1. 概述 前面很多大侠都分享过MySQL的InnoDB存储引擎将数据刷新的各种情况.我们这篇文章从InnoDB往 ...
- [转] Android利用tcpdump抓包
原文链接:http://mysuperbaby.iteye.com/blog/902201 Android利用tcpdump抓包 博客分类: Android AndroidAccessGoHTML ...
- css实现图片闪光效果
1.这个效果是看到京东商城上的一个下效果,原本的思路是 用js控制一个图片在某张需要闪光的图片上重复出现,但是 网上找了一些资料,竟然是用css写的,真是太帅了!!! 2.原理:在需要闪光的图片前添加 ...
- C#字符串的四舍五入
Round(Decimal) Round(Double) Round(Decimal, Int32) Round(Decimal, MidpointRounding) Round(Double, In ...
- ajax+json+Struts2实现list传递实例讲解
由于学习需要,需要通过ajax来获取后台的List集合里面的值.由于前面没有接触过,所以今天就来研究下了. 一.首先需要下载JSON依赖的jar包.它主要是依赖如下: json-lib-2.2.2-j ...
- 用 MyEclipse 开发 Spring 入门操作
何为Spring Spring框架是一个轻量级的控制反转(IOC)技术和面向切面编程(AOP)技术的容器框架,利用Spring框架可以实现对象的生命周期管理和分离应用系统中的业务逻辑组件和通用的技术服 ...
- swift枚举
以下是指南针四个方向的一个例子: enum CompassPoint { case North case South case East case West } 多个成员值可以出现在同一行上,用 ...
- Google earth
装了Google earth之后,需要联网,才能放缩看不同精细程度的场景,除非你之前看过,在你自己的缓存中有存储. Google earth有自己的server,我们交互化的时候,server传数据给 ...
- Ubuntu gcc编译报错:format ‘%llu’ expects argument of type ‘long long unsigned int’, but argument 2 has type ‘__time_t’ [-Wformat=]
平时用的都是Centos系统,今天偶然在Ubuntu下编译了一次代码,发现报错了: 源码: #include <stdio.h> #include <sys/time.h> # ...