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.

58 lines
1.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <bits/stdc++.h>
using namespace std;
const int N = 100010, M = N << 1;
typedef pair<int, int> PII;
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++;
}
bool bfs(int x) {
// 假设 1:黑2这样方便理解一些
color[x] = 1;
queue<PII> q;
q.push({x, 1});
while (q.size()) {
PII t = q.front();
q.pop();
int u = t.first, c = t.second;
for (int i = h[u]; ~i; i = ne[i]) {
int v = e[i];
if (!color[v]) {
color[v] = 3 - c;
q.push({v, 3 - c});
} else if (color[v] == c) // 发现冲突
return 0;
}
}
return 1;
}
int main() {
memset(h, -1, sizeof h);
cin >> n >> m;
int a, b;
while (m--) {
cin >> a >> b;
add(a, b), add(b, a);
}
int flag = 1;
for (int i = 1; i <= n; i++) {
if (!color[i]) {
if (!bfs(i)) {
flag = 0;
break;
}
}
}
if (flag)
puts("Yes");
else
puts("No");
return 0;
}