TOJ 1840 Jack Straws
Description
In the game of Jack Straws, a number of plastic or wooden "straws" are dumped on the table and players try to remove them one-by-one without disturbing the other straws. Here, we are only concerned with if various pairs of straws are connected by a path of touching straws. You will be given a list of the endpoints for some straws (as if they were dumped on a large piece of graph paper) and then will be asked if various pairs of straws are connected. Note that touching is connecting, but also two straws can be connected indirectly via other connected straws.
Input
Input consist multiple case,each case consists of multiple lines. The first line will be an integer n (1 < n < 13) giving the number of straws on the table. Each of the next n lines contain 4 positive integers,x1,y1,x2 and y2, giving the coordinates, (x1,y1),(x2,y2) of the endpoints of a single straw. All coordinates will be less than 100. (Note that the straws will be of varying lengths.) The first straw entered will be known as straw #1, the second as straw #2, and so on. The remaining lines of the current case(except for the final line) will each contain two positive integers, a and b, both between 1 and n, inclusive. You are to determine if straw a can be connected to straw b. When a = 0 = b, the current case is terminated.
When n=0,the input is terminated.
There will be no illegal input and there are no zero-length straws.
Output
You should generate a line of output for each line containing a pair a and b, except the final line where a = 0 = b. The line should say simply "CONNECTED", if straw a is connected to straw b, or "NOT CONNECTED", if straw a is not connected to straw b. For our purposes, a straw is considered connected to itself.
Sample Input
7
1 6 3 3
4 6 4 9
4 5 6 7
1 4 3 5
3 5 5 5
5 2 6 3
5 4 7 2
1 4
1 6
3 3
6 7
2 3
1 3
0 0
2
0 2 0 0
0 0 0 1
1 1
2 2
1 2
0 0
0
Sample Output
CONNECTED
NOT CONNECTED
CONNECTED
CONNECTED
NOT CONNECTED
CONNECTED
CONNECTED
CONNECTED
CONNECTED
Source
East Central North America 1994
判断两线段是否相交,直接上模板。
先用并查集预处理好两线段相交,最后判断两两是否在同一个集合就可以了。
1 #include <stdio.h> 2 #define eps 1e-8 3 #define zero(x) (((x)>0?(x):-(x))<eps) 4 5 int cnt; 6 int lis[20]; 7 struct Point{ 8 double x; 9 double y; 10 }; 11 struct Line{ 12 Point a1; 13 Point a2; 14 }ll[20]; 15 16 double xmult(Point p1, Point p2, Point p0){ 17 return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y); 18 } 19 20 int dots_inline(Point p1, Point p2, Point p3){ 21 return zero(xmult(p1,p2,p3)); 22 } 23 24 int dot_online_in(Point p,Line l){ 25 return zero(xmult(p,l.a1,l.a2))&&(l.a1.x-p.x)*(l.a2.x-p.x)<eps 26 &&(l.a1.y-p.y)*(l.a2.y-p.y)<eps; 27 } 28 29 int same_side(Point p1,Point p2,Line l){ 30 return xmult(l.a1,p1,l.a2)*xmult(l.a1,p2,l.a2)>eps; 31 } 32 33 int intersect_in(Line u, Line v){ 34 if(!dots_inline(u.a1,u.a2,v.a1)||!dots_inline(u.a1,u.a2,v.a1)){ 35 return !same_side(u.a1,u.a2,v)&&!same_side(v.a1,v.a2,u); 36 } 37 return dot_online_in(u.a1,v)||dot_online_in(u.a2,v)|| 38 dot_online_in(v.a1,u)||dot_online_in(v.a1,u); 39 } 40 41 void set(){ 42 for(int i=1; i<=cnt; i++){ 43 lis[i]=i; 44 } 45 } 46 47 int find(int u){ 48 while(lis[u]!=u){ 49 u=lis[u]; 50 } 51 return u; 52 } 53 54 int main(int argc, char *argv[]) 55 { 56 while( scanf("%d",&cnt)!=EOF && cnt ){ 57 for(int i=1; i<=cnt; i++){ 58 scanf("%lf %lf %lf %lf",&ll[i].a1.x ,&ll[i].a1.y ,&ll[i].a2.x ,&ll[i].a2.y); 59 } 60 set(); 61 for(int i=1; i<=cnt; i++){ 62 for(int j=i+1; j<=cnt; j++){ 63 if(intersect_in(ll[i], ll[j])){ 64 int tx=find(i); 65 int ty=find(j); 66 if(tx!=ty) 67 lis[tx]=ty; 68 } 69 } 70 } 71 int u,v; 72 while( scanf("%d %d" ,&u ,&v)!=EOF ){ 73 if(u==0&& v==0)break; 74 if(find(u)==find(v)) 75 puts("CONNECTED"); 76 else 77 puts("NOT CONNECTED"); 78 } 79 } 80 return 0; 81 }