思路:
原理:一个图是二分图当且仅当图中不含奇数环,当且仅当染色法无矛盾!
dfs,将临点染成不同颜色,染色失败返回false
时间复杂度 O(n + m)
模板
给定一个 n 个点 m 条边的无向图,图中可能存在重边和自环。
请你判断这个图是否是二分图。
输入格式
第一行包含两个整数 n 和 m。
接下来 m 行,每行包含两个整数 u和 v,表示点 u 和点 v 之间存在一条边。
输出格式
如果给定图是二分图,则输出 Yes,否则输出 No。
数据范围
1≤n,m≤105
输入样例:
4 41 31 42 32 4
输出样例:
Yes
代码:
// dfsimport java.util.*;public class Main {static int n, m, idx;static int[] h, e, ne, color;public static void main(String[] args) {Scanner sc = new Scanner(System.in);n = sc.nextInt();m = sc.nextInt();h = new int[n + 1];Arrays.fill(h, -1);e = new int[2*m];ne = new int[2*m];color = new int[n + 1];while (m-- > 0) {int a, b;a = sc.nextInt();b = sc.nextInt();add(a, b);add(b, a);}boolean flag = true;for (int i = 1; i <= n; i++) {//第1个点还未被染色if (color[i] == 0) {//尝试将其染成1if (!dfs(i, 1)) {flag = false;break;}}}if (flag) System.out.println("Yes");else System.out.println("No");}static boolean dfs(int u, int c) {color[u] = c;for (int i = h[u]; i != -1; i = ne[i]) {//u当前临边为jint j = e[i];//如果j所在点为被染色,尝试将其染色if (color[j] == 0) {if (!dfs(j, 3 - c)) return false;}//如果j所在点已经被染色,判断下是否与c相同if (color[j] == c) return false;}return true;}static void add(int a, int b) {e[idx] = b;ne[idx] = h[a];h[a] = idx ++;}}
//bfsimport java.util.*;public class Main {static int n, m, idx;static int[] h, e, ne, color;public static void main(String[] args) {Scanner sc = new Scanner(System.in);n = sc.nextInt();m = sc.nextInt();h = new int[n + 1];Arrays.fill(h, -1);e = new int[2*m];ne = new int[2*m];color = new int[n + 1];while (m-- > 0) {int a, b;a = sc.nextInt();b = sc.nextInt();add(a, b);add(b, a);}boolean flag = true;for (int i = 1; i <= n; i++) {//第1个点还未被染色if (color[i] == 0) {//尝试将其染成1if (!bfs(i, 1)) {flag = false;break;}}}if (flag) System.out.println("Yes");else System.out.println("No");}static boolean bfs(int u, int c) {color[u] = c;Deque<Integer> q = new LinkedList<>();q.offer(u);while (!q.isEmpty()) {int cur = q.poll();c = 3 - color[cur];for (int i = h[cur]; i != -1; i = ne[i]) {int j = e[i];if (color[j] == 0) {color[j] = c;q.offer(j);} else if (color[j] != c)return false;}}return true;}static void add(int a, int b) {e[idx] = b;ne[idx] = h[a];h[a] = idx ++;}}
//并查集import java.util.*;public class Main {static int n, m, idx;static int[] h, e, ne, color;public static void main(String[] args) {Scanner sc = new Scanner(System.in);n = sc.nextInt();m = sc.nextInt();h = new int[n + 1];Arrays.fill(h, -1);e = new int[2*m];ne = new int[2*m];color = new int[n + 1];while (m-- > 0) {int a, b;a = sc.nextInt();b = sc.nextInt();add(a, b);add(b, a);}for (int i = 1; i <= n; i++)color[i] = i;boolean flag = true;label: for (int i = 1; i <= n; i++) {if (h[i] == -1) continue; //当前点没有出边,孤立点直接跳过int first = e[h[i]];int c = find(color[i]);for (int k = h[i]; k != -1; k = ne[k]) {int j = e[k];if (find(j) == c) {flag = false;break label;}color[find(j)] = find(first);}}if (flag) System.out.println("Yes");else System.out.println("No");}static int find(int x) {return x == color[x] ? x : (color[x] = find(color[x]));}static void add(int a, int b) {e[idx] = b;ne[idx] = h[a];h[a] = idx ++;}}
