Theorem 1, non-graph G is two the necessary and sufficient conditions for the graph: ① Figure G contains at least two points ②g the length of all loops must be an even number
According to this theorem, it is possible to determine whether a non-graph is a two-point graph if there is a singular circle in the binary graph.
The commonly used method is the adjacent staining method, the parent node and the child node have different colors, and the adjacent nodes have the same color, which indicates the existence of odd circle.
BFS implementation:
1#include <stdio.h>2#include <string.h>3#include <iostream>4#include <queue>5 #defineMAXN 10106 using namespacestd;7 8 intN, M;9 intCOL[MAXN];Ten intMP[MAXN][MAXN]; One BOOLISBG; A - //BFS Judging is not a two-point chart - BOOLBfscol (intID) { thequeue<int>que; - Que.push (ID); -Col[id] =1; - + while(!Que.empty ()) { - inttop =Que.front (); + Que.pop (); A for(intj=1; j<=n; ++j) { at if(Mp[top][j]) { - if(Col[j] = =-1) { -COL[J] = (col[top]^1); - Que.push (j); - } - Else if(Col[j] = =Col[top]) { in return false; - } to } + } - } the return true; * } $ Panax Notoginseng intMain () { - while(Cin >> N >>m) { theMemset (MP,0,sizeof(MP)); + for(intI=0; i<m; ++i) { A intx, y; theCIN >> x >>y; +Mp[x][y] =1; -MP[Y][X] =1; $ } $ -ISBG =true; -memset (Col,-1,sizeof(col)); the - for(intI=1; i<=n; ++i) {Wuyi if(Col[i]! =-1)Continue; the Else if(!Bfscol (i)) { -ISBG =false; Wu Break; - } About } $ - if(!ISBG) { -cout <<"no\n"; - Continue; A } + Elsecout <<"yes\n"; the } - return 0; $}
View Code
DFS implementations:
1#include <stdio.h>2#include <string.h>3#include <iostream>4 #defineMAXN 10105 using namespacestd;6 7 intN, M;8 intCOL[MAXN];9 intMP[MAXN][MAXN];Ten BOOLISBG; One A //DFS Judging whether it is a two-part diagram - voidDfs_col (intIintc) { - if(!ISBG)return;//Pruning theCol[i] =C; - - for(intj=1; j<=n; ++j) { - if(Mp[i][j]) {//all adjacent points + if(Col[j] = =-1) {//to dye a different color -Dfs_col (J, c^1); + } A Else if(Col[j] = = c) {//if the neighboring points are of the same color, not a binary chart atISBG =false; - return; - } - } - } - return; in } - to intMain () { + while(Cin >> N >>m) { -Memset (MP,0,sizeof(MP)); the for(intI=0; i<m; ++i) { * intx, y; $CIN >> x >>y;Panax NotoginsengMp[x][y] =1; -MP[Y][X] =1; the } + AISBG =true; thememset (Col,-1,sizeof(col)); + - for(intI=1; i<=n; ++i) { $ if(Col[i]! =-1)Continue; $Dfs_col (I,1); - if(ISBG = =false) - Break; the } - Wuyi if(!ISBG) { thecout <<"no\n"; - Continue; Wu } - Elsecout <<"yes\n"; About } $ return 0; -}
View Code
Two-Part chart determination