hdu 1558 Segment set(判断线段有交点+并查集)

来源:互联网 发布:mysql中的级联删除 编辑:程序博客网 时间:2024/06/03 18:59

Segment set

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3703    Accepted Submission(s): 1394


Problem Description
A segment and all segments which are connected with it compose a segment set. The size of a segment set is the number of segments in it. The problem is to find the size of some segment set.

 

Input
In the first line there is an integer t - the number of test case. For each test case in first line there is an integer n (n<=1000) - the number of commands.

There are two different commands described in different format shown below:

P x1 y1 x2 y2 - paint a segment whose coordinates of the two endpoints are (x1,y1),(x2,y2).
Q k - query the size of the segment set which contains the k-th segment.

k is between 1 and the number of segments in the moment. There is no segment in the plane at first, so the first command is always a P-command.
 

Output
For each Q-command, output the answer. There is a blank line between test cases.
 

Sample Input
110P 1.00 1.00 4.00 2.00P 1.00 -2.00 8.00 4.00Q 1P 2.00 3.00 3.00 1.00Q 1Q 3P 1.00 4.00 8.00 2.00Q 2P 3.00 3.00 6.00 -2.00Q 5
 

Sample Output
12225
 

Author
LL
 

Source
HDU 2006-12 Programming Contest
题目分析:每次新添加的直线和前面的直线判断是否相交,利用并查集合并集合
 
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <cmath>#define eps 1e-10#define MAX 1007using namespace std;struct Point{    double x,y;    Point():x(0),y(0){}    Point ( double a , double b )        :x(a),y(b){}};struct Line{    Point u,v;}l[MAX];int fa[MAX];int num[MAX];int sig ( double d ){    return fabs(d)<eps?0:d<0?-1:1;}double cross ( Point& o , Point& a , Point& b ){    return (a.x-o.x)*(b.y-o.y)-(b.x-o.x)*(a.y-o.y);}bool OnSegment ( Point&a , Point& b , Point& c ){    return c.x>=min(a.x,b.x)&& c.x <= max ( a.x,b.x)        && c.y >= min(a.y,b.y)&&c.y<=max(a.y,b.y);}bool segCross ( Point& a , Point& b , Point& c , Point& d ){    double s1,s2;    int d1,d2,d3,d4;    d1 = sig( s1 = cross(a,b,c) );    d2 = sig( s2 = cross(a,b,d) );    d3 = sig( cross(c,d,a) );    d4 = sig( cross(c,d,b) );    if ( (d1^d2)==-2 && (d3^d4)==-2) return 1;    else if ( d1 == 0 && OnSegment( a , b , c ) ) return 1;    else if ( d2 == 0 && OnSegment( a , b , d ) ) return 1;    else if ( d3 == 0 && OnSegment( c , d , a ) ) return 1;    else if ( d4 == 0 && OnSegment( c , d , b ) ) return 1;    return 0;}int cnt,t,n;char s[5];void init ( int n ){    for ( int i = 0 ; i <= n ; i++ )        fa[i] = i;}int find ( int x ){    return x==fa[x]?x:fa[x]=find(fa[x]);}int main ( ){    double a,b,c,d;    int e;    scanf ( "%d" , &t );    while ( t-- )    {        cnt = 1;        scanf ( "%d" , &n );        init( n );        for ( int i = 0 ; i < n ; i++ )        {            scanf ( "%s" , s );            if ( s[0] == 'P' )            {                scanf ( "%lf%lf%lf%lf" , &a,&b,&c,&d );                l[cnt].u.x = a;                l[cnt].u.y = b;                l[cnt].v.x = c;                l[cnt].v.y = d;                num[cnt] = 1;                for ( int i = 1 ; i < cnt ; i++ )                    if ( segCross ( l[i].u , l[i].v , l[cnt].u , l[cnt].v ) )                    {                        int x = find(i);                        int y = find(cnt);                        if ( x == y ) continue;                        fa[y] = x;                        num[x]+=num[y];                    }                cnt++;            }            else            {                scanf ( "%d" , &e );                printf ( "%d\n" , num[find(e)] );            }        }        if ( t ) puts ("");    }}


0 0