Monochromatic Triangles
InterviewTime limit1sMemory limit128 MB
Given n points and a list of red edges (all other pairs are black), count the triangles whose three sides share one color.
- Level
Medium5 of 10
- Topics
- Combinatorics, Graph, Brute force, Implementation
- Solved
- No attempts yet
Problem
There are points in the plane, and no three of them are collinear. Every pair of points is joined by a segment colored either red or black. A triangle whose three sides all have the same color is called a monochromatic triangle. Given the list of all red segments, count the monochromatic triangles. Every pair of points not in the list is joined by a black segment.
Write a program that:
- reads the number of points, the number of red segments, and their list from standard input,
- computes the number of monochromatic triangles,
- writes the result to standard output.
Input
The first line contains the number of points (). The second line contains the number of red segments (). Each of the next lines contains two integers and , separated by a single space, the endpoints of a red segment (). Every other pair of points is black.
Output
Print a single integer: the number of monochromatic triangles.