Figure m coloring problem (backtracking)

Source: Internet
Author: User

Algorithm Design Example: m coloring (backtracking) memory limit: 2000 kb time limit: MSaccept: 8 submit: 14 Description given undirected connected graph G and m different colors. These colors are used to color the vertices of graph G. Each vertex has a color. Whether there is a coloring method to make the two vertices of each edge in G different colors. How many methods can be used to color the graph m. The first Input is the number of test samples T (T <120), followed by T test samples. The first line of each test sample is vertex number n, Edge Number M, and available color number m (n <= 10, M <100, m <= 7 ), in the next M row, each row has two integers u and v, indicating that the vertex u and v are connected by an edge. (1 <= u <v <= n ). Output corresponds to two rows Output for each test sample. The first row is in the format of "Case #: W", where '#' indicates the test samples (starting from 1 ), W is the number of m-colored schemes. Sample Input 15 8 51 21 31 42 32 42 53 44 5 Sample Output Case 1: 360 Author Eapink solution: # include <iostream> using namespace std; # define N 100int m, n, M, a [N] [N], x [N], textNum; int static sum = 0; bool OK (int k) {for (int j = 1; j <= n; j ++) if (a [k] [j] & (x [j] = x [k]) return false; return true ;} void backtrack (int t) {if (t> n) {sum ++; // for (int I = 1; I <= n; I ++) // cout <x [I] <""; // cout <endl;} else for (int I = 1; I <= m; I ++) {x [t] = I; if (OK (t) backtrack (t + 1); x [t] = 0 ;}} int main () {int I, j, z = 1; cin> textNum; // enter the number of tests while (textNum> 0) {cin> n; // enter the number of vertices for (I = 1; I <= n; I ++) for (j = 1; j <= n; j ++) a [I] [j] = 0; cin> M> m; // number of input edges and number of available colors for (int k = 1; k <= M; k ++) // generate the graph's Adjacent matrix {cin> I> j; a [I] [j] = 1; a [j] [I] = 1 ;} /* for (I = 1; I <= n; I ++) {for (j = 1; j <= n; j ++) cout <a [I] [j] <"; cout <endl;} */for (I = 0; I <= n; I ++) x [I] = 0; backtrack (1); cout <"Case" <z <":" <sum <endl; sum = 0; textNum --; z ++;} return 0 ;}

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.