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 |
---|---|
5 1 2 2 3 4 5 -1 -1 |
Set 1: Minimum links to open is 1 |
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 ...
随机推荐
- Jmeter中察看结果树中的响应数据,中文显示乱码问题处理
打开apache-jmeter-xxx\bin\jmeter.properties文件,搜索"encoding"关键字,找到如下配置: # The encoding to be u ...
- pycharm下载 -professional
Jet Brains 开发的PyCharm 我传到百度云上了.只供开发使用,违者必究.大家一起学开发.有谁有python 进阶 电子书,可以发到我的邮箱里:120946018@qq.com,谢谢 Ma ...
- cellspacing,cellpadding什么区别
cellspacing设置为“0”,显示的结果就是第一个表格的每个单元格之间的距离为0.若将表格边框设为“0”,则单元格 的距离就是0了cellpadding属性用来指定单元格内容与单元格边界之间的空 ...
- 记录一下我使用的vim的配置文件
还不是很完美: "au BufReadPost * if line("'\"") > 0|if line("'\"") &l ...
- Win10系统Start Menu上的图标莫名消失
今天在工作过程中,突然有测试的同事给我报来一个问题.她是这么描述的“执行完XXX工具之后,在Start Menu找不到图标了.” 针对问题本身: 1,是执行完XXXX工具之后? 2,Start Men ...
- json学习小记
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- js 操作 字符串
去w3school看看string的基本方法把: 方法描述 anchor() 创建 HTML 锚. big() 用大号字体显示字符串. blink() 显示闪动字符串. ...
- android 蓝牙串口通讯使用简介
需要的权限 <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-perm ...
- a标签样式
.a{ cursor:pointer; color: blue; text-decoration:none; }
- iOS开发中的内存分配(堆和栈)
进程的内存分区 所有进程(执行的程序)都必须占用一定数量的内存,它或是用来存放从磁盘载入的程序代码,或是存放取自用户输入的数据等等.不过进程对这些内存的管理方式因内存用途不一而不尽相同,有些内存是事先 ...