HDU 1558 Segment set(并查集之三)

问题描述:

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

问题思路:

P命令插入一条线段。

Q命令查询线段K所在线段集合的线段数目。

套用了判断两条线段是否相交的模板。

代码:

#include<stdio.h>
#include<algorithm>
using namespace std;
int pre[1007];
struct point
{
    double x;
    double y;
};
double mult(point a,point b,point c)
{
    return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
}
bool check(point aa,point bb,point cc,point dd)
{
    if(max(aa.x,bb.x)<min(cc.x,dd.x))
        return false;
    if(max(aa.y,bb.y)<min(cc.y,dd.y))
        return false;
    if (max(cc.x,dd.x)<min(aa.x,bb.x))
        return false;
    if(max(cc.y,dd.y)<min(aa.y,bb.y))
        return false;
    if(mult(cc,bb,aa)*mult(bb,dd,aa)<0)
        return false;
    if(mult(aa,dd,cc)*mult(dd,bb,cc)<0)
        return false;
    return true;
}
int find(int x)
{
    int r=x;
    while(r!=pre[r])
        r=pre[r];
    int i=x,j;
    while(i!=r)
    {
        j=pre[i];
        pre[i]=r;
        i=j;
    }
    return r;
}
void mix(int x,int y)
{
    int fx=find(x);
    int fy=find(y);
    if(fx!=fy)
        pre[fy]=fx;
}
void compress(int x)
{
    int r=x;
    while(r!=pre[r])
        r=pre[r];
    pre[x]=r;
}
int main()
{
    int n,T,count_p;
    char c;
    struct point p1[1007];
    struct point p2[1007];
    while(scanf("%d",&T)!=EOF)
    {
        while(T--)
        {
            scanf("%d",&n);
            getchar();
            count_p=0;
            for(int i=1;i<=n;i++)
                pre[i]=i;
            for(int i=1;i<=n;i++)
            {
                c=getchar();
                if(c==‘P‘)
                {
                    count_p++;
                    scanf("%lf%lf%lf%lf",&p1[count_p].x,&p1[count_p].y,&p2[count_p].x,&p2[count_p].y);
                    getchar();
                    for(int j=1;j<count_p;j++)
                        if(check(p1[count_p],p2[count_p],p1[j],p2[j]))
                            mix(count_p,j);
                }
                else{
                    int x;
                    int count=0;
                    scanf("%d",&x);
                    getchar();
                    for(int i=1;i<=n;i++)
                        compress(i);
                    int root=find(x);
                    for(int i=1;i<=n;i++)
                        if(pre[i]==root)
                            count++;
                    printf("%d\n",count);
                }
            }
            if(T!=0)
                printf("\n");
        }
    }
    return 0;
}
时间: 2024-08-27 15:48:17

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): 3907    Accepted Submission(s): 1471 Problem Description A segment and all segments which are connected with it compose a 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 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可以实现交通(但不一定有