hdu1558 Segment set (判断线段相交+并查集)

Segment set

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


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
 

Recommend
LL   |   We have carefully selected several similar problems for you:   1829  1811  1198  3172  3038 
 
解析:1.判断任意两条线段是否相交,若相交,则属于一个集合。
           2.用并查集记录每条线段所属的集合,并记录该集合的线段数量。
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ms(a) memset(a,0,sizeof(a))
using namespace std;

const int maxn=1e3;
int f[maxn+10],s[maxn+10];
struct tnode{
	double x,y;
}p[maxn+10][2];

int find(int x)
{
  if(f[x]==x)return x;
  return f[x]=find(f[x]);
}

double crossProd(tnode a,tnode b,tnode c)
{
  return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
}

bool intersect(tnode a,tnode b,tnode c,tnode d)
{
  if(max(a.x,b.x)>=min(c.x,d.x) &&
     max(c.x,d.x)>=min(a.x,b.x) &&
     max(a.y,b.y)>=min(c.y,d.y) &&
     max(c.y,d.y)>=min(a.y,b.y) &&
     crossProd(a,b,c)*crossProd(a,b,d)<=0 &&
     crossProd(c,d,a)*crossProd(c,d,b)<=0)return 1;
   return 0;
}

int main()
{
  //freopen("1.in","r",stdin);
  
  int t,n,i,j,rj,rs,sum;char tmp;
  scanf("%d",&t);
  while(t--)
    {
      scanf("%d\n",&n); 
      for(sum=0,i=1;i<=n;i++)
        {
          scanf("%c\n",&tmp);
          if(tmp=='Q')
            {
              scanf("%d\n",&j);
              printf("%d\n",s[find(j)]);
			  continue;
			}
		  s[++sum]=1,f[sum]=sum;
		  scanf("%lf%lf",&p[sum][0].x,&p[sum][0].y);
		  scanf("%lf%lf\n",&p[sum][1].x,&p[sum][1].y);
		  for(j=1;j<sum;j++)
		    if(intersect(p[j][0],p[j][1],p[sum][0],p[sum][1]))
		      {
		        rj=find(j),rs=find(sum);
		        if(rj!=rs)f[rj]=rs,s[rs]+=s[rj];
			  }
		}
	  if(t)printf("\n");
	}
  return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值