UVA818-Cutting Chains(二進制枚舉+dfs判環)
Problem UVA818-Cutting Chains
Accept:393 Submit:2087
Time Limit: 3000 mSec
Problem Description
What a ?nd! Anna Locke has just bought several links of chain some of which may be connected. They are made from zorkium, a material that was frequently used to manufacture jewelry in the last century, but is not used for that purpose anymore. It has its very own shine, incomparable to gold or silver, and impossible to describe to anyone who has not seen it ?rst hand. Anna wants the pieces joined into a single end-to-end strand of chain. She takes the links to a jeweler who tells her that the cost of joining them depends on the number of chain links that must be opened and closed. In order to minimize the cost, she carefully calculates the minimum number of links that have to be opened to rejoin all the links into a single sequence. This turns out to be more di?cult than she at ?rst thought. You must solve this problem for her.
Input
The input consists of descriptions of sets of chain links, one set per line. Each set is a list of integers delimited by one or more spaces. Every description starts with an integer n, which is the number of chain links in the set, where 1 ≤ n ≤ 15. We will label the links 1, 2, ..., n. The integers following n describe which links are connected to each other. Every connection is speci?ed by a pair of integers i,j where 1 ≤ i,j ≤ n and i ?= j, indicating that chain links i and j are connected, i.e., one passes through the other. The description for each set is terminated by the pair ‘-1 -1’, which should not be processed. The input is terminated by a description starting with n = 0. This description should not be processed and will not contain data for connected links.
Output
For each set of chain links in the input, output a single line which reads
Set N: Minimum links to open is M
where N is the set number and M is the minimal number of links that have to be opened and closed such that all links can be joined into one single chain.
Sample Input
5 1 2 2 3 4 5 -1 -1 7 1 2 2 3 3 1 4 5 5 6 6 7 7 4 -1 -1 4 1 2 1 3 1 4 -1 -1 3 1 2 2 3 3 1 -1 -1 3 1 2 2 1 -1 -1 0 Sample Ouput
Set 1: Minimum links to open is 1
Set 2: Minimum links to open is 2
Set 3: Minimum links to open is 1
Set 4: Minimum links to open is 1
Set 5: Minimum links to open is 1
題解:一看到n不超過15,向二進制的方向想是很自然的,順著思路就出來了,暴力枚舉情況,關鍵在於如何判斷一個情況是成立的首先判環是肯定的,然後就是判斷斷開的個數是否大於等於連通分支的個數-1。這兩點都很好想,容易忽略的就是如果一個環的分支數大於2也是不行的。這個雖然不太容易一下想到,但是樣例有提示(良心樣例),也不是什麽困難的問題,代碼都是套路。
1 #include <iostream> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cstdio> 5 #include <algorithm> 6 #define INF 0x3f3f3f3f 7 8 using namespace std; 9 10 const int maxn = 20; 11 int n; 12 int gra[maxn][maxn]; 13 int vis[maxn]; 14 15 bool dfs(const int sit,int fa,int u){ 16 vis[u] = -1; 17 for(int v = 0;v < n;v++){ 18 if(!gra[u][v] || vis[v]==1 || v==fa || !(sit&(1<<v))) continue; 19 if(vis[v] < 0) return false; 20 if(!vis[v] && !dfs(sit,u,v)) return false; 21 } 22 vis[u] = 1; 23 return true; 24 } 25 26 bool check(const int sit,int &res){ 27 memset(vis,0,sizeof(vis)); 28 for(int u = 0;u < n;u++){ 29 if(!(sit&(1<<u))) continue; 30 if(!vis[u]){ 31 if(!dfs(sit,u,u)) return false; 32 res++; 33 } 34 } 35 36 for(int u = 0;u < n;u++){ 37 if(!(sit&(1<<u))) continue; 38 int cnt = 0; 39 for(int v = 0;v < n;v++){ 40 if(gra[u][v] && sit&(1<<v)) cnt++; 41 if(cnt > 2) return false; 42 } 43 } 44 return true; 45 } 46 47 int iCase = 1; 48 49 int main() 50 { 51 while(~scanf("%d",&n) && n){ 52 int x,y; 53 memset(gra,0,sizeof(gra)); 54 while(scanf("%d%d",&x,&y) && (x!=-1 && y!=-1)){ 55 x--,y--; 56 gra[x][y] = gra[y][x] = 1; 57 } 58 int Min = INF; 59 for(int i = (1<<n)-1;i >= 0;i--){ 60 int res = 0; 61 if(check(i,res)){ 62 int cnt = 0; 63 for(int j = 0;j < n;j++){ 64 if(!(i&(1<<j))) cnt++; 65 } 66 if(res-1 <= cnt) Min = min(Min,cnt); 67 if(Min == 0) break; 68 } 69 } 70 printf("Set %d: Minimum links to open is %d\n",iCase++,Min); 71 } 72 return 0; 73 }
UVA818-Cutting Chains(二進制枚舉+dfs判環)