Title: http://acm.hdu.edu.cn/showproblem.php?pid=1558
Test instructions is in the coordinate system, when the input p (note is uppercase, I when the beginning of WA became lowercase) when the input line of the start and end coordinates, when the input Q input n, and then the output line with the nth segment intersection of how many bars
First of all, to determine whether the segment intersection, in the introduction of the algorithm p577
Segment A (x1,y1)-B (X2,y2), where the linear L1 equation is F1 (x, y) = 0;
Segment C (x3,y3)-D (X4,y4), where the linear L2 equation is F2 (x, y) = 0;
How to determine the intersection of two segments: (A, b on both sides of the line L2) and (c,d on both sides of the line L1).
The expression can be expressed in mathematical expressions: F2 (x1,y1) *f2 (x2,y2) >= 0 and F1 (x3,y3) *f1 (x4,y4) >= 0; Equal to 0 means that it happens to be straight.
Then it's the base of the set and the number of segments in a set and in the set.
1#include <cstdio>2 using namespacestd;3 intfather[1001],num[1001];4 voidgive ()5 {6 for(intI=1; i<= +; i++)7 {8father[i]=i;9num[i]=1;Ten } One } A int_find (intx) - { - if(father[x]==x)returnFather[x]; thefather[x]=_find (father[x]); - returnFather[x]; - } - Doublex1[1001],x2[1001],y1[1001],y2[1001]; + Doublea[1001],b[1001]; - intMain () + { A intt,n,sx,sy,k,i,q; at Charop; -scanf"%d",&t); - while(t--) - { -scanf"%d",&n); -Give (); k=1; in while(n--) - { toscanf"%c",&op); + if(op=='P') - { thescanf"%lf%lf%lf%lf",&x1[k],&y1[k],&x2[k],&y2[k]); *A[k]= (Y2[k]-y1[k])/(x2[k]-x1[k]); $B[k]= (X2[k]*y1[k]-x1[k]*y2[k])/(x2[k]-x1[k]);Panax Notoginseng for(i=1; i<k;i++) - { the intt1=0, t2=0; + if(! ((A[k]*x1[i]-y1[i]+b[k]) * (A[k]*x2[i]-y2[i]+b[k]) >0)) At1=1; the if(! ((A[i]*x1[k]-y1[k]+b[i]) * (A[i]*x2[k]-y2[k]+b[i]) >0)) +T2=1; - if(t1==1&&t2==1) $ { $sx=_find (k); -sy=_find (i); - if(sx!=Sy) { thefather[sx]=Sy; -num[sy]+=NUM[SX];Wuyi } the } - } Wuk++; - } About Else $ { -scanf"%d",&q); -printf"%d\n", Num[_find (q)]); - } A } + if(t) printf ("\ n"); the } - return 0; $}
HDU 1558 (segment intersection + and check set) Segment set