冰楓論壇

標題: [演算法心得] UVA 193. Graph Coloring [打印本頁]

作者: jimmyhealer    時間: 2022-5-11 12:19
標題: [演算法心得] UVA 193. Graph Coloring
嗨大家好~ 我是新加入論壇的萌新,之後可能會每日更新一下演算法心得,那目前會在 c/c++ 版,如果大家很熱絡的話再申請 演算法/競賽程式 版吧!

--------------------------------------------------- 正文開始 ---------------------------------------------------

題目名稱: Graph Coloring
題目網站: Uva
題目網址: https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=129
題目內容:

You are to write a program that tries to find an optimal coloring for a given graph. Colors are applied to the nodes ofthe graph and the only available colors are black and white.The coloring of the graph is called optimal if a maximum ofnodes is black. The coloring is restricted by the rule that notwo connected nodes may be black.
zeJkQ6FhBQ


題目大意:

給你一張圖,要求你塗色,限制是塗黑色之間不能相鄰,而塗白色沒有限制。問最多能圖幾個黑色。

範例輸入 #1:
1
6 8
1 2
1 3
2 4
2 5
3 4
3 6
4 6
5 6

範例輸出 #1:
3
1 4 5
--------------------------------------------------- 題解心得 ---------------------------------------------------

這題分析一下有點像最大獨立集,但是就算優化過後的時間複雜度仍為 O(1.1888 ^ n),他總共有 100 個點,時間限制只有 3 秒,顯然會TLE。
所以換一條思路,如果直接DFS,只要塗黑色那相鄰的點勢必要塗白色,這樣大大的減少數量。
  1. #include<bits/stdc++.h>
  2. #define ll long long
  3. #define INF 0x3f3f3f3f
  4. #define MOD 1000000007
  5. #define io ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  6. using namespace std;
  7. const int MXN = 200005;
  8. int ans = 0;
  9. int n = 0, m = 0, k = 0;
  10. vector<int> pathAns, color;
  11. vector<vector<int> > path;

  12. void dfs(int idx, int c){
  13.   if(idx > n){
  14.     if(ans < c){
  15.       ans = c;
  16.       for(int i = 1; i <= n; ++i){
  17.         pathAns[i] = color[i];
  18.       }
  19.     }
  20.     return;
  21.   }
  22.   for(int i = idx; i <= n; ++i){
  23.     if(n - idx + c + 1 <= ans) return; // 剪枝,如果當前畫最多黑色仍然不足ans就退出。
  24.     if(color[i] == 0){
  25.       color[i] = i;
  26.       for(int j : path[i]) if(color[j] == 0) color[j] = i;
  27.       int now = i;
  28.       while(color[now] && now <= n) now++;
  29.       dfs(now, c + 1);
  30.       for(int j = 1; j <= n; ++j) if(color[j] == i) color[j] = 0;
  31.     }
  32.   }
  33. }

  34. void sol(){
  35.   ans = 0;
  36.   cin >> n >> m;
  37.   path.clear();
  38.   pathAns.clear();
  39.   color.clear();
  40.   path.resize(n + 1);
  41.   pathAns.resize(n + 1);
  42.   color.resize(n + 1);
  43.   for(int i = 0; i < m; ++i){
  44.     int s, t;
  45.     cin >> s >> t;
  46.     path[s].emplace_back(t);
  47.     path[t].emplace_back(s);
  48.   }
  49.   dfs(1, 0);
  50.   cout << ans << '\n';
  51.   int flag = 0;
  52.   for(int i = 1; i <= n; ++i){
  53.     if(pathAns[i] == i){
  54.       if(flag){
  55.         cout << ' ';
  56.       }
  57.       cout << i;
  58.       flag = 1;
  59.     }
  60.   }
  61.   cout << '\n';
  62. }

  63. int main(){
  64.   io
  65.   int t = 1;
  66.   cin >> t;
  67.   while(t--){
  68.     sol();
  69.   }
  70. }
複製代碼





歡迎光臨 冰楓論壇 (https://bingfong.com/) Powered by 冰楓