The question is to give a line segment in sequence. If you ask the line segment given by the K, it will output the number of all line segments that are directly and indirectly intersecting with the K line segment (note that it contains itself, so num is initialized to 1)
Intersection of typical judgments, and use and query Sets
My code has wa for a long time, that is, in the function, if the value of the parameter is exchanged, note that if the parameter is of the pointer type, as a result, the data in the external function that calls this function is also exchanged to affect the program results (see the comment)
My code:
# Include <stdio. h> # include <string. h> int father [1001]; int num [1001]; double line [1001] [2] [2]; int N, Q; double min (double A, double B) {return a <B? A: B;} double max (double A, double B) {return A> B? A: B;} bool equal (double A, double B) {return a = B;} bool between (double A, double B, double C) {if (a <= max (B, c) & A> = min (B, c) return 1; return 0 ;} int swap (double line1 [] [2], double line2 [] [2]) {int I, j; double temp; for (I = 0; I <2; I ++) for (j = 0; j <2; j ++) {temp = line1 [I] [J]; line1 [I] [J] = line2 [I] [J]; line2 [I] [J] = temp;} return 0 ;} bool cut (double line1 [] [2], double line2 [] [2]) // judge the intersection {doubl E K1, K2, B1, B2, X, Y; If (equal (line2 [0] [0], line2 [1] [0]) // if there is no slope, let the function enter the if statement below, but this exchange changes the sequence number of the line segment in the global line {swap (line1, line2 ); // so I copied both line segments in Main.} If (equal (line1 [0] [0], line1 [1] [0]) // No slope condition {If (equal (line2 [0] [0], line2 [1] [0]) {If (equal (line1 [0] [0], line2 [0] [0]) & (between (line1 [0] [1], line2 [0] [1], line2 [1] [1]) | between (line1 [1] [1], line2 [0] [1], line2 [1] [1]) | between (line2 [0] [1], line 1 [0] [1], line1 [1] [1]) | between (line2 [1] [1], line1 [0] [1], line1 [1] [1]) return 1; else {return 0 ;}} else {k2 = (line2 [1] [1]-line2 [0] [1])/(line2 [1] [0]-line2 [0] [0]); b2 = line2 [1] [1]-K2 * line2 [1] [0]; If (! Between (line1 [0] [0], line2 [0] [0], line2 [1] [0]) return 0; y = k2 * line1 [0] [0] + B2; If (between (Y, line1 [0] [1], line1 [1] [1]) & between (Y, line2 [0] [1], line2 [1] [1]) return 1; elsereturn 0 ;}} k1 = (line1 [1] [1]-line1 [0] [1])/(line1 [1] [0]-line1 [0] [0]); b1 = line1 [1] [1]-K1 * line1 [1] [0]; k2 = (line2 [1] [1]-line2 [0] [1]) /(line2 [1] [0]-line2 [0] [0]); b2 = line2 [1] [1]-K2 * line2 [1] [0]; if (K1-k2 = 0 & & (Between (line1 [0] [0], line2 [0] [0], line2 [1] [0]) | between (line1 [1] [0], line2 [0] [0], line2 [1] [0]) | between (line2 [0] [0], line1 [0] [0], line1 [1] [0]) | between (line2 [1] [0], line1 [0] [0], line1 [1] [0]) return 1; X = (B2-B1)/(K1-K2); y = x * k1 + b1; If (between (Y, line1 [0] [1], line1 [1] [1]) & between (Y, line2 [0] [1], line2 [1] [1]) & between (X, line1 [0] [0], line1 [1] [0]) & between (x, line2 [0] [0], line 2 [1] [0]) return 1; return 0;} int Init () {int I; n = 0; for (I = 1; I <= 1000; I ++) {FATHER [I] = I; num [I] = 1;} return 0;} int find (INT X) {If (father [x] = x) return X; return father [x] = find (father [x]);} int uni (int A, int B) {int AFA = find (A), BFA = find (B); If (AFA = BFA) return 0; num [AFA] + = num [BFA]; father [BFA] = AFA; return 0;} int copy (double line1 [] [2], double line2 [] [2]) {int I, J; for (I = 0; I <2; I ++) For (j = 0; j <2; j ++) {line1 [I] [J] = line2 [I] [J];} return 0 ;} int main () {INT cases, I, K; char STR [2]; double temp1 [2] [2]; double temp2 [2] [2]; scanf ("% d", & cases); While (cases --) {scanf ("% d", & Q); Init (); While (Q --) {scanf ("% s", STR); If (STR [0] = 'P') {n ++; scanf ("% lf", & line [N] [0] [0], & line [N] [0] [1], & line [N] [1] [0], & line [N] [1] [1]); for (I = 1; I <n; I ++) {copy (temp1, line [N]); // copy to avoid switching copy (TEM P2, line [I]); // copy to avoid switching if (cut (temp1, temp2) Uni (I, n );}} else {scanf ("% d", & K); printf ("% d \ n", num [find (k)]) ;}} if (cases! = 0) printf ("\ n");} return 0 ;}
In the future, you must keep an eye on it, that is, when the function exchanges parameters.