Uva 193 Graph Coloring

 Graph Coloring 

找到理想的最多的黑点排布的情况,其中黑点不能相邻,而白点可以相邻,想清楚了就知道直接放黑点就行了,遇到不能放的地方不要放,可以放的地方选择放与不放,随时更新并保存理想的情况

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=129

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<cmath> 
 5 #define MAXN 110 
 6 using namespace std;
 7 int graph[MAXN][MAXN];
 8 int note[MAXN], visit[MAXN], flag[MAXN], store[MAXN];
 9 int n, _max;
10 
11 
12 int Traverse(int cur, int sum)
13 {
14     int choice = 1;
15     if(_max < sum)
16     {
17         _max = sum;
18         memcpy(store, flag, sizeof(int)*(n+1));
19     }
20     if(sum+n-cur+1 <= _max) return 0;
21     for(int i=1; i<=n; ++i)
22         if(graph[cur][i] && flag[i] != 0)
23         {
24             choice = 0;
25             break;
26         }
27     if(choice == 1)
28     {
29         flag[cur] = 1;
30         Traverse(cur+1, sum+1);
31         flag[cur] = 0;
32     }
33     Traverse(cur+1, sum);
34     return 0;
35 }
36 
37 int main()
38 {
39     #ifndef ONLINE_JUDGE
40     freopen("input.txt", "r", stdin);
41     #endif
42     int T;
43     cin>>T;
44     while(T--)
45     {
46         int k;
47         cin>>n>>k;
48         memset(graph, 0, sizeof(graph));
49         memset(flag, 0, sizeof(flag));
50         memset(store, 0, sizeof(store));
51         for(int i=0; i<k; ++i)
52         {
53             int from, to;
54             cin>>from>>to;
55             graph[from][to] = graph[to][from] = 1;
56         }
57         _max = 0;
58         Traverse(1, 0);
59         printf("%d\n", _max);
60         for(int i=1,j=0; i<=n; ++i)
61         {
62             if(store[i] != 0)
63             {
64                 if(j++ == 0) printf("%d", i);
65                 else printf(" %d", i);
66             }
67         }
68         printf("\n");
69     }
70     return 0;
71 }

我能告诉你我错了无数遍吗?算了吧,菜鸟一个就是蛋疼!!!这次是想着减少不必要的遍历,却发现改来改去都是WA,肯定不知道哪里错了,花了一个早上的时间也算对得住它,起码自己的抗压能力又提高到了一个层次

原文地址:https://www.cnblogs.com/liaoguifa/p/3050551.html