1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
  | 
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 100010, M = 200010;
int n, m;
// 注意这里建图初始化为M条边
int h[M], e[M], ne[M], idx;
int color[N];
void add(int a, int b){
    e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
bool dfs(int x, int c){
    
    color[x] = c;
    
    for(int i = h[x]; i != -1; i = ne[i]){
        int j = e[i];
        
        if(!color[j]){
            // 如果没染色进行染色
            if(!dfs(j, 3 - c))  return false;
        }else{
            // 如果已经染色,判断染色是否冲突,是否有奇数环
            if(color[j] == c)   return false;
        }
    }
    
    return true;
}
int main(){
    cin >> n >> m;
    
    memset(h, -1, sizeof(h));
    
    while(m-- ){
        int a, b;
        scanf("%d%d", &a, &b);
        add(a, b);
        add(b, a);
    }
    
    bool flag = true;
    for(int i = 1; i <= n; i++){
        if(!color[i]){
            if(dfs(i, 1) == false){
                flag = false;
                break;
            }
        }
    }
    
    if(flag == false){
        cout << "No" << endl;
    }else{
        cout << "Yes" << endl;
    }
    
    return 0;
}
  |