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

Segment set

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


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之后添加数据很巧妙,将k初始化成 1 之后再次输入后与前边小于 k 的挨着进行判断,之后k++,再输入下一组数据
关键点:快速跨立实验判断线段相交
Time:2014-08-22 21:42
总在努力比别人投入更多时间,但却总是不如别人,或许,天赋的确很重要,但我仍需要前进,笨笨就笨笨吧,无所谓别人怎么看,努力去做自己。加油!!!
*/
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
const int MAX=1010;
struct Point{
	double x,y; 
} s[MAX],e[MAX];
int father[MAX],numSon[MAX];
int findRoot(int x){
return father[x]==x?father[x]:father[x]=findRoot(father[x]);
}
double mul(Point a,Point c,Point b){
	return (c.x-a.x)*(b.y-a.y)-(b.x-a.x)*(c.y-a.y);
}
bool Intersect(Point a,Point b,Point c,Point d){//快速跨立排斥实验
	if(min(a.x,b.x)>max(c.x,d.x)) return false;
	if(min(a.y,b.y)>max(c.y,d.y)) return false;
	if(max(a.x,b.x)<min(c.x,d.x)) return false;
	if(max(a.y,b.y)<min(c.y,d.y)) return false;
	if(mul(a,c,b)*mul(a,b,d)<0) return false;
	if(mul(c,a,d)*mul(c,d,b)<0) return false;
	return true;
}
void merge(int a,int b){//并查集
	a=findRoot(a);
	b=findRoot(b);
	if(a!=b){
		father[b]=a;
		numSon[a]+=numSon[b];
		numSon[b]=0;
	}
}
int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		int N;
		scanf("%d",&N);
		for(int i=0;i<=N;i++){
			father[i]=i;
			numSon[i]=1;
		}
		char cmd[3];
			int k=1;
		while(N--){
			scanf("%s",cmd);
			if(cmd[0]=='P'){
				scanf("%lf%lf%lf%lf",&s[k].x,&s[k].y,&e[k].x,&e[k].y);
				for(int i=1;i<k;i++){
					if(Intersect(s[i],e[i],s[k],e[k])){
						merge(i,k);
					}
				}
				k++;
			}else{
				int pos;
				scanf("%d",&pos);
				printf("%d\n",numSon[findRoot(pos)]);
			}
		}
		if(T)printf("\n");
	}
return 0;
} 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值