hdu 1558(计算几何+并查集)
Segment set
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4479 Accepted Submission(s): 1672
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.

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.
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
2
2
2
5
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
const int N = 1005;
struct Point
{
double x,y;
};
struct Line
{
Point a,b;
} line[N];
double cross(Point a,Point b,Point c)
{
return (a.x-c.x)*(b.y-c.y)-(a.y-c.y)*(b.x-c.x);
}
bool isCross(Point a, Point b, Point c, Point d)
{
if (cross(c, b, a)*cross(b, d, a)<0)return false;
if (cross(a, d, c)*cross(d, b, c)<0)return false;
return true;
}
int father[N],cnt[N];
int _find(int x)
{
if(x==father[x]) return x;
return _find(father[x]);
}
int main()
{
int tcase;
scanf("%d",&tcase);
while(tcase--)
{
int n;
scanf("%d",&n);
int k =1;
for(int i=1; i<=N; i++)
{
father[i]=i;
cnt[i]=1;
}
while(n--)
{
char c[2];
scanf("%s",c);
if(c[0]=='P')
{
scanf("%lf%lf%lf%lf",&line[k].a.x,&line[k].a.y,&line[k].b.x,&line[k].b.y);
k++;
for(int i=1; i<k; i++)
{
if(isCross(line[i].a,line[i].b,line[k-1].a,line[k-1].b))
{
int x = _find(k-1);
int y = _find(i);
if(x!=y)
{
father[x] = y;
cnt[y]+=cnt[x];
}
}
}
}
if(c[0]=='Q')
{
int num;
scanf("%d",&num);
/* for(int i=1; i<k; i++) //没想明白
{
if(isCross(line[i].a,line[i].b,line[num].a,line[num].b))
{
int x = _find(num);
int y = _find(i);
if(x!=y) {
father[x] = y;
cnt[y]+=cnt[x];
}
}
}*/
printf("%d\n",cnt[_find(num)]);
}
}
if(tcase)
printf("\n");
}
}
hdu 1558(计算几何+并查集)的更多相关文章
- HDU 2818 (矢量并查集)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2818 题目大意:每次指定一块砖头,移动砖头所在堆到另一堆.查询指定砖头下面有几块砖头. 解题思路: ...
- hdu 1116 欧拉回路+并查集
http://acm.hdu.edu.cn/showproblem.php?pid=1116 给你一些英文单词,判断所有单词能不能连成一串,类似成语接龙的意思.但是如果有多个重复的单词时,也必须满足这 ...
- Bipartite Graph hdu 5313 bitset 并查集 二分图
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5313 题意: 给出n个顶点,m条边,问最多添加多少条边使之构成一个完全二分图 存储结构: bitset ...
- hdu 3081(二分+并查集+最大流||二分图匹配)
Marriage Match II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- 2015 ACM/ICPC Asia Regional Changchun Online HDU - 5441 (离线+并查集)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5441 题意:给你n,m,k,代表n个城市,m条边,k次查询,每次查询输入一个x,然后让你一个城市对(u,v ...
- hdu 3536【并查集】
hdu 3536 题意: 有N个珠子,第i个珠子初始放在第i个城市.有两种操作: T A B:把A珠子所在城市的所有珠子放到B城市. Q A:输出A珠子所在城市编号,该城市有多少个珠子,该珠子转移了 ...
- HDU 1829 分组并查集
题意:有两种性别,每组数据表示是男女朋友,判断输入的几组数据是否有同性恋 思路:http://blog.csdn.net/iaccepted/article/details/24304087 分组并查 ...
- HDU 1198(并查集)
题意:给你11个图,每一个都有管道,然后给一张由这11个正方形中的n个组成的图,判断有几条连通的管道: 思路:在大一暑假的时候做过这道题,当时是当暴力来做的,正解是并查集,需要进行一下转换: 转换1: ...
- HDU 4496 D-City(并查集,逆思维)
题目 熟能生巧...常做这类题,就不会忘记他的思路了... //可以反过来用并查集,还是逐个加边,但是反过来输出...我是白痴.....又没想到 //G++能过,C++却wa,这个也好奇怪呀... # ...
随机推荐
- CSS3 Flexbox(伸缩盒/弹性盒模型)可视化指南
在http://css.doyoe.com/(CSS参考手册)中,本文对应其中的伸缩盒 引入 Flexbox布局官方称为CSS Flexible Box Layout Module是一个CSS3新的布 ...
- 理解Queue队列中join()与task_done()的关系
在网上大多关于join()与task_done()的结束原话是这样的: Queue.task_done() 在完成一项工作之后,Queue.task_done()函数向任务已经完成的队列发送一个信号 ...
- mutable c++
The keyword mutable is used to allow a particular data member of const object to be modified. This i ...
- 《Cracking the Coding Interview》——第8章:面向对象设计——题目3
2014-04-23 18:10 题目:设计一个点唱机. 解法:英文叫Musical Jukebox.这是点唱机么?卡拉OK么?这种题目实在是云里雾里,又没有交流的余地,我索性用一个vector来表示 ...
- Java线程的两种实现形式
一.创建线程的第一种方式:继承Thread类 class Demo extends Thread{ @Override public void run() { super.run(); for(int ...
- js学习日记-各种宽高总结(配图)
1.窗口和浏览器 window.innerWidth.window.innerHeight 浏览器内部可用宽高 window.outerWidth.window.outerHeight 浏览器 ...
- Python面试题之一:解密
Python面试题之一: 说明:就是Python工程师面试题 一.字典转换与正则提取值 1:key与Value交换 a = {'a':1,'b':2} print({value:key for key ...
- ansible自动部署Keepalived实现Nginx服务双机热备
脚本实现通过ansible-playbook自动化安装Keepalived和配置,主要解决问题如下: Keepalived自动化安装: keepalived_vrid配置,自动根据vip获取最后一段作 ...
- php 代码段执行时间
<?php //程序运行时间 $starttime = explode(' ',microtime()); echo microtime(); /*········以下是代码区······· ...
- Android应用如何打包?
android app开发结束后,就需要对app进行打包.部署与发布了,那对于android初学者而言,如何对apk进行打包呢?今天小编就为大家分享一二,一起来看看吧~~ aapt package - ...