HDU 1558 Segment set

Segment set

Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 222 Accepted Submission(s): 100
 
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
题目大意:

P 是添加一个线段,  Q 是问 有几条线段和第 i 条线段相交。

思路:

其实我觉得,这个题用暴力完全可以去解。这里有一个很好的判断两条线段是否相交的版,不然,两条直线是否相交写起来还真的好烦。
还有需要注意的是,合并两个集合的时候,记得将他们所包含的线段的条数合并进去。嗯,就这么多了。

AC代码:

#include <iostream>
#include <string.h>
using namespace std;
const double EPS = 1e-10;
#define MAX 1001
struct point
{
    double x,y;
};
struct line
{
    point a,b;
}l[MAX];
int father[MAX],num[MAX];
double Max(double a,double b)   {return a>b?a:b;}
double Min(double a,double b)   {return a>b?b:a;}
bool inter(line l1,line l2)
{
    point p1,p2,p3,p4;
    p1=l1.a;p2=l1.b;
    p3=l2.a;p4=l2.b;

    if( Min(p1.x,p2.x)>Max(p3.x,p4.x) ||
       Min(p1.y,p2.y)>Max(p3.y,p4.y) ||
       Min(p3.x,p4.x)>Max(p1.x,p2.x) ||
       Min(p3.y,p4.y)>Max(p1.y,p2.y) )
        return 0;
    double k1,k2,k3,k4;
    k1 = (p2.x-p1.x)*(p3.y-p1.y) - (p2.y-p1.y)*(p3.x-p1.x);
    k2 = (p2.x-p1.x)*(p4.y-p1.y) - (p2.y-p1.y)*(p4.x-p1.x);
    k3 = (p4.x-p3.x)*(p1.y-p3.y) - (p4.y-p3.y)*(p1.x-p3.x);
    k4 = (p4.x-p3.x)*(p2.y-p3.y) - (p4.y-p3.y)*(p2.x-p3.x);
    return (k1*k2<=EPS && k3*k4<=EPS);
}
int GetParent(int x)
{
    while(father[x]!=x)
        x=father[x];
    return x;
}
//合并函数
void Merge(int a,int b)
{
    int temp_a,temp_b;
    temp_a=GetParent(a);
    temp_b=GetParent(b);
    if(temp_a!=temp_b)
    {
        father[temp_a]=temp_b;
        num[temp_b]+=num[temp_a];
    }
}

int main()
{
    int test,i,n,k,js;
    char c;
    cin>>test;
    while(test--)
    {
        js=0;
        cin>>n;
        int i;
        for(i=1;i<=n;i++)
        {
            father[i]=i;
            num[i]=1;
        }
        while(n--)
        {
            cin>>c;
            if(c=='P')
            {
                ++js;
                cin>>l[js].a.x>>l[js].a.y>>l[js].b.x>>l[js].b.y;
                for(i=1;i<js;++i)
                {
                    if( inter(l[js],l[i]) )
                        Merge(js,i);
                }
            }
            else
            {
                cin>>k;
                cout<<num[GetParent(k)]<<endl;
            }
        }
        if(test)
            cout<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值