USCOJ 1231 Triangles(HUST 校赛题)
2014-07-07 20:54
381 查看
Description
You are given a figure consisting of n points in a 2D-plane and m segments connecting some of them. We guarantee that any two segments don’t share points except their ends and there’s no more than one segment between
the same pair of points. Please count the total number of triangles in the given figure.
Input
There’re multiple test cases. In each case:
The first line contains two positive integers n and m. (n ≤ 200, m ≤ 20000)
Each of the following n lines contains two real numbers x i and y i indicating the coordinates of the i-th point. (−100000 < x i ,y i < 100000)
Each of the following m lines contains four real numbers x i , y i , x j , y j . It means (x i ,y i ) and (x j ,y j ) are connected by a segment. We guarantee that these points are part of the given n points.
Output
For each test case, print a single line contains the total number of triangles in the given figure. Sample input and output
Sample Input
Sample Output
You are given a figure consisting of n points in a 2D-plane and m segments connecting some of them. We guarantee that any two segments don’t share points except their ends and there’s no more than one segment between
the same pair of points. Please count the total number of triangles in the given figure.
Input
There’re multiple test cases. In each case:
The first line contains two positive integers n and m. (n ≤ 200, m ≤ 20000)
Each of the following n lines contains two real numbers x i and y i indicating the coordinates of the i-th point. (−100000 < x i ,y i < 100000)
Each of the following m lines contains four real numbers x i , y i , x j , y j . It means (x i ,y i ) and (x j ,y j ) are connected by a segment. We guarantee that these points are part of the given n points.
Output
For each test case, print a single line contains the total number of triangles in the given figure. Sample input and output
Sample Input
4 5 0 0 1 1 2 0 1 0 0 0 1 1 1 1 2 0 2 0 1 0 1 0 0 0 1 1 1 0
Sample Output
3
//题意:给你一些平面上的点,和由这些点所连接成的边。问这些边能组成几个三角形。 //第一要解决的问题是如何判断这些边是否构成三角形,显然可以得到只要三个点不共线且每两个点之间有边即可组成三角形。 //第二要解决的问题是若三个点共线,例如0 0 0 1 和0 1 0 2,虽然可以一眼看出这三个点在同一直线上,但0 0 0 2之间的边并没有被保存,因此需要先预处理。 #include <stdio.h> #include <math.h> #define eps 0.000001 struct point//用结构体数组保存这些点的坐标。 { double x,y; } p[220]; int main() { int m,n; double a,b,c,d; while(scanf("%d%d",&n,&m)!=EOF) { int edge[201][201]= {0};//用二维数组表示两点之间是否有边连接,若定义在外面的话注意要清零。 int i,j,k,l,count=0; for(i=1; i<=n; i++) scanf("%lf%lf",&p[i].x,&p[i].y); for(i=1; i<=m; i++) { scanf("%lf%lf%lf%lf",&a,&b,&c,&d); for(j=1; j<=n; j++) { if(fabs(a-p[j].x)<eps&&fabs(b-p[j].y)<eps) k=j; if(fabs(c-p[j].x)<eps&&fabs(d-p[j].y)<eps) l=j; } edge[k][l]=edge[l][k]=1; } for(i=1; i<=n; i++)//将潜在的边在二维数组进行表示 { for(j=1; j<=n; j++) { for(k=1; k<=n; k++) { if(edge[j][i]==1&&edge[i][k]==1)//这个地方要注意点的顺序,若表示成edge[i][j]==1&&edge[j][k]==1,下面为edge[i][k]=edge[k][i]=1。这样表示的话会出错,我也不知道为啥 if(fabs((p[j].x-p[i].x)*(p[k].y-p[i].y)-(p[j].y-p[i].y)*(p[k].x-p[i].x))<eps)//判断是否共线 edge[j][k]=edge[k][j]=1; } } } for(i=1; i<j; i++)//判断是否构成三角形 { for(j=i+1; j<k; j++) { for(k=j+1; k<=n; k++) { if(edge[i][j]==1&&edge[j][k]==1&&edge[i][k]==1) if(fabs((p[j].x-p[i].x)*(p[k].y-p[i].y)-(p[j].y-p[i].y)*(p[k].x-p[i].x))>eps)//不共线且两两点之间有边则构成一个三角形。 count++; } } } printf("%d\n",count);//最后结果 } return 0; }
相关文章推荐
- hust 1607 Triangles 校赛 一个很好的题 hash
- HUST2012校赛,USC2013新队员训练第二场
- hust校赛 1614 折叠纸张找凸痕
- hust 校赛 1611 The mell hell 求排对所需最少时间
- hust1601 校赛
- hust校赛 1615 Matrix 矩阵和其逆矩阵的乘积中不为0的元素个数
- hust校赛 f题 The tree of hust(lis 变形)
- 【计算几何】Triangles HUST 1607
- hust1608 Dating With Girls hust校赛 BFS求最长路
- hust 校赛 Substring
- hust校赛c题 Move the Books(“最重上升子序列”)
- hust校赛d题 PHP is the best language int the world(二分图着色+递推)
- hust 1607 Triangles(经典好题)
- hust 华中科技大学校赛初赛 1599 Multiple 字符串中64的倍数的个数
- hust 1607--Triangles
- HUST_ACdream区域赛指导赛之手速赛系列(1)(2)D——数学——Triangles
- hust 1007 平衡树
- Scenario 2: Drawing Two Triangles with Indexing
- 1231 The Embarrassed Cryptography
- 2010年东北大学ACM程序设计竞赛冬季校赛题解