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