1010: Triangles

Time Limit: 2 Sec   Memory Limit: 128 MB

Submit: 18  
Solved: 8

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 xi and yi indicating the coordinates of the i-th point. (−100000 < xi, yi < 100000)

Each of the following m lines contains four real numbers xi, yi, xj, yj . It means (xi,yi) and (xj,yj) 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

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

思路:题意是给你n个点,在这n个点里有m条连线,求这些线段最后组成多少个三角形。题目想好怎么做就不难了,大致就是先找出:三点在一条线上,但是只有两条连线,你必须找出这样的例子,并且把第三条线段加上,然后就是遍历所有的点,三点之间有连线且不共线,则组成三角形。
代码:
  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<cmath>
  4. #include<map>
  5. #include<cstring>
  6. using namespace std;
  7. const unsigned int MAX=200;
  8. #define ERR 0.000001
  9. struct Point
  10. {
  11. double x,y;
  12. }point[MAX+10];
  13. int edge[MAX+10][MAX+10];
  14. map <double,int> mymap;
  15. bool in_line(Point a1,Point a2,Point a3)//判断是否共线
  16. {
  17. if(fabs((a2.x-a1.x)*(a3.y-a2.y)-(a2.y-a1.y)*(a3.x-a2.x))<=ERR)
  18. return true;
  19. return false;
  20. }
  21. int main()
  22. {
  23. //freopen("Triangles.in","r",stdin);
  24. int m,n,i,j,k,ans;
  25. int u,v;
  26. while(scanf("%d%d",&n,&m)!=EOF)
  27. {
  28. mymap.clear();
  29. memset(edge,0,sizeof(edge));
  30. ans=0;
  31. for(i=0;i<n;i++)
  32. {
  33. scanf("%lf%lf",&point[i].x,&point[i].y);
  34. mymap[point[i].x*20000+point[i].y]=i;
  35. }
  36. for(i=0;i<m;i++)
  37. {
  38. double p1,q1,p2,q2;
  39. scanf("%lf%lf%lf%lf",&p1,&q1,&p2,&q2);
  40. u=mymap[p1*20000+q1];
  41. v=mymap[p2*20000+q2];
  42. //printf("u==%d v==%d\n",u,v);
  43. edge[u][v]=edge[v][u]=1;//点与线之间联系起来
  44. }
  45. /*for(i=0;i<n;i++)
  46. {
  47. for(j=i+1;j<n;j++)
  48. {
  49. printf("edge[%d][%d]=%d ",i,j,edge[i][j]);
  50. }
  51. printf("\n");
  52. }*/
  53. for(i=0;i<n;i++)
  54. for(j=0;j<n;j++)
  55. for(k=0;k<n;k++)
  56. if(i!=j&&j!=k&&i!=k&&edge[j][i]&&edge[i][k]&&!edge[j][k]&&in_line(point[i],point[j],point[k]))
  57. edge[j][k]=edge[k][j]=1;//三个点中,有两条连线,并且三点共线,加一条连线
  58. /*for(i=0;i<n;i++)//这种方法貌似可以,并且复杂度较低,但就是通不过,不知为啥
  59. for(j=i+1;j<n;j++)
  60. for(k=j+1;k<n;k++)
  61. {
  62. //printf("point[%d] x=%lf y=%lf ",i,point[i].x,point[i].y);
  63. //printf("point[%d] x=%lf y=%lf ",j,point[j].x,point[j].y);
  64. //printf("point[%d] x=%lf y=%lf \n",k,point[k].x,point[k].y);
  65. if(in_line(point[i],point[j],point[k]))
  66. {
  67. if((edge[i][j]&&(edge[j][k]||edge[i][k]))||(edge[j][k]&&edge[i][k]))
  68. edge[i][j]=edge[i][k]=edge[j][k]=edge[j][i]=edge[k][i]=edge[k][j]=1;
  69. }
  70. }*/
  71. for(i=0;i<n;i++)//扫描所有点,三点两两之间有连线,且不共线,则组成三角形
  72. for(j=i+1;j<n;j++)
  73. for(k=j+1;k<n;k++)
  74. {
  75. if(edge[i][j]&&edge[i][k]&&edge[j][k]&&!in_line(point[i],point[j],point[k]))
  76. ans++;
  77. }
  78. printf("%d\n",ans);
  79. }
  80. return 0;
  81. }

FROM:暑假训练第二场

Triangles的更多相关文章

  1. Count the number of possible triangles

    From: http://www.geeksforgeeks.org/find-number-of-triangles-possible/ Given an unsorted array of pos ...

  2. [ACM_搜索] Triangles(POJ1471,简单搜索,注意细节)

    Description It is always very nice to have little brothers or sisters. You can tease them, lock them ...

  3. acdream.Triangles(数学推导)

    Triangles Time Limit:1000MS     Memory Limit:64000KB     64bit IO Format:%lld & %llu Submit Stat ...

  4. UVA 12651 Triangles

    You will be given N points on a circle. You must write a program to determine how many distinctequil ...

  5. Codeforces Gym 100015F Fighting for Triangles 状压DP

    Fighting for Triangles 题目连接: http://codeforces.com/gym/100015/attachments Description Andy and Ralph ...

  6. Codeforces Round #309 (Div. 1) C. Love Triangles dfs

    C. Love Triangles Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/553/pro ...

  7. Codeforces Round #308 (Div. 2) D. Vanya and Triangles 水题

    D. Vanya and Triangles Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55 ...

  8. Project Euler 94:Almost equilateral triangles 几乎等边的三角形

    Almost equilateral triangles It is easily proved that no equilateral triangle exists with integral l ...

  9. Project Euler 91:Right triangles with integer coordinates 格点直角三角形

    Right triangles with integer coordinates The points P (x1, y1) and Q (x2, y2) are plotted at integer ...

  10. Project Euler 75:Singular integer right triangles

    题目链接 原题: It turns out that 12 cm is the smallest length of wire that can be bent to form an integer ...

随机推荐

  1. FormsAuthentication 登录兼容 IE11 保存cookie

    现象:使用FormsAuthentication进行登录验证,在IE11客户端无法保存cookie 解决方法:在web.config中的forms中增加cookieless="UseCook ...

  2. UVa 140 (枚举排列) Bandwidth

    题意较复杂,请参见原题=_=|| 没什么好说的,直接枚举每个排列就好了,然后记录最小带宽,以及对应的最佳排列. STL里的next_permutation函数真是好用. 比较蛋疼的就是题目的输入了.. ...

  3. poj2406 周期

    脑残wa了一次 var s:ansistring; ans,i,k,m:longint; pre:..] of longint; function max(x,y:longint):longint; ...

  4. Google 多源码管理工具 gclient

    google的chromium项目是用gclient来管理源码的checkout, update等. gclient是google专门为这种多源项目编写的脚本,它可以将多个源码管理系统中的代码放在一起 ...

  5. xml 实现圆形图 和 椭圆形图

    1. 效果图 2.圆形图 <ImageView android:layout_width="wrap_content" android:layout_height=" ...

  6. (转)solr排序OOM解决方法

    转自 http://topcat.iteye.com/blog/1293650 问题 lucene使用排序时会将被排序字段全部加入内存再进行排序,当多次使用不同字段进行排序时会造成OOM问题 解决方案 ...

  7. CSS遮罩——如何在CSS中使用遮罩

    Css遮罩是2008年4月由苹果公司添加到webkit引擎中的.遮罩提供一种基于像素级别的,可以控制元素透明度的能力,类似于png24位或png32位中的alpha透明通道的效果. 图像是由rgb三个 ...

  8. C# 等待另外一个窗体关闭,再进行主线程的代码

    方法1 用Form类或其子类的showDialog方法. 比如你在form1里有一个按扭,然后你在Form1的点击事件里写上显示form2的代码: Form2 frm=new Form2(); frm ...

  9. 房租管理小软件(七):flowlayoutPancel 中增加分类控

    见下图的 string FNodeName = dt.Rows[i]["FNodeName"].ToString(); ) { RoomControl.Thumbnail.Grou ...

  10. 【原】Redis分区

    Redis高级篇 分区 为什么分区? Redis中的分区主要有两个目的: 允许用多台机器的内存存放更大的数据集.如果没有分区,那么你只能存放单台机器内存的最大值的数据集. 允许用多核和多台机器提高计算 ...