首页 > 其他 > 详细

hdu 1558 Segment set

时间:2015-05-21 22:40:18      阅读:277      评论:0      收藏:0      [点我收藏+]
#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;
}

hdu 1558 Segment set

原文:http://blog.csdn.net/xinag578/article/details/45896185

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!