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成了小写)的时候输入一条线段的起点坐标和终点坐 ...
随机推荐
- 【leetcode】Edit Distance
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
- ios coredata 无任何错误提示crash
最近写程序是遇到了一种情况,对coredata 操作时,有一定几率crash,crash时无任何说明,断点调试后发现,fetch出的对象的属性竟然和数据库中的不同,不知道什么情况下导致了context ...
- ios 音乐播放,音乐信息显示方法
下面的博客写的很清楚了 http://msching.github.io/blog/page/2/ 主要涉及AVAudioPlayer和下面这几个函数 MPNowPlayingInfoCenter.d ...
- [Linux]centOS7下RPM安装Perl
1.下载rpm依赖包,依照顺序安装. perl-parent-0.225-244.el7.noarch perl-HTTP-Tiny-0.033-3.el7.noarch perl-podla ...
- ABAP 行列稳定刷新语句
DATA stbl TYPE lvc_s_stbl. stbl-row = 'X'." 基于行的稳定刷新 stbl-col = 'X'." 基于列稳定刷新 ...
- nohup后台运行jar
nohup 用途:LINUX命令用法,不挂断地运行命令. 语法:nohup Command [ Arg ... ] [ & ] 描述:nohup 命令运行由 Command 参数和任何相关 ...
- 【leetcode】Single Number II (medium) ★ 自己没做出来....
Given an array of integers, every element appears three times except for one. Find that single one. ...
- 地图API文档
目录 腾讯地图API 2 1.API概览... 2 1.1 WebService API(官网注明是beta版本,可能不稳定,慎用):... 2 1.2 URL API:... 2 1.3 静态图AP ...
- 在R语言中无法设置CRAN镜像问题
很大的可能是因为使用的浏览器不是IE浏览器的问题,因为CRAN的镜像需要用IE浏览器来打开. 只需要按照下面设置即可: 1.打开IE-->设置-->Internet选项-->高级 2 ...
- Android Service 与 Thread 的区别
Ref:http://blog.csdn.net/jiangwei0910410003/article/details/17008687 1). Thread:Thread 是程序执行的最小单元,它是 ...