hdu 1558 Segment set

来源:互联网 发布:淘宝抢购验证码 编辑:程序博客网 时间:2024/06/05 04:32

Segment set

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


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

Sample Output
12225

Author
LL

Source
HDU 2006-12 Programming Contest

Recommend
LL
知道两直线相交判定方法后其实这道题就很简单了。但是被一些小问题卡主了。尤其是那个?  :....的以前没注意过它的优先级和结合律。改了一下午。。。哎悲剧。。。留下调程序的代码。以警示自己以后要细心!

#include <stdio.h>typedef struct//定义一个直线{    double x1,y1,x2,y2;} line;line l[1010];int seter[1010],sum[1010];//seter用于建立并查集。sum记录几合中元素个数int ok(line a,line b)//判断两条直线是否相交{    int flag=0;    double c,d,e,f;    double ax1,ay1,ax2,ay2,bx1,by1,bx2,by2;//构造矩形分别存为矩形左下角和右上角坐标    a.x1>a.x2?ax1=a.x2,ax2=a.x1:(ax1=a.x1,ax2=a.x2);//优先级呀。。。改死我了!    a.y1>a.y2?ay1=a.y2,ay2=a.y1:(ay1=a.y1,ay2=a.y2);//不加括号它会看成    b.x1>b.x2?bx1=b.x2,bx2=b.x1:(bx1=b.x1,bx2=b.x2);//(?:),...;    b.y1>b.y2?by1=b.y2,by2=b.y1:(by1=b.y1,by2=b.y2);//气死了!    if(ax1<=bx2&&ay1<=by2&&bx1<=ax2&&by1<=ay2)//快速排斥。其为两矩形相交或内含条件        flag=1;        //if(b.x2==4.00&&flag==0)            //{printf("ok\n");       // printf("%lf  %lf  %lf %lf %lf %lf %lf %lf\n",ax1,ay1,ax2,ay2,bx1,by1,bx2,by2);}    if(flag==0)//矩形不相交直接pass掉        return 0;//其下为判断两线段是否跨列。求叉积    c=(b.x1-a.x1)*(b.y1-b.y2)-(b.x1-b.x2)*(b.y1-a.y1);    d=(b.x1-a.x2)*(b.y1-b.y2)-(b.x1-b.x2)*(b.y1-a.y2);    e=(a.x1-b.x1)*(a.y1-a.y2)-(a.x1-a.x2)*(a.y1-b.y1);    f=(a.x1-b.x2)*(a.y1-a.y2)-(a.x1-a.x2)*(a.y1-b.y2);    if(c*d<=0&&e*f<=0)//两线段相交条件为相互跨列        return 1;    else        return 0;}int root(int p)//并查集找根节点加路径压缩{    int i,j,t;    i=p;    while(seter[i]!=i)        i=seter[i];    j=p;    while(j!=i)    {        t=seter[j];        seter[j]=i;        j=t;    }    return i;}void merge(int p,int q)//合并两个集合{    //printf("%d to %d\n",p,q);    int r1,r2;    r1=root(p);    r2=root(q);    if(r1==r2)        return ;    if(sum[r1]>sum[r2])    {        seter[r2]=r1;        sum[r1]+=sum[r2];    }    else    {        seter[r1]=r2;        sum[r2]+=sum[r1];    }}int main(){    int i,j,k,t,n,m,c;    char com;    double x1,y1,x2,y2;    scanf("%d",&t);    for(i=1;i<=t;i++)    {        scanf("%d",&n);        getchar();        c=1;        for(j=1;j<=n;j++)//初始化            seter[j]=j,sum[j]=1;           // printf("hello\n");        for(j=1;j<=n;j++)        {            scanf("%c",&com);         //   printf("com is  %c\n",com);            if(com=='P')            {                scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);                //printf("%lf %lf %lf %lf\n",x1,y1,x2,y2);                //if(x1>x2){temp=x1,x1=x2,x2=temp;}                //if(y1>y2){temp=y1,y1=y2,y2=temp;}开始天真了。这不直接改变线段了么。。                l[c].x1=x1,l[c].x2=x2;                l[c].y1=y1,l[c].y2=y2;                for(k=1;k<c;k++)                  if(ok(l[c],l[k]))                    merge(k,c);                c++;            }            else if(com=='Q')            {                scanf("%d",&m);                printf("%d\n",sum[root(m)]);            }            while(getchar()!='\n');//过滤掉每行后面的符号        }        if(i<t)            printf("\n");    }    return 0;}/*110P 1.00 1.00 4.00 2.00P 1.00 -2.00 8.00 4.00Q 1P 2.00 3.00 3.00 1.00Q 1Q 3P 1.00 4.00 8.00 2.00Q 2P 3.00 3.00 6.00 -2.00Q 5*/


原创粉丝点击