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条连线,求这些线段最后组成多少个三角形。题目想好怎么做就不难了,大致就是先找出:三点在一条线上,但是只有两条连线,你必须找出这样的例子,并且把第三条线段加上,然后就是遍历所有的点,三点之间有连线且不共线,则组成三角形。
代码:
#include<iostream>
#include<stdio.h>
#include<cmath>
#include<map>
#include<cstring>
using namespace std;
const unsigned int MAX=200;
#define ERR 0.000001
struct Point
{
double x,y;
}point[MAX+10];
int edge[MAX+10][MAX+10];
map <double,int> mymap;
bool in_line(Point a1,Point a2,Point a3)//判断是否共线
{
if(fabs((a2.x-a1.x)*(a3.y-a2.y)-(a2.y-a1.y)*(a3.x-a2.x))<=ERR)
return true;
return false;
}
int main()
{
//freopen("Triangles.in","r",stdin);
int m,n,i,j,k,ans;
int u,v;
while(scanf("%d%d",&n,&m)!=EOF)
{
mymap.clear();
memset(edge,0,sizeof(edge));
ans=0;
for(i=0;i<n;i++)
{
scanf("%lf%lf",&point[i].x,&point[i].y);
mymap[point[i].x*20000+point[i].y]=i;
}
for(i=0;i<m;i++)
{
double p1,q1,p2,q2;
scanf("%lf%lf%lf%lf",&p1,&q1,&p2,&q2);
u=mymap[p1*20000+q1];
v=mymap[p2*20000+q2];
//printf("u==%d v==%d\n",u,v);
edge[u][v]=edge[v][u]=1;//点与线之间联系起来
}
/*for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
printf("edge[%d][%d]=%d ",i,j,edge[i][j]);
}
printf("\n");
}*/
for(i=0;i<n;i++)
for(j=0;j<n;j++)
for(k=0;k<n;k++)
if(i!=j&&j!=k&&i!=k&&edge[j][i]&&edge[i][k]&&!edge[j][k]&&in_line(point[i],point[j],point[k]))
edge[j][k]=edge[k][j]=1;//三个点中,有两条连线,并且三点共线,加一条连线
/*for(i=0;i<n;i++)//这种方法貌似可以,并且复杂度较低,但就是通不过,不知为啥
for(j=i+1;j<n;j++)
for(k=j+1;k<n;k++)
{
//printf("point[%d] x=%lf y=%lf ",i,point[i].x,point[i].y);
//printf("point[%d] x=%lf y=%lf ",j,point[j].x,point[j].y);
//printf("point[%d] x=%lf y=%lf \n",k,point[k].x,point[k].y);
if(in_line(point[i],point[j],point[k]))
{
if((edge[i][j]&&(edge[j][k]||edge[i][k]))||(edge[j][k]&&edge[i][k]))
edge[i][j]=edge[i][k]=edge[j][k]=edge[j][i]=edge[k][i]=edge[k][j]=1;
}
}*/
for(i=0;i<n;i++)//扫描所有点,三点两两之间有连线,且不共线,则组成三角形
for(j=i+1;j<n;j++)
for(k=j+1;k<n;k++)
{
if(edge[i][j]&&edge[i][k]&&edge[j][k]&&!in_line(point[i],point[j],point[k]))
ans++;
}
printf("%d\n",ans);
}
return 0;
}

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. LINUX进程优先级实现

    首先linux进程优先级的范围是-20到19 将当前目录下的documents目录打包,但不希望tar占用太多CPU: nice -19 tar zcf pack.tar.gz documents 这 ...

  2. Android开发之bug-No Activity found to handle Intent

    android.content.ActivityNotFoundException: No Activity found to handle Intent 做Android开发中,使用隐式intent ...

  3. 1106. Two Teams(dfs 染色)

    1106 结点染色 当前结点染为黑 朋友染为白  依次染下去 这题是为二分图打基础吧 #include <iostream> #include<cstdio> #include ...

  4. OWIN katana注册中间件的几种写法

    首先特别说明下在startup中注册完中间件的两个注意事项,看到有人写的东西有误导人的作用.关于startup启动发现类的内容,参照这里 http://www.asp.net/aspnet/overv ...

  5. 排序算法(C#)

    1.插入排序 1.1直接插入排序 算法介绍: 直接插入排序(straight insertion sort)的做法是:   每次从无序表中取出第一个元素,把它插入到有序表的合适位置,使有序表仍然有序. ...

  6. 查询Table name, Column name, 拼接执行sql文本, 游标, 存储过程, 临时表

    018_Proc_UpdateTranslations.sql: SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO if (exists (select ...

  7. java.lang.NoClassDefFoundError: javax/transaction/Synchronization (jUnit测试报错)

      测试hibernate   报错原因项目缺少包   在 hibernate 解压目录下找到 jta.jar 文件     往项目中添加该 jar 包,即可解决   添加方法:[右击项目]--> ...

  8. 使用weka进行Cross-validation实验

    Generating cross-validation folds (Java approach) 文献: http://weka.wikispaces.com/Generating+cross-va ...

  9. uvalive 3135 Argus priority_queue

    用优先队列维护每个时间点优先级最高的元素. #include<iostream> #include<cstdio> #include<cstdlib> #inclu ...

  10. 2014上海网络赛 HDU 5053 the Sum of Cube

    水 #include <stdio.h> #include <stdlib.h> #include<math.h> #include<iostream> ...