A fair competition
Practice
2.5 (30 votes)
Dsu
Basics of disjoint data structures
Dfs
Bfs
Data structures
Disjoint data structures
Problem
94% Success 5497 Attempts 30 Points 2s Time Limit 256MB Memory 1024 KB Max Code

In competition, \(N\) participants are competing against each other for the top two spots. There are \(M\) friendly relations between participants. In each friendship relation, you will be given two integers \(L\) and \(R\), indicating \(L\) and \(R\) are friends.

Note

  • If A and B are friends, B and C are friends, then A, B, C will have the same friend circle.
  • Two combinations are considered different if either first or second ranker is different.
  • The same friendship relation can occur multiple times in input, however, \(L\) will never be equal to \(R\).

Now, the jury has to decide the winners for the top two spots but he does not want to select both participants from the same friend circle so the competition seems fairer. Find the number of ways in which he can do so.

Input format

  • The first line contains \(T\) denoting the number of test cases. 
  • The first line of each test case contains two space-separated integers \(N\) and \(M\) denoting the number of participants and the number of friendly relations.
  • Next \(M\) lines of each test case contain two integers \(L\) and \(R\) indicating a friendly relation between \(L\) and \(R\).

Output format

For each test case, print a single line denoting the number of ways in which the jury can choose the top two participants.

Constraints

\(1 \leq T \leq 20000\)

\(2 \leq N \leq 200000\)

\(1 \leq M \leq 500000\)

\(1 \leq L,R \leq N\)

Sum of \(N\) over all test cases does not exceed 200000 and \(M\) will not exceed 500000

 

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
17 votes
Tags:
Data StructuresDisjoint setEasyGraphs
Points:30
10 votes
Tags:
Data StructuresDisjoint setEasy
Points:30
68 votes
Tags:
ApprovedData StructuresDisjoint setEasyOpen