Segment set

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

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
题意:求某根线段与其在同一个集合的线段的数量。
题解:线段相交+并查集。。但是我没想清楚为什么按我注释的那样写就WA了。。明明只是顺序不同
#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(计算几何+并查集)的更多相关文章

  1. HDU 2818 (矢量并查集)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2818 题目大意:每次指定一块砖头,移动砖头所在堆到另一堆.查询指定砖头下面有几块砖头. 解题思路: ...

  2. hdu 1116 欧拉回路+并查集

    http://acm.hdu.edu.cn/showproblem.php?pid=1116 给你一些英文单词,判断所有单词能不能连成一串,类似成语接龙的意思.但是如果有多个重复的单词时,也必须满足这 ...

  3. Bipartite Graph hdu 5313 bitset 并查集 二分图

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5313 题意: 给出n个顶点,m条边,问最多添加多少条边使之构成一个完全二分图 存储结构: bitset   ...

  4. hdu 3081(二分+并查集+最大流||二分图匹配)

    Marriage Match II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  5. 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 ...

  6. hdu 3536【并查集】

    hdu 3536 题意: 有N个珠子,第i个珠子初始放在第i个城市.有两种操作: T A B:把A珠子所在城市的所有珠子放到B城市.  Q A:输出A珠子所在城市编号,该城市有多少个珠子,该珠子转移了 ...

  7. HDU 1829 分组并查集

    题意:有两种性别,每组数据表示是男女朋友,判断输入的几组数据是否有同性恋 思路:http://blog.csdn.net/iaccepted/article/details/24304087 分组并查 ...

  8. HDU 1198(并查集)

    题意:给你11个图,每一个都有管道,然后给一张由这11个正方形中的n个组成的图,判断有几条连通的管道: 思路:在大一暑假的时候做过这道题,当时是当暴力来做的,正解是并查集,需要进行一下转换: 转换1: ...

  9. HDU 4496 D-City(并查集,逆思维)

    题目 熟能生巧...常做这类题,就不会忘记他的思路了... //可以反过来用并查集,还是逐个加边,但是反过来输出...我是白痴.....又没想到 //G++能过,C++却wa,这个也好奇怪呀... # ...

随机推荐

  1. 3687: 简单题(bitset)

    3687: 简单题 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 700  Solved: 319[Submit][Status][Discuss] ...

  2. JavaScript 仿ios滑动选择器

    从git上找了一个,不过不是我想要的,更改了许多.到了我想要的效果: roller_selector_0_9.js 首先上js: "use strict"; /* * Author ...

  3. myeclipse中项目名有红叉,但项目中文件没有报错的解决办法

    导入了别人的项目,各种jar包都放好后,path也都build好了,项目也能正常启动,但是就是项目名有红叉,这是为什么呢? 网上有人说Java build path中的jar包missing了,这是一 ...

  4. 23、php知识点总结基础教程--part-1

    1.基本语法 PHP 脚本可放置于文档中的任何位置. PHP 脚本以 <?php 开头,以 ?> 结尾 <?php // 此处是 PHP 代码 ?> PHP 文件的默认文件扩展 ...

  5. appium-desktop 环境搭建 Java版

    用的是appium-desktop1.8.1,testng6.11,java-client6.1.0,selenium-java3.13.0 1.下载逍遥模拟器,装好app 2.下载adb,用adb连 ...

  6. java 继承小结

    [code=java] //多态的经典例子 //向上转型后,父类只能调用子类和父类的共同方法和的重写方法(方法名相同,参数也相同),不能调用重载方法(方法名相同,但参数不同) class A { pu ...

  7. 【志银】Ubuntu Apache2配置SSL证书

    1.准备工作 证书文件:zain.crt.zain.key /etc/apache2/文件夹下新建ssl 文件夹,将证书文件放入/etc/apache2/ssl 2.配置SSL证书 打开/etc/ap ...

  8. 【Nescafé 31】杯NOIP模拟赛

    t1 题意:n*m的棋盘上从(1,1)走到(n,m),只能向下或向右,一些格子有老鼠,每个老鼠互不相同,当处于与老鼠有重边的格子时,视为看见了这只老鼠,求到终点看到最少的不同老鼠数. 分析:DP 由于 ...

  9. JZOJ 5305 C先生

    题意: 有一个n个点,m条边的图,没有重边.自环,且每一条边最多属于一个环路. 给出q组询问,每次询问u,v两点间的路径有多少种可能. 思路: 先看下方样例说明: 由样例说明可以得知,路径上每经过一个 ...

  10. jquery.jbox JBox-v2.3修改版

    原版jquery.jbox是个不错的jquery扩展,使用简单,功能很多.可惜的是作者把javascript加密了,并且2011年以后就不再更新.如果项目中用到了新的jquery版本,甚至jbox就没 ...