Subgraph isomorphism P57656


Statement
 

pdf   zip

Given an undirected graph G=(V,E)G = (V, E), where VV is a set of vertices and EE is a set of edges, a connected component of GG is a maximal connected subgraph of GG. In other words, every two vertices xx and yy of VV belong to the same connected component if and only if there is a path from xx to yy. In the example below there are 7 connected components.

image

Given two undirected (sub)graphs G1=(V1,E1)G_1 = (V_1, E_1) and G2=(V2,E2)G_2 = (V_2, E_2), G1G_1 and G2G_2 are said to be isomorphic if and only if there exists a bijection f:V1V2f : V_1 \to V_2 such that for every x,yV1x, y \in V_1, {x,y}E1{f(x),f(y)}E2\{x, y\} \in E_1 \Leftrightarrow \{f(x), f(y)\} \in E_2. In the example above, the connected component with vertices {5,2,9}\{ 5, 2, 9\} is isomorphic to exactly two connected components: those with vertices {12,15,8}\{12, 15, 8\} and {6,10,1}\{6, 10, 1\}.

Write a program such that, for every given undirected graph GG, computes the number of pairs (not counting order) of connected components of GG that are isomorphic. For instance, the result for the graph above is 4: {5,2,9}\{ 5, 2, 9\} with {12,15,8}\{12, 15, 8\}, {5,2,9}\{ 5, 2, 9\} with {6,10,1}\{6, 10, 1\}, {12,15,8}\{12, 15, 8\} with {6,10,1}\{6, 10, 1\}, and {7}\{ 7\} with {4}\{ 4\}.

Input

Input consists of several graph descriptions. Each one begins with the number of vertices nn and the number of edges mm. Follow mm pairs of different numbers, each between 0 and n1n-1. You can assume 1n100001 \le n \le 10000. No edges are repeated. Every given connected component has at most 6 vertices.

Output

For every graph, print the number of connected components that are pairwise isomorphic.

Public test cases
  • Input

    16 10
    5 2 5 9 12 8 14 11 14 3 0 13 6 10 6 1 15 8 11 3
    101 0
    

    Output

    4
    5050
    
  • Information
    Author
    Salvador Roura
    Language
    English
    Official solutions
    C++
    User solutions
    C++