Tribal War
InterviewTime limit3sMemory limit512 MB
Given N tribes where listed pairs are allied and all other pairs are hostile, count the number of triples that are all allied or all hostile.
- Level
Medium5 of 10
- Topics
- Combinatorics, Math, Graph, Implementation
- Solved
- No attempts yet
Problem
On a certain planet live small tribes. For convenience, number the tribes from to .
- Any two distinct tribes are either allied or hostile. This relation is symmetric, so there is no case where tribe A considers tribe B an ally while B considers A an enemy.
- Even if tribe A and tribe B are allied and tribe B and tribe C are allied, tribe A and tribe C may be hostile.
- For any three distinct tribes, if all three are allied, call it a three-way alliance, and if all three are hostile, call it a three-way hostility.
Find the total number of distinct three-way alliances and three-way hostilities among the tribes.
For example, suppose , tribes 1 and 2, tribes 1 and 3, and tribes 4 and 5 are allied, and every other pair of tribes is hostile. Then and form three-way hostilities and there is no three-way alliance, so the answer is .
As another example, suppose and tribes 1 and 2, tribes 2 and 3, and tribes 1 and 3 are allied. Then is a three-way alliance, and , , are three-way hostilities, so the answer is .
Input
The first line gives the number of test cases . ()
The first line of each test case gives and , separated by a space.
The next lines each give two integers and , meaning that and are allied.
Every alliance given in the input satisfies , and the same pair of tribes is never given more than once.
A pair of tribes that does not appear in the input is assumed to be hostile.
Output
For each test case, print the alliance-antialliance coefficient on one line.