Programming algorithms-two-part Figure determination Code (C)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.