标签:integer limit efi win 代码 cep nta one pass links
Problem UVA818-Cutting Chains
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.
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.
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.
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判环)
标签:integer limit efi win 代码 cep nta one pass links
原文地址:https://www.cnblogs.com/npugen/p/9572835.html