hdu 1558 Segment set (并查集)

Segment set

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 3907    Accepted Submission(s): 1471

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

题目大意:在一个平面直角坐标系里面,通过P操作不断的加入线段,如果两个线段有相交,就表明他们是一个集合里面的。Q操作询问当前情况下第k条线段所在的集合里面有几条线段。

并查集的题目,但是我觉得主要考几何。我开始可以想到,通过判断两条线段是否有交点,如果有就放在一个集合里面。这么想的确很简单,但是做起来真的十分麻烦。。

如果对于两条线段,可以通过简单计算得到两者的交点x0=(b2-b1)/(k1-k2),还有y0。那么我只要判断x0,y0是否在线段相交的地方即可。但是还要注意,这个交点是从k1,k2得到的。所以如果k1,k2不存在,又要分情况讨论。

以下是我的代码,感觉好像还有遗漏的地方,虽然的确是AC了。

#include<stdio.h>
#include<string.h>
int p[10000],sum[10000];
double x1[1005],x2[1005],y1[1005],y2[1005];
void init(int x)
{
    int i;
    for(i=0;i<=x;i++)
    p[i]=i;
    for(i=0;i<=x;i++)
    sum[i]=1;
}
int findroot(int x)
{
    int r=x;
    while(r!=p[r])
    r=p[r];
    int i,j;
    i=x;
    while(i!=r)
    {
        j=p[i];
        p[i]=r;
        i=j;
    }
    return r;
}
void merge(int x,int y)
{
    int fx=findroot(x);
    int fy=findroot(y);
    if(fx!=fy){
        p[fx]=fy;
        sum[fy]+=sum[fx];
            }
}
double jiaodian(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4)
{

    if(x1==x2&&x3!=x4){             //k1不存在,k2存在
         double k2=(y3-y4)/(x3-x4);
        double y=k2*(x1-x3)+y3;
        if((y>=y1&&y<=y2)||(y>=y2&&y<=y1))return 1;
        else return 0;
    }
   else if(x3==x4&&x1!=x2){           //k2不存在,k1存在
        double k1=(y1-y2)/(x1-x2);
        double y=k1*(x3-x1)+y1;
        if((y>=y3&&y<=y4)||(y>=y4&&y<=y3))return 1;
        else return 0;
    }
   else if(x1==x2&&x3==x4){
        if(x1==x3&&((y1>=y3&&y1<=y4)||(y1>=y4&&y1<=y3)||(y2>=y4&&y2<=y3)||(y2>=y3&&y2<=y4)))return 1;
        else return 0;
    }
    double k1=(y1-y2)/(x1-x2);
     double k2=(y3-y4)/(x3-x4);
    double b1=(x1*y2-x2*y1)/(x1-x2);
    double b2=(x3*y4-x4*y3)/(x3-x4);
    double x=(b2-b1)/(k1-k2);
    double y=k1*(x-x1)+y1;
    if(((x>=x1&&x<=x2)||(x>=x2&&x<=x1))&&((y>=y1&&y<=y2)||(y>=y2&&y<=y1))&&
    ((x>=x3&&x<=x4)||(x>=x4&&x<=x3))||((y>=y3&&y<=y4)&&(y>=y4&&y<=y3)))return 1;
     return 0;
}
void isconnect(int x)
{
    int i;
    for(i=1;i<=x;i++)
  {

      if(jiaodian(x1[i],y1[i],x2[i],y2[i],x1[x],y1[x],x2[x],y2[x])){merge(i,x);}
  }
  return ;
}
int main()
{
    int t,n,i,j,k,m,cnt,q;

    char c[10];
    scanf("%d",&t);
    while(t--)
    {
        q=1;
        scanf("%d",&n);
        init(n);

        cnt=1;
        for(i=1;i<=n;i++)
        {
            scanf("%s",c);
            if(c[0]=='P')
            {
            scanf("%lf%lf%lf%lf",&x1[q],&y1[q],&x2[q],&y2[q]);

            if(i>1){
             isconnect(q);

            }
            q++;
            }
            if(c[0]=='Q'){
                scanf("%d",&k);
                int s=findroot(k);
                cnt=sum[s];
                printf("%d\n",cnt);

            }

        }
            if(t>0)printf("\n");
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-07-29 12:22:08

hdu 1558 Segment set (并查集)的相关文章

hdu 1558 Segment set (并查集+计算几何)

Segment set Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3486    Accepted Submission(s): 1297 Problem Description A segment and all segments which are connected with it compose a segment set

hdu 1558 (线段相交+并查集) Segment set

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1558 题意是在坐标系中,当输入P(注意是大写,我当开始就wa成了小写)的时候输入一条线段的起点坐标和终点坐标,当输入Q的时候输入n,然后输出与第n条线段相交的线段有多少条 首先判断线段是否相交,在算法导论p577上有介绍 线段A(x1,y1)-B(x2,y2),所在直线L1方程为F1(x,y)=0;线段C(x3,y3)-D(x4,y4),所在直线L2方程为F2(x,y)=0; 如何判断两条线段有交点:(

hdu 1558 线段相交+并查集路径压缩

Segment set Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3457    Accepted Submission(s): 1290 Problem Description A segment and all segments which are connected with it compose a segment set.

hdu 1558 线段相交+并查集

题意:要求相交的线段都要塞进同一个集合里 sol:并查集+判断线段相交即可.n很小所以n^2就可以水过 1 #include <iostream> 2 #include <cmath> 3 #include <cstring> 4 #include <cstdio> 5 using namespace std; 6 7 int f[1010]; 8 char ch; 9 int tmp,n; 10 double X1,X2,Y1,Y2; 11 12 #def

HDU 1558 Segment set (并查集+线段非规范相交)

题目链接 题意 : 如果两个线段相交就属于同一集合,查询某条线段所属集合有多少线段,输出. 思路 : 先判断与其他线段是否相交,然后合并. 1 //1558 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include <cmath> 6 #define eps 1e-8 7 #define zero(x) (((x) > 0 ? (x) : (-x)) < e

hdu 3234 Exclusive-OR (并查集+异或性质)

Exclusive-OR Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2177    Accepted Submission(s): 603 Problem Description You are not given n non-negative integers X0, X1, ..., Xn-1 less than 220 ,

HDU 4496 D-City (并查集)

题意:给你n个点m条边,问删除前i条边后有多少个连通分块. 思路:从后往前操作,从后往前添加i条边等于添加完m条边后删掉前m-i条边,可知刚开始没有边,所以sum[m]=n; #include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #include <queue> #include <math.h> #define M 100010

hdu 1811Rank of Tetris (并查集 + 拓扑排序)

1 /* 2 题意:这些信息可能有三种情况,分别是"A > B","A = B","A < B",分别表示A的Rating高于B,等于B,小于B. 3 4 现在Lele并不是让你来帮他制作这个高手榜,他只是想知道,根据这些信息是否能够确定出这个高手榜,是的话就输出"OK". 5 否则就请你判断出错的原因,到底是因为信息不完全(输出"UNCERTAIN"),还是因为这些信息中包含冲突(输出&quo

HDU 1232 畅通工程(并查集)

畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 30485    Accepted Submission(s): 16013 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可以实现交通(但不一定有