hdu 1558 线段相交+并查集路径压缩
Segment set
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3457 Accepted Submission(s): 1290
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.
2
2
2
5
#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 线段相交+并查集路径压缩的更多相关文章
- hdu 1558 (线段相交+并查集) Segment set
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1558 题意是在坐标系中,当输入P(注意是大写,我当开始就wa成了小写)的时候输入一条线段的起点坐标和终点坐 ...
- hdu 1558 线段相交+并查集
题意:要求相交的线段都要塞进同一个集合里 sol:并查集+判断线段相交即可.n很小所以n^2就可以水过 #include <iostream> #include <cmath> ...
- poj 1127:Jack Straws(判断两线段相交 + 并查集)
Jack Straws Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2911 Accepted: 1322 Descr ...
- 并查集+路径压缩(poj1988)
http://poj.org/problem?id=1988 Cube Stacking Time Limit: 2000MS Memory Limit: 30000K Total Submiss ...
- 【数轴涂色+并查集路径压缩+加速】C. String Reconstruction
http://codeforces.com/contest/828/problem/C [题意] [思路] 因为题目保证一定有解,所有优化时间复杂度的关键就是不要重复染色,所以我们可以用并查集维护区间 ...
- hdu 1558 Segment set (并查集)
Segment set Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- 并查集 + 路径压缩(经典) UVALive 3027 Corporative Network
Corporative Network Problem's Link Mean: 有n个结点,一开始所有结点都是相互独立的,有两种操作: I u v:把v设为u的父节点,edge(u,v)的距离为ab ...
- HDU 1558 Segment set( 判断线段相交 + 并查集 )
链接:传送门 题意:输入一个数 n 代表有 n 组操作,P 是在平面内加入一条线段,Q x 是查询第 x 条线段所在相交集合的线段个数 例如:下图 5 与 1.2 相交,1 与 3 相交,2 与 4 ...
- 判断线段相交(hdu1558 Segment set 线段相交+并查集)
先说一下题目大意:给定一些线段,这些线段顺序编号,这时候如果两条线段相交,则把他们加入到一个集合中,问给定一个线段序号,求在此集合中有多少条线段. 这个题的难度在于怎么判断线段相交,判断玩相交之后就是 ...
随机推荐
- NOIP模拟赛 抓牛
[题目描述] 农夫约翰被通知,他的一只奶牛逃逸了!所以他决定,马上出发,尽快把那只奶牛抓回来. 他们都站在数轴上.约翰在N(O≤N≤100000)处,奶牛在K(O≤K≤100000)处.约翰有两种办法 ...
- vc文件操作汇总—支持wince
一.判断文件及文件夹是否存在 // 判断文件是否存在 BOOL IsFileExist(const CString& csFile) { DWORD dwAttrib = GetFileAtt ...
- Mycat高可用解决方案二(主从复制)
Mycat高可用解决方案二(主从复制) 系统部署规划 名称 IP 主机名称 用户名/密码 配置 mysql主节点 192.168.199.110 mysql-01 root/hadoop 2核/2G ...
- pycharm安装 suds模块报错:AttributeError: module 'pip' has no attribute 'main'
需求:安装suds模块 遇到的问题: 一.报错信息:[file][Default Settint]---Project Interpreter 点击 搜索suds安装模块报错 解决:依据上图提示找到C ...
- angular5自适应窗口大小
import {AfterViewInit, Directive, ElementRef, HostBinding, HostListener, Inject, Input, Renderer2} f ...
- Hogan的安装和使用
Hogan的安装和使用 通过npm安装hogan: npm install hogan.js --save-dev CommonJs下的使用方式: // 引入hogan var hogan = req ...
- spartan6不能直接把时钟连到IO上
1.问题的提出:spartan6中不允许时钟信号直接连到IO口上面? 2.解决办法: ODDR2的使用 ODDR2Primitive: Double Data Rate Output D Flip-F ...
- thinkphp5开发restful-api接口 学习笔记一
视频学习地址: http://study.163.com/course/courseMain.htm?courseId=1004171002 源码和文档(如果满意,欢迎 star): https:// ...
- Python基础:字符串(string)
字符串的常用操作 字符串与数组一样,支持索引操作.切片与遍历 索引.切片操作: name = 'jason' name[0] 'j' name[1:3] 'as' 遍历: for char in na ...
- 面试之mybatis和hibernate的区别
mybatis是支持普通SQL查询.存储过程和高级映射的优秀持久层框架.封装了 几乎所有的JDBC代码和参数的手工设置 ,以及结果集的检索: 封装了:1,获取连接,执行sql,释放连接. 2,sql的 ...