从一个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);
}
}