Two section Figure Determination Code (C)
This address: Http://blog.csdn.net/caroline_wendy
Title: Given a graph with n vertices. To stain each vertex on the graph, and to make the adjacent vertex colors different.
Can be dyed in up to 2 colors. There are no heavy edges and closed loops.
That is, the problem of binary graph .
Using Depth-first search (DFS), dye the vertices to C, and then dye the adjacent edges to-c.
Assuming that the adjacent edges are dyed, and the same, the graph is not a binary graph; Assuming that all edges are dyed and not the same, it is a two-part picture.
Do multiple searches to avoid non-connected graphs .
Code:
/* * CppPrimer.cpp * * Created on:2014.7.27 * author:caroline *//*eclipse cdt*/#include <stdio.h> #include <vector>using namespace Std;class program {static const int max_v = 100;/*vector<int> G[max_v] = {{1,3}, {0,2 }, {1,3}, {0,2}};int v = 4;*/vector<int> G[max_v] = {{{), {0,2}, {0,1}};int V = 3;int Color[max_v] = {0};bool DFS ( int V, int c) {Color[v] = c;for (size_t i=0; i<g[v].size (); ++i) {if (color[g[v][i]] = = c) return false;if (color[g[v][ I]] = = 0 &&!dfs (g[v][i],-c)) return false;} return true;} Public:void solve () {for (int i=0; i<v; i++) {if (color[i] = = 0) {if (!dfs (i,1)) {printf ("result = no\n"); return;}} printf ("result = yes\n");}; int main (void) {program ip;ip.solve (); return 0;}
Output:
result = No
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
Programming algorithms-two-part Figure determination Code (C)