zoukankan      html  css  js  c++  java
  • BOJ 1575 Triangles

    Triangles

    Accept:12     Submit:28

    Time Limit:1000MS     Memory Limit:65536KB

    Description

    There are points given in a space. There are no three points, such that they lie on the same straight line. Each pair of points is connected by a segment coloured red or black. Each triangle, whose sides have the same colour is called a monochromatic triangle. We are given a list of all red segments and we want to find the number of all monochromatic triangles.

    InputFormat

    The first line is an integer T(1≤T≤20) indicating the case number.

    For each case,the first line is N(1≤N≤1000) indicating the number of points and M(1≤M≤1000000) indicating the number of red edges.

    In each of the following M lines there are two integers x and y separated by a single space(1≤x,y≤N). They are numbers of vertices which are end points of a red segment.

    OutputFormat

    For each case output the number of monochromatic triangles(which all 3 edges are same color).

    SampleInput

    1

    6 9

    1 2

    2 3

    2 5

    1 4

    1 6

    3 4

    4 5

    5 6

    3 6

    SampleOutput

    2

    第三场新生排位赛的C题,比赛时看题面就吓到了,但后来听学长讲过后,发现经过转换后是一道很简单的数学题。

    给出n个点构成的一个完全图,图中的边有两种颜色,红色和黑色,求同色边构成的三角形有多少个。

    首先我们知道图中三角形总数为C(n,3)

    对于每一个点i,我们可以统计出由它连出去的红色边数red[i]和黑色变数n-1-red[i],这样该点连出去的不同色三角形就是red[i]*(n-1-red[i])

    将所有点连出去的不同色三角形数目1求和再除以2(因为每条边被计算了两次)就得到图中不同色三角形的总数。

    三角形总数减去不同色三角形数就是同色三角形数

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 
     5 using namespace std;
     6 
     7 long red[1001];
     8 
     9 int main()
    10 {
    11     int t;
    12     long m,n;
    13 
    14     scanf("%d",&t);
    15 
    16     while(t--)
    17     {
    18         int a,b;
    19         long ans,total,dif=0;
    20 
    21         memset(red,0,sizeof(red));
    22 
    23         scanf("%ld %ld",&n,&m);
    24         total=n*(n-1)*(n-2)/6;
    25 
    26         for(int i=1;i<=m;i++)
    27         {
    28             scanf("%d %d",&a,&b);
    29             red[a]++;
    30             red[b]++;
    31         }
    32 
    33         for(int i=1;i<=n;i++)
    34             dif+=red[i]*(n-1-red[i]);
    35         ans=total-dif/2;
    36 
    37         printf("%ld
    ",ans);
    38     }
    39 
    40     return 0;
    41 }
    [C++]
  • 相关阅读:
    如何使用API创建OpenStack虚拟机?
    Windows Server 2012 新特性:IPAM的配置
    DNSSec
    Win Server 8中的利器:微软在线备份服务
    AD RMS总结
    开发中辅助功能
    开发中坑爹的地方
    Js 中常用方法
    asp.net 错误处理
    js中的注意事项(持续整理)
  • 原文地址:https://www.cnblogs.com/lzj-0218/p/3185578.html
Copyright © 2011-2022 走看看