Address https://www.acwing.com/problem/content/862/
Given a free edges m n points to view the drawing there may be multiple edges and loopback.
Please determine whether this figure is a bipartite graph.
Input Format
The first line contains two integers n and m.
Subsequently m rows, each row comprising two integers u and v, indicates the presence of points u and v between an edge.
Output Format
If a given graph is a bipartite graph, the output is "Yes", otherwise a "No".
data range
1≤n,m≤105
Sample input:
4 4
1 3
1 4
2 3
2 4
Sample output:
Yes
The basic template
FIG traverse point is then randomly assigned adjacent to the color dots of another color must be
Template code
1 #include <cstring> 2 #include <iostream> 3 #include <algorithm> 4 5 using namespace std; 6 7 const int N = 100010, M = 200010; 8 9 int n, m; 10 int h[N], e[M], ne[M], idx; 11 int color[N]; 12 13 void add(int a, int b) 14 { 15 e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ; 16 } 17 18 bool dfs(int u, int c) 19 { 20 color[u] = c; 21 22 for (int i = h[u]; i != -1; i = ne[i]) 23 { 24 int j = e[i]; 25 if (!color[j]) 26 { 27 if (!dfs(j, 3 - c)) return false; 28 } 29 else if (color[j] == c) return false; 30 } 31 32 return true; 33 } 34 35 int main() 36 { 37 scanf("%d%d", &n, &m); 38 39 memset(h, -1, sizeof h); 40 41 while (m -- ) 42 { 43 int a, b; 44 scanf("%d%d", &a, &b); 45 add(a, b), add(b, a); 46 } 47 48 bool flag = true; 49 for (int i = 1; i <= n; i ++ ) 50 if (!color[i]) 51 { 52 if (!dfs(i, 1)) 53 { 54 flag = false; 55 break; 56 } 57 } 58 59 if (flag) puts("Yes"); 60 else puts("No"); 61 62 return 0; 63 }
Own Code
. 1 #include <the iostream> 2 #include <Vector> . 3 #include <algorithm> . 4 #include < String > . 5 . 6 . 7 the using namespace STD; . 8 . 9 / * 10 Input format 11 The first line contains two integers n and m. 12 is 13 is the next m rows, each row comprising two integers u and v, indicates the presence of points u and v between an edge. 14 15 Output Format 16 if a given graph is a bipartite graph, the output "Yes", and otherwise outputs "No". . 17 18 is data range . 19 1 ≦ n, m≤105 20 is input sample: 21 is . 7. 7 22 is . 1. 3 23 is . 1. 4 24 2. 3 25 2. 4 26 is . 5. 6 27 . 6. 7 28 . 5. 7 29 sample output: 30 No 31 is * / 32 33 is const int N = 100010 , M = 200010 ; 34 is 35 int n-, m; 36 37 [ Vector <Vector < int UV >> ( 100010 ); 38 is 39 Vector < int > ColorPt ( 100010 ); 40 41 is String RET; 42 is 43 is void DFS (int IDX, int Color) { 44 is IF (IDX> n-RET || == " No " ) { 45 return ; 46 is } 47 48 ColorPt [IDX] = Color; 49 50 // color 51 is Color Color% = 2 + . 1 ; 52 is for ( int I = 0 ; I <UV [IDX] .size (); I ++ ) { 53 is // adjacent to the point dyed another color point 54 is int coloridx = UV [IDX] [I]; 55 //Coloring conflicts directly returns 56 is IF (ColorPt [coloridx] =! 0 && ColorPt [coloridx] =! Color) 57 is { 58 RET = " No " ; 59 return ; 60 } 61 is IF (ret.empty () && ColorPt [coloridx ] == 0 ) 62 is DFS (coloridx, Color); 63 is } 64 65 66 return ; 67 } 68 69 70 int main () 71 is { 72 CIN n->> >> m; 73 74 for (int i = 0; i < m; i++) { 75 int a; int b; 76 cin >> a >> b; 77 uv[a].push_back(b); 78 uv[b].push_back(a); 79 } 80 /*for(int i= 1;i < n;i++) 81 dfs(i,1);*/ 82 83 for (int i = 1; i <= n; i++) { 84 if (ret == "No") break; 85 if (colorr[i] != 0) continue; 86 dfs(i, 1); 87 } 88 89 if (ret.empty()) ret = "Yes"; 90 cout << ret << endl; 91 92 93 return 0; 94 }