Counting triplets

3.8

10 votes
Ad-Hoc, Algorithms, Combinatorics, Graphs, Medium, 普通
Problem

You are given an undirected, complete graph G that contains N vertices. Each edge is colored in either white or black. You are required to determine the number of triplets (i,j,k) (1i<j<kN) of vertices such that the edges (i,j),(j,k),(i,k) are of the same color.

There are M white edges and N(N1)2M black edges.

Input format

  • First line: Two integers - N and M (3N105,1M3105)
  • (i+1)th line: Two integers - ui and vi (1ui,viN) denoting that the edge (ui,vi) is white in color

Note: The conditions(ui,vi)(uj,vj) and (ui,vi)(vj,uj) are satisfied for all 1i<jM.

Output format

Print an integer that denotes the number of triples that satisfy the mentioned condition.

Additional information

  • For 20 points: N200 is satisfied
  • For additional 20 points: N2000 is satisfied
  • Original constraints for remaining points
Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

The triplets are: {(1,2,3),(1,2,4),(2,3,4),(1,3,4)}.

The graph consisting of only white edges:

The graph consisting of only black edges:

 

Editor Image

?