

Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 点
問題文
頂点 辺の連結無向グラフが与えられます。頂点には から までの番号がついています。
辺の情報はマス目 を用いて表され、 が 1
のとき頂点 を結ぶ辺が存在し、そうでないとき存在しないことを表します。
頂点全体を空でない集合 に分解し、以下を満たすようにすることが可能か判定してください。 可能な場合、集合の個数 の最大値を求めてください。
- どの辺も、番号が隣り合う頂点集合の頂点どうしを結ぶ。より正確には、どの辺 に対しても、ある が存在し、 または のいずれかを満たす。
制約
- は
0
または1
である - は
0
である - 与えられるグラフは連結
- は整数である
入力
入力は以下の形式で標準入力から与えられる。
出力
条件を満たす分割が不可能な場合、 を出力せよ。 そうでない場合、集合の個数 の最大値を出力せよ。
入力例 1Copy
2 01 10
出力例 1Copy
2
頂点 をそれぞれ に含めればよいです。
入力例 2Copy
3 011 101 110
出力例 2Copy
-1
入力例 3Copy
6 010110 101001 010100 101000 100000 010000
出力例 3Copy
4
Score : points
Problem Statement
Given is a connected undirected graph with vertices and edges. The vertices are numbered to , and the edges are described by a grid of characters .
If is 1
, there is an edge connecting Vertex and ; otherwise, there is no such edge.
Determine whether it is possible to divide the vertices into non-empty sets such that the following condition is satisfied. If the answer is yes, find the maximum possible number of sets, , in such a division.
- Every edge connects two vertices belonging to two "adjacent" sets. More formally, for every edge , there exists such that or holds.
Constraints
- is
0
or1
. - is
0
. - The given graph is connected.
- is an integer.
Input
Input is given from Standard Input in the following format:
Output
If it is impossible to divide the vertices into sets so that the condition is satisfied, print . Otherwise, print the maximum possible number of sets, , in a division that satisfies the condition.
Sample Input 1Copy
2 01 10
Sample Output 1Copy
2
We can put Vertex in and Vertex in .
Sample Input 2Copy
3 011 101 110
Sample Output 2Copy
-1
Sample Input 3Copy
6 010110 101001 010100 101000 100000 010000
Sample Output 3Copy
4