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

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");
    }
}
时间: 2024-08-07 00:18:20

hdu 1558(计算几何+并查集)的相关文章

Hdu 2473(并查集删除操作) Junk-Mail Filter

有木有很吊 加强 加强版   啊  ,看了都不敢做了   ,后来先做了食物链这个我还是看过的,但还是A不掉,没明白神魔意思 ,总而言之,大牛的博客是个好东西,我就那么看了一下,还是不懂怎莫办啊,哎,就那样就A掉了....... 今天我们来谈一下这个并查集的删除操作,根据我对大牛的理解啊,这个并查集的删除操作并不是把原来的节点删除掉,而是用一个替身替掉,现在的这个点只是用作桥梁的作用,即是无用的,del  ,,,del  ,,,,删除,那些被删掉的就从n开始给他们一个地址,然后即如下代码所示 #i

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

题目 熟能生巧...常做这类题,就不会忘记他的思路了... //可以反过来用并查集,还是逐个加边,但是反过来输出...我是白痴.....又没想到 //G++能过,C++却wa,这个也好奇怪呀... #include<stdio.h> #include<string.h> int fx,fy,r,bin[10010]; int x[100010],y[100010],n,m,i,count,ans[100010],j; int find(int x) { //改成这样就不会超时了么好

HDU 1272 简单并查集

小希的迷宫 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 24915    Accepted Submission(s): 7641 Problem Description 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双

hdu 3038 (并查集)

题目大意: 给出m个询问,问[l,r]之间的和   ,求出有多少次询问不和之前的矛盾的. 思路分析: 用并查集记录当前节点到根节点的和. #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #define maxn 222222 using namespace std; int set[maxn]; int sum[maxn]; int find(int

hdu 4496 D-City 并查集

D-City Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 2317    Accepted Submission(s): 814 Problem Description Luxer is a really bad guy. He destroys everything he met. One day Luxer went to D-

Farm Irrigation HDU - 1198 (并查集)

Farm Irrigation HDU - 1198 题意:给11种管道,问草地最少需要打多少个井才可以全部灌溉. 把每种管道的状态用二进制表示一下,然后对每一块草地,判断能否和上面或者左面的草地的管道连接. 然后并查集搞一下. 1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn=55; 4 5 int g[12]={10,9,6,5,12,3,11,14,7,13,15}; 6 int f[maxn*maxn]

HDU 5441 Travel 并查集

HDU 5441 Travel 题意:一张无向图,q个查询,对于每个x,有多少对点之间的路径中最长的一条路不大于x. 思路:比赛时王秋平写的,我补下题.这题也比较简单,将边排序,从小到大加到并查集,对查询也排序,从小到大对于每个查询把不大于x的边加到并查集,用cnt[y]记录以y为根的连通块有多少节点,那么在连通块发生 变化时,ans=2 * cnt[x] * cnt[y] 1 #include <iostream> 2 #include <cstdio> 3 #include &

HDU 2818 (矢量并查集)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2818 题目大意:每次指定一块砖头,移动砖头所在堆到另一堆.查询指定砖头下面有几块砖头. 解题思路: [HDU数据有问题],数据从0开始,且给定n块砖头(比如1000),数据会有第1005块砖头,导致访问越界. [解决方案],并查集初始化范围改为0~maxn(30005) 由于只给定一块砖头,却要移动所在堆.所以需要并查集维护所在堆. p[x]=y,即x所在堆的堆底是y,注意此时并查集是有方向的.

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

Marriage Match II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3558    Accepted Submission(s): 1158 Problem Description Presumably, you all have known the question of stable marriage match. A