Bi-coloring
InterviewTime limit1sMemory limit128 MB
Count the two-colorings where every edge joins different colors, or report -1 when the graph is not bipartite.
Problem
You are given an undirected graph. You want to paint every vertex with one of two colors, A and B, so that the two endpoints of every edge get different colors.
Count the colorings that satisfy this condition. The two colors are distinguishable, so two colorings are different as soon as one vertex is painted differently. You do not have to use both colors.
Input
The first line contains , the number of test cases.
The first line of each test case contains two integers () and (), the number of vertices and the number of edges. Each of the next lines contains two integers , (, ), meaning there is a bidirectional edge between vertex and vertex . There are no self loops and no duplicate edges.
Test cases may be separated by a blank line, and the last line of the input may be blank.
Output
For each test case, print on one line the number of ways to color the graph with the two colors. If the graph cannot be colored with the two colors, print -1.