染色法判定二分图复盘
作者:
火球大的脸盆
,
2022-08-12 17:54:54
,
所有人可见
,
阅读 188
import java.io.*;
import java.util.*;
public class Main {
static final int N = 200010;
static int n, m, idx;
static int u, v;
static int[] e = new int[N], ne = new int[N], h = new int[N], color = new int[N];
static void add(int a, int b) {
e[idx] = b;
ne[idx] = h[a];
h[a] = idx++;
}
static boolean dfs(int u, int c) {
color[u] = c;
for (int i = h[u]; i != -1; i = ne[i]) {
int j = e[i];
if (color[j] == 0) {
if (!dfs(j, 3 - c)) return false;
}
else if (color[j] == c) return false;
}
return true;
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
Arrays.fill(h, -1);
String[] first_line = br.readLine().split(" ");
n = Integer.parseInt(first_line[0]);
m = Integer.parseInt(first_line[1]);
while (m-- != 0) {
String[] second_line = br.readLine().split(" ");
u = Integer.parseInt(second_line[0]);
v = Integer.parseInt(second_line[1]);
add(u, v);
add(v, u);
}
boolean flag = true;
for (int i = 1; i <= n; i++) {
if (color[i] == 0 && !dfs(i, 1)) {
flag = false;
break;
}
}
if (flag) {
bw.write("Yes\n");
bw.flush();
}
else {
bw.write("No\n");
bw.flush();
}
}
}