spoj#ILD18ACP. Count Pairs
Count Pairs
Given a undirected graph with n veritces and m edges. Your taks is count the number of distinct pairs (u, v) that there is exist a path with length exactly 2 from u to v. Another mean, with each pair (u, v), we could find a vertex t that we have an edge (u, t) and (t, v). The input set may be contains multiple edge between any vertex and not consider to connected.
Input
- First line: n, m (1 <= n, m <= 10^5).
- m following line: u, v (1 <= u, v <= n).
Output
The number of distinct pairs.
Example
Input: 5 4
2 1
1 5
3 1
4 3
Output: 4
Note: we have (1, 4), (2, 3), (2, 5), (3, 5)