import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Zadanie3 {
private static int edge;
private static int[][] matrix;
private static int[] adj;
void convert(int[][] m) {
int v = m.length;
List[] adj = new List[v];
for (int i = 0; i < v; ++i)
adj[i] = new List();
for (int i = 0; i < m.length; i++) {
for (int j = 0; j < m[i].length; j++) {
if (m[i][j] == 1) {
adj[i].add(j);
}
}
}
}
public static void main(String[] args) {
try {
Scanner scanner = new Scanner(new File("dane.txt"));
String line;
int a, b, x, edge = 0;
scanner.nextLine();
while (scanner.hasNextLine()) {
line = scanner.nextLine();
x = line.indexOf(",");
a = Integer.valueOf(line.substring(1, x));
b = Integer.valueOf(line.substring(x + 1, line.length() - 1));
System.out.println("A;" + a + " B:" + b);
edge = Math.max(a, b);
}
System.out.println(edge);
matrix = new int[edge + 1][edge + 1];
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
matrix[i][j] = 0;
}
}
scanner.close();
Scanner scanner1 = new Scanner(new File("dane.txt"));
String line1;
scanner1.nextLine();
while (scanner1.hasNextLine()) {
line1 = scanner1.nextLine();
x = line1.indexOf(",");
a = Integer.valueOf(line1.substring(1, x));
b = Integer.valueOf(line1.substring(x + 1, line1.length() - 1));
matrix[a][b] = 1;
// matrix[b][a] = 1;
}
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.print(matrix[i][j]);
}
System.out.println("");
}
scanner1.close();
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
Bipartite b = new Bipartite(edge);
if(b.isBipartite(matrix, 0))
System.out.println("Graph is bipartite");
else
System.out.println("Graph isn't bipartite");
}
}
class List {
private Integer[] tab;
private int s, start = 0;
public List(int v) {
tab = new Integer[v + 1];
for (int i = 0; i < tab.length; i++) {
tab[i] = null;
}
}
public List() {
tab = new Integer[512];
for (int i = 0; i < tab.length; i++) {
tab[i] = null;
}
}
public int size() {
return s;
}
public int poll() {
int head = 0;
for (int i = 0; i < tab.length; i++)
if (tab[i] != null) {
head = tab[i];
tab[i] = null;
s--;
break;
}
return head;
}
public void add(int v) {
if (start <= tab.length) {
for (int i = start; i < tab.length; i++)
if (tab[i] == null) {
tab[i] = v;
start++;
s++;
break;
}
}
}
public void print() {
for (int i = 0; i < tab.length; i++)
System.out.print(tab[i] + " ");
System.out.println("");
}
}
class Bipartite {
private static int V;
private int[] colorTab;
public Bipartite(int v) {
V = v;
colorTab = new int[V];
}
public boolean isBipartite(int[][] G, int start) {
for (int i=0; i<V; i++)
colorTab[i] = -1;
colorTab[start] = 1;
List q = new List();
q.add(start);
while(q.size() != 0) {
int u = q.poll();
if(G[u][u] == 1)
return false;
for(int v=0; v<V; ++v) {
if(G[u][v] == 1 && colorTab[v] == -1) {
colorTab[v] = 1 - colorTab[u];
q.add(v);
} else if(G[u][v] == 1 && colorTab[v] == colorTab[u])
return false;
}
}
return true;
}
}