You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.0 KiB

#include <bits/stdc++.h>
using namespace std;
const int N = 100010, M = N << 1;
int n, m;
int color[N];
// 邻接表
int e[M], h[N], idx, ne[M];
void add(int a, int b) {
e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
// u:点 c:颜色值
bool dfs(int u, int c) {
color[u] = c;
for (int i = h[u]; ~i; i = ne[i]) {
int v = e[i];
if (!color[v]) { // 没有染色过
if (!dfs(v, 3 - c)) return 0; // 这个3-c用的太妙了
} else if (color[v] == c)
return 0; // 染色冲突
}
return 1;
}
int main() {
memset(h, -1, sizeof h);
cin >> n >> m;
while (m--) {
int a, b;
cin >> a >> b;
add(a, b), add(b, a);
}
bool flag = 1;
for (int i = 1; i <= n; i++)
if (!color[i]) { // 把当前点染色为1
if (!dfs(i, 1)) {
flag = 0;
break;
}
}
if (flag)
puts("Yes");
else
puts("No");
return 0;
}