Codeforces 230E Triangles【思维】

来源:互联网 发布:2017程序员笔记本推荐 编辑:程序博客网 时间:2024/05/22 09:46

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 51 21 32 32 43 4
output
3
input
5 31 22 31 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);    }}