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

本文介绍了一种基于并查集的数据结构算法来解决段集查询问题的方法。通过判断新增直线与已有直线是否相交,并利用并查集进行集合合并,实现对段集大小的高效查询。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
  
1 10 P 1.00 1.00 4.00 2.00 P 1.00 -2.00 8.00 4.00 Q 1 P 2.00 3.00 3.00 1.00 Q 1 Q 3 P 1.00 4.00 8.00 2.00 Q 2 P 3.00 3.00 6.00 -2.00 Q 5
 

Sample Output
  
1 2 2 2 5
 

Author
LL
 

Source
题目分析:每次新添加的直线和前面的直线判断是否相交,利用并查集合并集合
 
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#define eps 1e-10
#define MAX 1007

using 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 ("");
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值