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

 

转载于:https://www.cnblogs.com/radioteletscope/p/7436425.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值