HDU_1558_SegmentSet

Segment set

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


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
 

Recommend
LL

线段相交+并查集问题

问题问的是集合中的元素个数

因此合并操作的时候也要合并元素个数

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

const int MN=1005;
const double AC=1e-3;
char con[2];
double se[MN][4];           //输入的直线
int senc[MN];               //直线的几何关系
int sen[MN];                //集合中的直线个数

int find(int n)             //查找
{
    if(senc[n]==n)
        return n;
    int t=senc[n];
    return senc[n]=find(t);
}

double dt(double v1,double v2,double v3,double v4)       //行列式
{
    return v1*v4-v2*v3;
}

bool isx(int i,int j)                                      //判断相交
{
    double t1=dt(se[i][2]-se[i][0],se[j][0]-se[j][2],se[i][3]-se[i][1],se[j][1]-se[j][3]);
    if(t1<=AC&&t1>=-AC)
        return 0;
    double t2=dt(se[j][0]-se[i][0],se[j][0]-se[j][2],se[j][1]-se[i][1],se[j][1]-se[j][3])/t1;
    if(t2>1||t2<0)
        return 0;
    double t3=dt(se[i][2]-se[i][0],se[j][0]-se[i][0],se[i][3]-se[i][1],se[j][1]-se[i][1])/t1;
    if(t3>1||t3<0)
        return 0;
    return 1;
}

void mer(int i,int j)
{
    //cout<<i<<" "<<j<<endl;
    int t1=find(i);
    int t2=find(j);
    if(t1==t2)
        return;
    sen[t2]=sen[t1]+sen[t2];   //合并元素个数
    senc[t1]=t2;
    //cout<<t2<<" "<<sen[t2]<<endl;
}

int main()
{
    int t;
    int n;
    int nn;
    scanf("%d",&t);
    while(t--)
    {
        int p=1;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)         //重置
        {
            senc[i]=i;
            sen[i]=1;
        }
        for(int i=1;i<=n;i++)
        {
            scanf("%s",con);
            if(con[0]=='Q')
            {
                scanf("%d",&nn);
                printf("%d\n",sen[find(nn)]);
            }
            else
            {
                scanf("%lf%lf%lf%lf",&se[p][0],&se[p][1],&se[p][2],&se[p][3]);

                for(int i=1;i<p;i++)
                {
                    if(isx(i,p))
                        mer(i,p);
                }
                p++;
            }
        }
        if(t)
            printf("\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值