import java.util.Scanner; public class Main { private static int index = 0; private static int[] lastEdge; private static int[] end; private static int[] previousEdge; private static int[] color; private static int n; private static int m; private static void addEdge(int a, int b) { index++; end[index] = b; previousEdge[index] = lastEdge[a]; lastEdge[a] = index; } private static void init(int n, int m) { Main.n = n; Main.m = m; Main.index = 0; end = new int[2 * m + 1]; lastEdge = new int[n + 1]; previousEdge = new int[2 * m + 1]; color = new int[n + 1]; } private static boolean dfs(int x, int c) { color[x] = c; for (int edge = lastEdge[x]; edge != 0; edge = previousEdge[edge]) { int b = end[edge]; if (color[b] == 0) { if (! dfs(b, 3 - c)) { return false; } } else if (color[b] == c) { return false; } } return true; } private static boolean isBipartiteGraph() { boolean flag = true; for (int i = 1;i <= n; ++ i) { if (color[i] == 0) { if (! dfs(i, 1)) { flag = false; break; } } } return flag; } public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNext()) { int n = in.nextInt(); int m = in.nextInt(); init(n, m); while (m -- > 0) { int a = in.nextInt(); int b = in.nextInt(); addEdge(a, b); addEdge(b, a); } System.out.println(isBipartiteGraph()); } } }
相关文章推荐
- Codeforces Round #435 (Div. 2)B. Mahmoud and Ehab and the bipartiteness(二分图,染色法)
- HDU 5971 Wrestling Match(染色法二分图 OR 并查集)
- 二分图判定(染色法)
- UVA - 10004 Bicoloring(判断二分图——交叉染色法 / 带权并查集)
- HDU 5285 wyh2000 and pupil(二分图,染色法)
- HDU 5285 wyh2000 and pupil(二分图,染色法)
- 二分图判断(染色法)
- SRM 593 Div1 L1:HexagonalBoard,用染色法判断无向图是否为二分图
- SRM 593 Div1 L1:HexagonalBoard,用染色法判断无向图是否为二分图
- c++全套流水账——染色法判断二分图,DFS的实践与应用
- NYOJ1015---二部图(判断是否是二分图:染色法)
- 判定二分图——染色法VjP1462小胖的难题
- 染色法判断是否是二分图 hdu2444
- 二分图染色问题
- poj 2446 Chessboard (二分图利用奇偶性匹配)
- 【二分图裸题】搭配飞行员
- 牛客网 素数伴侣(二分图最大匹配,匈牙利算法)
- scu4439 Vertex Cover(二分图最小点覆盖 匈牙利)
- poj 2771 uva12083 Guardian of Decency(二分图最大独立集)
- [网络流24题][codevs1922] 骑士共存问题 二分图最大独立集