hdu 1558 Segment set

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

int tot,bin[1024],cnt[1024];

void init()
{
    for(int i=0; i<1024; i++)
    {
        bin[i]=i;
        cnt[i]=1;
    }
    tot=1;
}

int _find(int x)
{
    while(bin[x]!=x)
        x=bin[x];

    return x;
}

int main()
{
    int T,m,i,j,t,k;
    double x1[2024],x2[2024],y1[2024],y2[2024];
    char cmd[10];
    scanf("%d",&T);
    while(T--)
    {
        init();
        scanf("%d",&m);
        for(k=0; k<m; k++)
        {
            scanf("%s",cmd);
            if(cmd[0]==‘P‘)
            {
                scanf("%lf%lf%lf%lf",&x1[tot],&y1[tot],&x2[tot],&y2[tot]);
                //printf("%lf%lf%lf%lf",x1[tot],y1[tot],x2[tot],y2[tot]);
                i=tot;
                for(j=1; j<tot; j++)
                {
                    int a=_find(i),b=_find(j);
                    double t1,t2;
                    if(a==b) continue;
                    t1=((x1[i]-x1[j])*(y2[j]-y1[j])-(y1[i]-y1[j])*(x2[j]-x1[j]))*
                       ((x2[j]-x1[j])*(y2[i]-y1[j])-(y2[j]-y1[j])*(x2[i]-x1[j]));

                    t2=((x1[j]-x1[i])*(y2[i]-y1[i])-(y1[j]-y1[i])*(x2[i]-x1[i]))*
                       ((x2[i]-x1[i])*(y2[j]-y1[i])-(y2[i]-y1[i])*(x2[j]-x1[i]));
                    //printf("%.2f %.2f\n",t1,t2);
                    if(t1>=0&&t2>=0)
                    {
                        if(a>b)
                        {
                            bin[a]=b;
                            cnt[b]+=cnt[a];
                        }
                        else
                        {
                            bin[b]=a;
                            cnt[a]+=cnt[b];
                        }
                    }
                }
                tot++;
            }
            else if(cmd[0]==‘Q‘)
            {
                scanf("%d",&t);
                printf("%d\n",cnt[_find(t)]);
            }
        }
        if(T) printf("\n");
    }
    return 0;
}
时间: 2024-11-19 04:49:34

hdu 1558 Segment set的相关文章

HDU 1558 Segment set (并查集+线段非规范相交)

题目链接 题意 : 如果两个线段相交就属于同一集合,查询某条线段所属集合有多少线段,输出. 思路 : 先判断与其他线段是否相交,然后合并. 1 //1558 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include <cmath> 6 #define eps 1e-8 7 #define zero(x) (((x) > 0 ? (x) : (-x)) < e

hdu 1558 Segment set【基础带权并查集+计算几何】

Segment set Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3599    Accepted Submission(s): 1346 Problem Description A segment and all segments which are connected with it compose a segment set

HDU 1558 Segment set(并查集之三)

问题描述: 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 a

hdu 1558 Segment set (并查集)

Segment set Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3907    Accepted Submission(s): 1471 Problem Description A segment and all segments which are connected with it compose a segment set

hdu 1558 Segment set (并查集+计算几何)

Segment set Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3486    Accepted Submission(s): 1297 Problem Description A segment and all segments which are connected with it compose a segment set

hdu 1558 Segment set(并查集+判断线段是否相交)

代码: #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int father[1005]; int son_cnt[1005]; char s[5]; //int cnt; struct point { double x,y; }; point a[1005],b[1005]; int find_father(int x) { int r=x; while(fathe

hdu 5372 Segment Game(树状数组)

题目链接:hdu 5372 Segment Game 因为线段长度是递增的,不会出现后面的线段被前面的线段完全覆盖,所以只要分别计算[1,l-1]之间有多少个左端点,[1,r]之间有多少个右端点,想减即可. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 2 * 1e5 + 5; #define lowbit(x) ((x)&a

HDU - 5666 Segment (大数位运算)好题

HDU - 5666 Segment Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit Status Description Silen August does not like to talk with others.She like to find some interesting problems. Today she finds an interesting problem

hdu 1558 (线段相交+并查集) Segment set

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1558 题意是在坐标系中,当输入P(注意是大写,我当开始就wa成了小写)的时候输入一条线段的起点坐标和终点坐标,当输入Q的时候输入n,然后输出与第n条线段相交的线段有多少条 首先判断线段是否相交,在算法导论p577上有介绍 线段A(x1,y1)-B(x2,y2),所在直线L1方程为F1(x,y)=0;线段C(x3,y3)-D(x4,y4),所在直线L2方程为F2(x,y)=0; 如何判断两条线段有交点:(