标签:des style blog http io color ar os for
Description
Input
Output
Sample Input
input | output |
---|---|
4 2 0 0 1 0 1 1 0 1 1 3 4 2 |
YES |
4 3 0 0 1 0 1 1 0 1 1 2 2 1 3 4 |
NO |
3 2 0 0 1 0 1 1 1 3 3 2 |
YES |
这道题主要是判相交,只要相交就把它压入并查集,一开始我是用了cnt去记录已经相交的节点,后来发现不行,因为新加如的一条线如果加进去了,它的另外一个端点也会加入,导致cnt记录的数值不准。于是用了另外一个数组c[i]去记录以i为根的所有子节点的个数。
在判断相交这里,一开始没有注意到新加入一条线段时,应该判断所有点是否在该线段上,如果端点在该线段上,则把它加入,加了OnSegment()判断之后就AC了。
#include<cstdio> #include<cmath> #include<iostream> using namespace std; #define maxn 205 struct point { double x,y; point(double x = 0,double y = 0):x(x),y(y){} }p[maxn]; struct Line { point a,b; int pos1,pos2; Line(){} Line(point x,point y,int ppos1,int ppos2){ a = x; b = y; pos1 = ppos1; pos2 = ppos2;} }line[maxn]; int n,m,cnt; int par[maxn]; int c[maxn]; typedef point Vector; Vector operator +(Vector A,Vector B){ return Vector(A.x+B.x, A.y+B.y); } Vector operator -(Vector A,Vector B) { return Vector(A.x-B.x,A.y-B.y); } Vector operator *(Vector A,double p) { return Vector(A.x*p,A.y*p); } Vector operator /(Vector A,double p){ return Vector(A.x/p,A.y/p); } const double eps = 1e-10; int dcmp(double x) { if(fabs(x) < eps) return 0; else return x < 0? -1:1; } bool operator == (const point &a,const point &b) { return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0; } double dot(Vector A,Vector B){ return A.x*B.x + A.y*B.y; } double cross(Vector A,Vector B){ return A.x*B.y-A.y*B.x; } bool OnSegment(point p,Line l) { return dcmp(cross(l.a-p,l.b-p)) == 0 && dcmp(dot(l.a-p,l.b-p)) < 0; } bool SegmentProperIntersection(Line l1,Line l2) { point a1 = l1.a; point a2 = l1.b; point b1 = l2.a; point b2 = l2.b; double c1 = cross(a2-a1,b1-a1); double c2 = cross(a2-a1,b2-a1); double c3 = cross(b2-b1,a1-b1); double c4 = cross(b2-b1,a2-b1); return dcmp(c1)*dcmp(c2) < 0 && dcmp(c3)*dcmp(c4) < 0; } void init() { for(int i = 1; i <= n; i++) c[i] = 1; for(int i = 0; i < maxn;i++) par[i] = i; } int Find(int x) { if(par[x] != x) { return par[x]=Find(par[x]); } else return x; } void Merge(int a,int b) { int t1 = Find(a); int t2 = Find(b); if(t1 != t2) { par[t2] = t1; c[t1] += c[t2]; //printf("%d %d merge\n",a,b); //return 1; } //return 0; } void input() { int x,y; for(int i = 1; i <= n; i++) { double x,y; scanf("%lf%lf",&x,&y); p[i] = point(x,y); } for(int i = 0; i < m; i++) { scanf("%d%d",&x,&y); line[i] = Line(p[x],p[y],x,y); for(int j = 1; j <= n; j++) { if(OnSegment(p[j],line[i])) Merge(j,x); } Merge(x,y); } } void deal() { for(int i = 0; i < m; i++) { for(int j = i + 1; j < m; j++) { if(SegmentProperIntersection(line[i],line[j])) { Merge(line[j].pos1,line[i].pos1); //Merge(line[j].pos2,line[i].pos1); } } } } int main() { //freopen("input.txt","r",stdin); while(scanf("%d%d",&n,&m) == 2) { init(); input(); deal(); if(c[Find(1)] == n) printf("YES\n"); else printf("NO\n"); } return 0; }
标签:des style blog http io color ar os for
原文地址:http://www.cnblogs.com/imLPT/p/4070867.html