uva 818 (位运算 + 判环)
Cutting Chains |
What a find! 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 first 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 difficult than she at first 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 specified 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 | Output for the Sample Input |
---|---|
|
|
ACM World Finals 2000, Problem C
题意好难理解,最后才弄明白原来有n个环,编号从1到n,给出了一些环环相扣的情况,比如给1和2表示1和2两个环的扣在一起的,每个环都是可以打开的,问最少打开多少个环,然后再扣好,可以让所有的环成为一条链。
题解:
因为n最大才15,可以用一个二进制数表示各个环是否被打开,然后未被打开的环判断一下是否还有位置度数大于2,以及是否有环的存在,并且保证打开环的数目加1要大于剩余链的数目。
很傻的忘了环编号从1开始,wa了无数遍。。。
- #include <cstdio>
- #include <iostream>
- #include <sstream>
- #include <cmath>
- #include <cstring>
- #include <cstdlib>
- #include <string>
- #include <vector>
- #include <map>
- #include <set>
- #include <queue>
- #include <stack>
- #include <algorithm>
- using namespace std;
- #define ll long long
- #define _cle(m, a) memset(m, a, sizeof(m))
- #define repu(i, a, b) for(int i = a; i < b; i++)
- #define repd(i, a, b) for(int i = b; i >= a; i--)
- #define sfi(n) scanf("%d", &n)
- #define pfi(n) printf("%d\n", n)
- #define sfi2(n, m) scanf("%d%d", &n, &m)
- #define pfi2(n, m) printf("%d %d\n", n, m)
- #define pfi3(a, b, c) printf("%d %d %d\n", a, b, c)
- #define MAXN 16
- #define R 6
- #define C 7
- const int INF = 0x3f3f3f3f;
- vector<int> v[MAXN];
- bool vis[MAXN];
- bool mp[MAXN][MAXN];
- bool open[MAXN];
- bool dfs(int r, int fa)
- {
- if(vis[r]) return true;
- vis[r] = ;
- int siz = v[r].size();
- int d = siz;
- repu(i, , siz)
- {
- if(open[v[r][i]]) d--;
- else if(v[r][i] != fa)
- if(dfs(v[r][i], r)) return true;
- }
- if(d > ) return true;
- return false;
- }
- int main()
- {
- int n;
- int kase = ;
- while(~sfi(n) && n)
- {
- kase++;
- _cle(mp, );
- int x, y;
- while()
- {
- sfi2(x, y);
- if(x == - && y == -) break;
- mp[y][x] = mp[x][y] = ;
- }
- repu(i, , n + )
- {
- v[i].clear();
- repu(j, , n + ) if(mp[i][j]) v[i].push_back(j);
- }
- int minn = n;
- int lim = <<n;
- int flag;
- repu(i, , lim)
- {
- flag = ;
- _cle(vis, ); _cle(open, );
- int tot = , t = ;
- repu(j, , n)
- if((<<j) & i) tot++, open[j + ] = ;//由于这个j + 1这我一直没加1,wa了无数遍
- repu(j, , n + )
- if(!open[j] && !vis[j])
- {
- t++;
- if(dfs(j, -)) { flag = ; break; }
- }
- if(!flag && t <= tot + ) minn = min(tot, minn);
- }
- printf("Set %d: Minimum links to open is %d\n", kase, minn);
- }
- return ;
- }
uva 818 (位运算 + 判环)的更多相关文章
- UVA - 13022 Sheldon Numbers(位运算)
UVA - 13022 Sheldon Numbers 二进制形式满足ABA,ABAB数的个数(A为一定长度的1,B为一定长度的0). 其实就是寻找在二进制中满足所有的1串具有相同的长度,所有的0串也 ...
- uva 10718 Bit Mask (位运算)
uva 10718 Bit Mask (位运算) Problem A Bit Mask Time Limit 1 Second In bit-wise expression, mask is a ...
- UVA 10718 Bit Mask 贪心+位运算
题意:给出一个数N,下限L上限U,在[L,U]里面找一个整数,使得N|M最大,且让M最小. 很明显用贪心,用位运算搞了半天,样例过了后还是WA,没考虑清楚... 然后网上翻到了一个人家位运算一句话解决 ...
- POJ 1781 In Danger Joseph环 位运算解法
Joseph环,这次模固定是2.假设不是固定模2,那么一般时间效率是O(n).可是这次由于固定模2,那么能够利用2的特殊性,把时间效率提高到O(1). 规律能够看下图: watermark/2/tex ...
- UVa 818Cutting Chains (暴力dfs+位运算+二进制法)
题意:有 n 个圆环,其中有一些已经扣在一起了,现在要打开尽量少的环,使所有的环可以组成一条链. 析:刚开始看的时候,确实是不会啊....现在有点思路,但是还是差一点,方法也不够好,最后还是参考了网上 ...
- 位运算基础(Uva 1590,Uva 509题解)
逻辑运算 规则 符号 与 只有1 and 1 = 1,其他均为0 & 或 只有0 or 0 = 0,其他均为1 | 非 也就是取反 ~ 异或 相异为1相同为0 ^ 同或 相同为1相异为0,c中 ...
- 【UVA】658 - It's not a Bug, it's a Feature!(隐式图 + 位运算)
这题直接隐式图 + 位运算暴力搜出来的,2.5s险过,不是正法,做完这题做的最大收获就是学会了一些位运算的处理方式. 1.将s中二进制第k位变成0的处理方式: s = s & (~(1 < ...
- UVA 565 565 Pizza Anyone? (深搜 +位运算)
Pizza Anyone? You are responsible for ordering a large pizza for you and your friends. Each of th ...
- UVa 1590 IP网络(简单位运算)
Description Alex is administrator of IP networks. His clients have a bunch of individual IP addres ...
随机推荐
- Kindle 推送教程:教你用电子邮箱推送电子书(Kindle伴侣)
Kindle 推送是什么意思?如何通过电子邮件附件推送?或许刚刚接触 Kindle 的朋友对这个概念不是很清楚,其实所谓 Kindle 推送是指亚马逊提供的一个"Kindle 个人文档服务& ...
- Qt之多窗口切换
在新建对象(下一页面)的时候,把自身的this指针带进去,然后把自身hide(),隐藏起来,在(下一页面中)要回退的时候只需通过: 1. parentWidget()->show(); //显示 ...
- Codeforces 749C:Voting(暴力模拟)
http://codeforces.com/problemset/problem/749/C 题意:有n个人投票,分为 D 和 R 两派,从1~n的顺序投票,轮到某人投票的时候,他可以将对方的一个人K ...
- windows+caffe(一)——自己环境
环境:win7 旗舰版升级到sp1 虚拟机 无GPU vs2013 matlab2016a python2.7 安装caffe已经成功 安装过程见我的另一篇:http://www.cnblogs.co ...
- [转]error: 'retainCount' is unavailable: not available in automatic reference counting mode
转载地址:http://choijing.iteye.com/blog/1860761 后来发现是编译选项的问题: 1.点击工程名 打开编译选项 2.在编译选项中,选择Bulid Settin ...
- solr 4.6配置正解
最近在学习solr,可是在网上找了很多个配置的资料,要不就是solr版本不对,反正各种问题.最后终于出来了,在这里给大家分享一下 1.准备工作 我们要先去下载一个tomcat,我下载的版本是tomca ...
- a标签样式
.a{ cursor:pointer; color: blue; text-decoration:none; }
- noi 2989 糖果
题目链接:http://noi.openjudge.cn/ch0206/2989/ 首先,数据很大,直接用背包会re. 这里增加的是对%k 的余数维度.f[i][j] 表示前 i 种糖果取到总颗数模 ...
- 【Java】一个小程序,计算它包含的代码所需的耗时
写一个小程序,用来计算它包含的代码所需的耗时.虽然简单,测试代码是否耗时还是有点用的,不用重新写嘛~ import java.util.Date; import java.util.concurren ...
- html5 drag and drop
注:链接.图片默认是draggable的. mousemove在整个拖放的过程中不会被触发. dragStart设置: e.dataTransfer.effectAllowed = "mov ...