Segment set

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

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
 
题目大意:有n个指令,p加入一条线段,q查询id线段所在集合(两线段有交点为同一集合)的元素个数。
思路:用并查集路径压缩记录各个线段间的关系,根据叉积的定义有:Cross(v,w)=0时w在v上,>0时w在v上方,<0时w在v下方。
两线段有交点的必要条件:必须每条线段的两个端点在另一线段的两侧或直线上。
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std; const double eps=1e-;
const int maxn=;
int f[maxn];
struct Point
{
double x,y;
Point(){}
Point(double x,double y):x(x),y(y){}
};
struct Line
{
Point a,b;
}L[maxn];
typedef Point Vector;
Vector operator -(Vector A,Vector B){return Vector(A.x-B.x,A.y-B.y);}
int dcmp(double x)
{
if(fabs(x)<eps) return ;
else return x<?-:;
}
double Cross(Vector A,Vector B){ return A.x*B.y-A.y*B.x;}//叉积 bool judge(Line a,Line b)//Cross(v,w)=0时w在v上,>0时w在v上方,<0时w在v下方
{
if(dcmp(Cross(a.a-b.a,b.b-b.a)*Cross(a.b-b.a,b.b-b.a))<=
&&dcmp(Cross(b.a-a.a,a.b-a.a)*Cross(b.b-a.a,a.b-a.a))<=)
return true;
return false;
}
int findset(int x){return f[x]!=x?f[x]=findset(f[x]):x;}
void Union(int a,int b)
{
a=findset(a);b=findset(b);
if(a!=b) f[a]=b;
}
int main()
{
int t,n,i,j,id;
char op[];
double x1,y1,x2,y2;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
int cnt=;
for(i=;i<n;i++)
{
scanf("%s",op);
if(op[]=='P')
{
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
L[++cnt].a=Point(x1,y1);L[cnt].b=Point(x2,y2);
f[cnt]=cnt;
for(j=;j<=cnt-;j++)
if(judge(L[cnt],L[j]))
Union(j,cnt);
}
else
{
int ans=;scanf("%d",&id);
id=findset(id);
for(j=;j<=cnt;j++)
if(findset(j)==id)
ans++;
printf("%d\n",ans);
}
}
if(t) printf("\n");
}
return ;
}

hdu 1558 线段相交+并查集路径压缩的更多相关文章

  1. hdu 1558 (线段相交+并查集) Segment set

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1558 题意是在坐标系中,当输入P(注意是大写,我当开始就wa成了小写)的时候输入一条线段的起点坐标和终点坐 ...

  2. hdu 1558 线段相交+并查集

    题意:要求相交的线段都要塞进同一个集合里 sol:并查集+判断线段相交即可.n很小所以n^2就可以水过 #include <iostream> #include <cmath> ...

  3. poj 1127:Jack Straws(判断两线段相交 + 并查集)

    Jack Straws Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2911   Accepted: 1322 Descr ...

  4. 并查集+路径压缩(poj1988)

    http://poj.org/problem?id=1988 Cube Stacking Time Limit: 2000MS   Memory Limit: 30000K Total Submiss ...

  5. 【数轴涂色+并查集路径压缩+加速】C. String Reconstruction

    http://codeforces.com/contest/828/problem/C [题意] [思路] 因为题目保证一定有解,所有优化时间复杂度的关键就是不要重复染色,所以我们可以用并查集维护区间 ...

  6. hdu 1558 Segment set (并查集)

    Segment set Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  7. 并查集 + 路径压缩(经典) UVALive 3027 Corporative Network

    Corporative Network Problem's Link Mean: 有n个结点,一开始所有结点都是相互独立的,有两种操作: I u v:把v设为u的父节点,edge(u,v)的距离为ab ...

  8. HDU 1558 Segment set( 判断线段相交 + 并查集 )

    链接:传送门 题意:输入一个数 n 代表有 n 组操作,P 是在平面内加入一条线段,Q x 是查询第 x 条线段所在相交集合的线段个数 例如:下图 5 与 1.2 相交,1 与 3 相交,2 与 4 ...

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

    先说一下题目大意:给定一些线段,这些线段顺序编号,这时候如果两条线段相交,则把他们加入到一个集合中,问给定一个线段序号,求在此集合中有多少条线段. 这个题的难度在于怎么判断线段相交,判断玩相交之后就是 ...

随机推荐

  1. C++调用C语言编译的so文件

    参考链接:https://blog.csdn.net/chenjinlong126/article/details/78990350 一.制作so文件:libadd_c.so或libadd_cpp.s ...

  2. RabbitMQ 学习资料

    https://www.rabbitmq.com/getstarted.html http://www.cnblogs.com/luxiaoxun/p/3918054.html http://back ...

  3. luogu4608 [FJOI2016]所有公共子序列问题

    题目描述: luogu loj 题解: 序列自动机(?)+高精+普及dp. 这个是猫老师的序列自动机(字符串从1开始): ]) { memset(t[n],-,sizeof(t[n])); ;i> ...

  4. 共享服务-FTP基础(二)

    续接上一篇 使用pam(Pluggable Authentication Modules)完成用户认证 pam_service_name=vsftpd pam配置文件:/etc/pam.d/vsftp ...

  5. Python爬虫系列-分析Ajax请求并抓取今日头条街拍图片

    1.抓取索引页内容 利用requests请求目标站点,得到索引网页HTML代码,返回结果. 2.抓取详情页内容 解析返回结果,得到详情页的链接,并进一步抓取详情页的信息. 3.下载图片与保存数据库 将 ...

  6. mysql的字符串连接符

    以前用SQL Server 连接字符串是用“+”,现在数据库用mysql,写个累加两个字段值SQL语句居然不支持"+",郁闷了半天在网上查下,才知道mysql里的+是数字相加的操作 ...

  7. leepcode作业解析-5-21

    25.Nim游戏 你和你的朋友,两个人一起玩 Nim 游戏:桌子上有一堆石头,每次你们轮流拿掉 1 - 3 块石头. 拿掉最后一块石头的人就是获胜者.你作为先手. 你们是聪明人,每一步都是最优解. 编 ...

  8. LeetCode(122) Best Time to Buy and Sell Stock II

    题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...

  9. 做ios工程时,把UI从xib移动到代码中遇到的问题

    由于四期要做多语言版本,带xib页面的工程做多语言版本比较麻烦,再加上现在已经习惯了代码中的viewdidload函数中初始化控件,所以就把两个页面从xib移到代码中去了. 在修改后加载页面会遇到ba ...

  10. 搭建基于金山快盘的Git服务器

    最近迷上了Git,这货堪称神器,用了它就再也不想用其他VCS了,就像上了高速就不想再走国道一样. 一般人使用Git+Github来搭建进行本地远程交互,不过Github弄个私人仓库是要刀乐思的,如果你 ...