Solution:
ans=sum of total triangles - sum of triangles with different colors
Define the angle with different edge as D-angle. Consider triangles with different colors, each such triangle has two D-angles, and these angels don't belong to other triangles.
So compute the numbers of D-angle at each node, then divide by 2, we can get the number of triangles with different colors.
#include <bits/stdc++.h> # define LL long using namespace std; int n,m; int deg[100005]; int main(){ scanf("%d %d", &n, &m); for(int i=1;i<=m;++i){ int u,v; scanf("%d %d",&u,&v); deg[u]++; deg[v]++; } LL res=0; for(int i=1;i<=n;++i){ res+=(LL)deg[i]*(n-1-deg[i]); } printf("%lld", (LL)n*(n-1)*(n-2)/6-(res>>1)); return 0; }