您的位置:首页 > 其它

HDU_1558_SegmentSet

2015-08-11 10:54 344 查看


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

HDU 2006-12 Programming Contest

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)
return n;
int t=senc
;
return senc
=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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: