标签:acm
代码:
#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(father[x]!=x)
{
x=father[x];
}
father[r]=x;
//ans[x]++;
return x;
}
double xmult(point a,point b,point c) //大于零代表a,b,c左转
{
return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
}
bool OnSegment(point a,point b,point c) //a,b,c共线时有效
{
return c.x>=min(a.x,b.x)&&c.x<=max(a.x,b.x)&&c.y>=min(a.y,b.y)&&c.y<=max(a.y,b.y);
}
bool Cross(point a,point b,point c,point d) //判断ab 与cd是否相交
{
double d1,d2,d3,d4;
d1=xmult(c,d,a);
d2=xmult(c,d,b);
d3=xmult(a,b,c);
d4=xmult(a,b,d);
if(d1*d2<0&&d3*d4<0) return 1;
else if(d1==0&&OnSegment(c,d,a)) return 1;
else if(d2==0&&OnSegment(c,d,b)) return 1;
else if(d3==0&&OnSegment(a,b,c)) return 1;
else if(d4==0&&OnSegment(a,b,d)) return 1;
return 0;
}
void mergee(int x,int y)
{
//if(intersect3(a[x],b[x],a[y],b[y]))
//{
x=find_father(x);
y=find_father(y);
if(x!=y)
{
father[y]=x;
son_cnt[x]+=son_cnt[y];
//son_cnt[y]=son_cnt[x];
// son_cnt[y]--;
}
//}
}
int main()
{
int t;
int cnt;
scanf("%d",&t);
while(t--)
{
//memset(ans,0,sizeof(ans));
int n;
scanf("%d",&n);
for(int i=1; i<=n; i++)
{
father[i]=i;
son_cnt[i]=1;
}
cnt=0;
for(int i=1;i<=n;i++)
{
scanf("%s",s);
if(strcmp(s,"P")==0)
{
cnt++;
scanf("%lf%lf%lf%lf",&a[cnt].x,&a[cnt].y,&b[cnt].x,&b[cnt].y);
for(int j=1;j<cnt;j++)
{
if(Cross(a[j],b[j],a[cnt],b[cnt]))
mergee(j,cnt);
}
}
else
{
int ttt;
scanf("%d",&ttt);
printf("%d\n",son_cnt[find_father(ttt)]);
}
}
if(t)
printf("\n");
}
return 0;
}
hdu 1558 Segment set(并查集+判断线段是否相交)
标签:acm
原文地址:http://blog.csdn.net/xky1306102chenhong/article/details/46447527