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. HTTP的缓存控制

    1.缓存的分类: (1)缓存分为服务端侧(server side,比如 Nginx.Apache)和客户端侧(client side,比如 web browser). (2)服务端缓存又分为 代理服务 ...

  2. druid sqlparser使用例子

    package com.alibaba.druid.bvt.sql.mysql; import java.util.List; import org.junit.Assert; import com. ...

  3. 使用Visual Studio 快速把 Json,Xml 字符串创建为一个实体类

  4. HTTP响应码

    更详细的内容参考:https://baike.baidu.com/item/HTTP状态码/5053660?fr=aladdin 转载CSDN作者:ddhsea 的文章 https://blog.cs ...

  5. iframe 如何让它展现内容自适应高度

    引用: <iframe id="ifm1" runat="server" src="/comment/page1?id=@productId&q ...

  6. Linq中Count()和Any()引发的效率问题

    1.count和any 今天看了鹤冲天的文章:Linq:切勿使用 Count() > 0 来判断集合非空   有所收获,写下文章总结一下: 先看如下代码: static void Main(st ...

  7. 事件Qevent的接受和忽略 和重定义 事件过滤器(转)

    转载来源:http://blog.csdn.net/seanyxie/article/details/5821970 事件处理流程:某个事件发生------>exec()循环会接收到这个事件-- ...

  8. [Leetcode/Javascript] 461.Hamming Distance

    [Leetcode/Javascript] 461.Hamming Distance 题目 The Hamming distance between two integers is the numbe ...

  9. MySQL数据库有哪些特点

    MySQL数据库的特点有: 它是C和C++语言编写的.支持多个操作系统.支持多线程.为多种编程语言提供API.优化SQL算法提高了查询速度以及提供用于管理和检查数据库的管理工具 MySQL数据库 My ...

  10. php连接数据库增删改查----多条件查询

    关于查询,可以直接写在主页面上 来进行查询 首先,先建立一个表单 <form method="post" action="crud.php"> &l ...