Segment set

Segment set

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 166    Accepted Submission(s): 76

 
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
 
考察计算几何学和并查集,判断两个线段是否相交,要满足排斥实验和跨越实验2个条件,算法导论中有详细的介绍。
 
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;

const int MAXN = 1000 + 10;
struct point{
	double x, y;
};
struct segment{
	point begin, end;
}s[MAXN];
int m, index = 0;
int p[MAXN];
void init(){
	for(int i = 0; i < MAXN; i++){
		p[i] = i;
	}
}
//叉积 
double direction(point pi, point pj, point pk){
	//(pi-pk) *(pj-pk)
	return (pi.x - pk.x) * (pj.y - pk.y) - (pi.y - pk.y) * (pj.x - pk.x);
}
//判断pk是不是在pi,pj这个线段上 
bool on_segment(point pk, point pi, point pj){
	if(min(pi.x, pj.x) <= pk.x && max(pi.x, pj.x) >= pk.x && min(pi.y, pj.y) <= pk.y && max(pi.y, pj.y) >= pk.y)
	return true;
	else return false;
}
bool intersect(segment a, segment b){
	//排斥实验
	if(max(a.begin.x, a.end.x) >= min(b.begin.x, b.end.x) && max(a.begin.y, a.end.y) >= min(b.begin.y, b.end.y)
		&& max(b.begin.x, b.end.x) >= min(a.begin.x, a.end.x) && max(b.begin.y, b.end.y) >= min(a.begin.y, a.end.y)); 
	else return false;
		
	//跨越实验 	
	double d1 = direction(a.begin, b.end, b.begin);
	double d2 = direction(a.end, b.end, b.begin);
	double d3 = direction(b.end, a.end, a.begin);
	double d4 = direction(b.begin, a.end, a.begin);
	if(((d1 > 0 && d2 < 0) || (d1 < 0 && d2 > 0)) 
		&& (((d3 > 0 && d4 < 0) ||( d3 < 0 && d4 > 0))) ) return true;
	else if(d1 == 0 && on_segment(a.begin, b.end, b.begin)) return true;
	else if(d2 == 0 && on_segment(a.end, b.end, b.begin)) return true;
	else if(d3 == 0 && on_segment(b.end, a.end, a.begin)) return true;
	else if(d4 == 0 && on_segment(b.begin, a.end, a.begin)) return true; 
	else return false;
}
int find(int x){
	if(x != p[x]) {
		p[x] = find(p[x]);
	}
	return p[x];
}
void join(segment a){
	for(int i = 0; i < index; i++){
		if(intersect(a, s[i])) {
			int x = find(i);
			if(x != index) p[x] = index;
		}
	}
}
int main(){
	int kase;
	char f;
	int c;
	double x1, y1, x2, y2;
	scanf("%d", &kase);
	while(kase--){
		init();
		scanf("%d", &m);
		for(int i = 0; i < m; i++){
			scanf("%*c%c", &f);
			if(f == 'P'){
				scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
				s[index].begin.x = x1;
				s[index].begin.y = y1;
				s[index].end.x = x2;
				s[index].end.y = y2;
				join(s[index]);
				index++;
			}
			else if(f == 'Q'){
				scanf("%d", &c);
				int x = find(c-1);
				int cnt = 0;
				for(int i = 0; i < index; i++){
					if(x == find(i))cnt++;  //注意find,重新更新了父节点 
				}
				printf("%d\n", cnt);
			
			}
		}
		index = 0;
		init();
			if(kase > 0) printf("\n");
	}
	return 1;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Segment Anything是一个用于图像分割的工具,可以将图像中的不同物体分割出来。你可以使用以下方法来安装和使用Segment Anything: 方法一: 1. 安装一些基础依赖:pip install opencv-python pycocotools matplotlib onnxruntime onnx 2. 使用pip安装Segment Anything:pip install git+https://github.com/facebookresearch/segment-anything.git 方法二: 1. 克隆Segment Anything的GitHub仓库:git clone git@github.com:facebookresearch/segment-anything.git 2. 进入克隆的目录:cd segment-anything 3. 使用pip安装Segment Anything:pip install -e . 安装完成后,你可以按照以下方法使用Segment Anything: 1. 从给定的提示中导入必要的模块:from segment_anything import build_sam, SamPredictor 2. 创建一个SamPredictor对象,并加载模型的检查点:predictor = SamPredictor(build_sam(checkpoint="</path/to/model.pth>")) 3. 设置要处理的图像:predictor.set_image(<your_image>) 4. 使用predict方法进行图像分割:masks, _, _ = predictor.predict(<input_prompts>) 希望这些信息对你有帮助!\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* [Segment Anything(SAM)的demo的简单使用](https://blog.csdn.net/Helloorld_1/article/details/130107465)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [Segment Anything Model (SAM)——卷起来了,那个号称分割一切的CV大模型他来了](https://blog.csdn.net/Together_CZ/article/details/129991631)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值