Codeforces 230E - Triangles 【图论】

本篇介绍了一个算法问题:给定一个无向完全图,取出部分边构成图A,剩余边构成图B,目标是计算这两个图中所有长度为3的环(三元环)的总数。通过枚举每个顶点并统计其在图A中的度数来高效解决该问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


E. Triangles

time limit per test 2 seconds

memory limit per test     256 megabytes


Alice and Bob don't play games anymore. Now they studyproperties of all sorts of graphs together. Alice invented the following task:she takes a complete undirected graph withn vertices, chooses somem edges and keeps them. Bob gets theremaining edges.

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

Input

The first line contains two space-separated integersn andm (1 ≤ n ≤ 106, 0 ≤ m ≤ 106) — the number of verticesin the initial complete graph and the number of edges in Alice's graph,correspondingly. Thenm lines follow: thei-th line contains two space-separatedintegersai,bi (1 ≤ ai, bi ≤ n,ai ≠ bi), — the numbers of the twovertices connected by the i-th edge in Alice's graph. It is guaranteed that Alice's graphcontains no multiple edges and self-loops. It is guaranteed that the initialcomplete graph also contains no multiple edges and self-loops.

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

Output

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

Please,do not use the%lldspecifier to read or write 64-bit integers in С++. It is advised to use thecin,cout streams or the%I64d specifier.

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 intotal contain 3 triangles.

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

 


【题意】


现在有一个n个顶点的无向完全图,显然它总共有n*(n-1)条边,现在从中拿出m条边,作为图A,剩下的边构成图B。


现在要求两个图中三元环的个数和。


【思路】


显然在原图中共有C(n,3)个三元环,现在我们需要考虑的便是拆成两个图后损失的三元环个数。


我们去枚举A图中的每个顶点,统计它在A图中的度x,那么很容易想到在原图中这个点还连向(n-1-x)个点,这些点在B图中,那么由这个点为中心便能构成x*(n-1-x)个三元环,而这些正是损失的数量。


于是对每个点计算求和即可,但由于有重复,例如A图中,以u为中心,连向v,和以v为中心,连向u造成了重复,计算的和除以二即可。


最后只要将总数减去损失的数量便是答案。



#include <cstdio>
#include <cmath>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define rush() int T;scanf("%d",&T);while(T--)

typedef long long ll;
const int maxn = 1000005;
const ll mod = 1e9+7;
const int INF = 0x3f3f3f3f;
const double eps = 1e-9;

int n,m;
int degree[maxn];

int main()
{
    int x,y;
    while(~scanf("%d%d",&n,&m))
    {
        mst(degree,0);
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&x,&y);
            degree[x]++;
            degree[y]++;
        }
        ll ans=(ll)n*(n-1)*(n-2)/6;
        ll cnt=0;
        for(int i=1;i<=n;i++)
        {
            cnt+=(ll)degree[i]*(n-1-degree[i]);
        }
        ans-=cnt/2;
        printf("%I64d\n",ans);
    }
    return 0;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值