• BZOJ 2916[Poi1997]Monochromatic Triangles


    题面:

    2916: [Poi1997]Monochromatic Triangles

    Time Limit: 1 Sec  Memory Limit: 128 MB
    Submit: 340  Solved: 175
    [Submit][Status][Discuss]

    Description

           空间中有n个点,任意3个点不共线。每两个点用红线或者蓝线连接,如果一个三角形的三边颜色相同,那么称为同色三角形。给你一组数据,计算同色三角形的总数。

    Input

    第一行是整数n, 3 <= n <= 1000,点的个数。

    第二行是整数m, 0 <= m <= 250000,红线数目。
     
    接下来的m行,每行两个数p和k,1 <= p < k <= n。表示一条红线的两个端点。

    Output

      一个整数,单色三角形的数目。

    Sample Input

    6
    9
    1 2
    2 3
    2 5
    1 4
    1 6
    3 4
    4 5
    5 6
    3 6

    Sample Output

    2

    HINT

    数据已加强

    O(n^3)的暴力会超时(扯淡!!!)。

    但是我们还是有显而易见的结论$ans=C_{3}^{n}-sum_{i=1}^{n}frac{(n-1-D[i])*D[i]}{2}$

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <string.h>
     4 #include <algorithm>
     5 using namespace std;
     6 inline int read()
     7 {
     8     int s=0,f=1;
     9     char ch=getchar();
    10     while(ch<'0'||ch>'9')
    11     {
    12         if(ch=='-')
    13             f=-1;
    14         ch=getchar();
    15     }
    16     while(ch>='0'&&ch<='9')
    17         s=(s<<1)+(s<<3)+ch-'0',ch=getchar();
    18     return s*f;
    19 }
    20 int n,m;
    21 int d[1001];
    22 int ans1,ans2;
    23 int main()
    24 {
    25     n=read();
    26     m=read();
    27     ans1=n*(n-1)*(n-2)/6;
    28     for(int i=1;i<=m;i++)
    29     {
    30         ++d[read()];
    31         ++d[read()];
    32     }
    33     for(int i=1;i<=n;i++)
    34         ans2+=(n-1-d[i])*d[i];
    35     printf("%d",ans1-(ans2>>1));
    36 }
    BZOJ 2916
  • 相关阅读:
    hdfs搭建常见问题
    [转]opencv与Labview的结合(Dll调用)
    [转]opencv二值化的cv2.threshold函数
    忙在比赛前
    [转]使用charls抓包微信小程序的解决方案
    [转]Python调用C语言
    [转]基于Python实现matplotlib中动态更新图片(交互式绘图)
    [转]Python十六进制与字符串的转换
    STM32学习日记
    [转]Linux下的softlink和hardlink
  • 原文地址:https://www.cnblogs.com/radioteletscope/p/7436425.html
Copyright © 2020-2023  润新知