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+20, M = 1e2+10, mod = 1e9+7, inf = 1e9+1000; typedef long long ll; const double INF = 1E200; const double EP = 1E-10; const int MAXV = 30000; const double PI = 3.14159265; struct POINT { double x; double y; POINT(double a=0, double b=0) { 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 0;if(x < 0) return -1;else return 1;} 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)) <= 0 && sgn((l1.s-l2.e) ^ (l2.s - l2.e))*sgn((l1.e-l2.e) ^ (l2.s-l2.e)) <= 0; } bool onseg(POINT P , LINE L) { return sgn((L.s-P)^(L.e-P)) == 0 && sgn((P.x - L.s.x) * (P.x - L.e.x)) <= 0 && sgn((P.y - L.s.y) * (P.y - L.e.y)) <= 0; } //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=1;i<=n;i++) fa[i] = i; for(int i=1;i<=n;i++) { double x,y; scanf("%lf%lf",&x,&y); p[i] = (POINT) {x,y}; } for(int i=1;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=1;i<=n;i++) { for(int j=1;j<=m;j++) { if(onseg(p[i],dg[j])) { unions(i,posa[j]); unions(i,posb[j]); } } } POINT pp ;//线段交点 for(int i=1;i<=m;i++) { for(int j=1;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 = 1; int fi = finds(1); for(int i=1;i<=n;i++) { if(finds(i)!=fi) { puts("NO");return 0; } } puts("YES"); }