Codeforces 230E Triangles【思维】

E. Triangles
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Alice and Bob don't play games anymore. Now they study properties of all sorts of graphs together. Alice invented the following task: she takes a complete undirected graph with n vertices, chooses some m edges and keeps them. Bob gets the  remaining edges.

Alice and Bob are fond of "triangles" in graphs, that is, cycles of length 3. That's why they wonder: what total number of triangles is there in the two graphs formed by Alice and Bob's edges, correspondingly?

Input

The first line contains two space-separated integers n and m (1 ≤ n ≤ 106, 0 ≤ m ≤ 106) — the number of vertices in the initial complete graph and the number of edges in Alice's graph, correspondingly. Then m lines follow: the i-th line contains two space-separated integers aibi (1 ≤ ai, bi ≤ nai ≠ bi), — the numbers of the two vertices connected by the i-th edge in Alice's graph. It is guaranteed that Alice's graph contains no multiple edges and self-loops. It is guaranteed that the initial complete graph also contains no multiple edges and self-loops.

Consider the graph vertices to be indexed in some way from 1 to n.

Output

Print a single number — the total number of cycles of length 3 in Alice and Bob's graphs together.

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is advised to use the cincout streams or the %I64dspecifier.

Examples
input
5 5
1 2
1 3
2 3
2 4
3 4
output
3
input
5 3
1 2
2 3
1 3
output
4
Note

In the first sample Alice has 2 triangles: (1, 2, 3) and (2, 3, 4). Bob's graph has only 1 triangle : (1, 4, 5). That's why the two graphs in total contain 3 triangles.

In the second sample Alice's graph has only one triangle: (1, 2, 3). Bob's graph has three triangles: (1, 4, 5), (2, 4, 5) and (3, 4, 5). In this case the answer to the problem is 4.


题目大意:

从一个N个点的无向完全图中,拿出M条边到另外一个图中,计算两个图中三元环的个数总和。


思路:


考虑新组成的图中,如果一个点的度数为degree【i】的话,这个点在原图的度就是n-1-degree【i】。

不难理解,我们该点i,从新图中拿出一条边,再从原图中剩余部分拿出一条边,就能够组成一个三元环,而且这个三元环是在原图中被删除掉的。

原图中应该有C(n,3)个三元环,那么此时去掉删除部分就是Ans。

那么Ans=C(n,3)-Σ(degree【i】*(n-1-degree【i】));


Ac代码:


#include<stdio.h>
#include<string.h>
using namespace std;
__int64 degree[1500000];
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        __int64 output=(__int64)n*(n-1)*(n-2)/6;
        memset(degree,0,sizeof(degree));
        for(int i=1;i<=m;i++)
        {
            int x,y;scanf("%d%d",&x,&y);
            degree[x]++;degree[y]++;
        }
        __int64 sum=0;
        for(int i=1;i<=n;i++)
        {
            sum+=(degree[i])*(n-1-degree[i]);
        }
        printf("%I64d\n",output-sum/2);
    }
}









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值