UVA 193 Graph Coloring (graph staining) dfs+ backtracking

Source: Internet
Author: User

Not their own production is easy WA Ah, thought for a while did not think out, can not help but to find the answer, really did not resist to find the answer, the result is WA two

Times,,, or I think the more reliable Ah,

Ideas:

If the current point can be dyed black, dye it black, continue deep search, then backtrack and dye it white

If the current point can only be dyed white, it will be dyed white deep

Code:

#include <stdio.h> #include <string.h> #include <stdlib.h> int map[105][105];int visit[105];int ans[ 105],n,max;void dfs (int cur, int sum) {int i,j;if (cur > N) {if (sum > Max) {max = Sum;for (i=1; i<=n; i++) ans[i] = Vis It[i];} return;} int flag = 1;for (i=1; i<=n; i++) {if ((map[cur][i] = = 1) && (visit[i] = = 1)) {flag = 0;break;}} if (flag) {Visit[cur] = 1;dfs (cur+1, sum+1); Visit[cur] = 0;} DFS (cur+1,sum); return;} int main () {int t,m,i,a,b; scanf ("%d", &t), while (t--) {max = 0; memset (visit,0,sizeof)) Visit (memset (M AP)); memset (ans,0,sizeof (ans)); scanf ("%d%d", &n,&m); for (I=1; i<=m; i++) {scanf ("%d%d", &a,&b); Map[a][b] = map[b][a] = 1;  } dfs (1,0); printf ("%d\n", max); int C NT = 0; for (I=1; i<=n; i++) {if (ans[i] = = 1) {printf ("%d", i), cnt++; if (cnt! = max) printf ("");}  


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

UVA 193 Graph Coloring (graph staining) dfs+ backtracking

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.