Problem
Figure 1. The graphs from the dataset
图1 。数据集中的图形
A bipartite graph is a graph whose vertices can be partitioned into two sets ( and ) such that there are no edges between vertices in the same set (for instance, if , then there is no edge between and ).
There are many other ways to formulate this property. For instance, an undirected graph is bipartite if and only if it can be colored with just two colors. Another one: an undirected graph is bipartite if and only if it contains no cycles of odd length.
Source: Algorithms by Dasgupta, Papadimitriou, Vazirani. McGraw-Hill. 2006.
Given: A positive integer and simple graphs in the edge list format with at most vertices each.
Return: For each graph, output "1" if it is bipartite and "-1" otherwise.
See Figure 1 for visual example from the sample dataset.
问题
甲二分图是曲线图 其顶点可以分为两组( 和 ),以使同一集合中的顶点之间没有边(例如,如果 ,则两者之间没有任何边缘 和 )。
还有许多其他方式可以表达此属性。例如,当且仅当无向图只能用两种颜色上色时,它才是二分图。另一个:无向图是二部的,当且仅当它不包含奇数长度的循环时。
资料来源:Dasgupta,Papadimitriou,Vazirani的算法。麦格劳-希尔。2006年。
返回值:对于每个图,如果是二分图,则输出“ 1”,否则输出“ -1”。
有关示例数据集中的可视示例,请参见图1。
Sample Dataset
2 3 3 1 2 3 2 3 1 4 3 1 4 3 1 1 2
Sample Output
-1 1