冰楓論壇

 找回密碼
 立即註冊
ads_sugarbook
搜索
查看: 1171|回覆: 0

[心得] [演算法心得] UVA 193. Graph Coloring

[複製鏈接]

7

主題

0

好友

11

積分

新手上路

Rank: 1

UID
320966
帖子
146
主題
7
精華
0
積分
11
楓幣
756
威望
11
存款
0
贊助金額
0
推廣
0
GP
31
閱讀權限
10
性別
保密
在線時間
9 小時
註冊時間
2021-9-30
最後登入
2022-7-6
發表於 2022-5-11 12:19:41 |顯示全部樓層
嗨大家好~ 我是新加入論壇的萌新,之後可能會每日更新一下演算法心得,那目前會在 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. }
複製代碼
複製連結並發給好友,以賺取推廣點數
簡單兩步驟,註冊、分享網址,即可獲得獎勵! 一起推廣文章換商品、賺$$
高級模式
B Color Image Link Quote Code Smilies |上傳

廣告刊登意見回饋關於我們職位招聘本站規範DMCA隱私權政策

Copyright © 2011-2024 冰楓論壇, All rights reserved

免責聲明:本網站是以即時上載留言的方式運作,本站對所有留言的真實性、完整性及立場等,不負任何法律責任。

而一切留言之言論只代表留言者個人意見,並非本網站之立場,用戶不應信賴內容,並應自行判斷內容之真實性。

小黑屋|手機版|冰楓論壇

GMT+8, 2024-4-19 09:14

回頂部