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 ...
随机推荐
- Quartz.NET
http://www.360doc.com/userhome.aspx?userid=11741424&cid=2#
- VMware下安装虚拟机Ubuntu14.04 Server设置桥接方式
我本地的采用的上网方式的拨号上网,IP段是一公网下的通过路由设置的局域网,网段182.18.1.* 本地连接包含以下: 其中无线上网卡的.WMware桥接是自定义的局域网IP段:192.168.253 ...
- Yeoman自动构建js项目
Aug 19, 2013 Tags: bowergruntJavascriptjsnodejsyeomanyo Comments: 10 Comments Yeoman自动构建js项目 从零开始nod ...
- 161213、Maven资源替换和Freemarker模板
先介绍一下本文的两位主角: Apache Maven - 正当红的项目管理工具 FreeMarker - 老牌的模板引擎 两者貌似互不相干,何来冲突呢? 原来Maven有一个内置的资源替换机制, 可以 ...
- Centos6.5 安装 RabbitMQ3.6.1
Centos6.5 安装 RabbitMQ3.6.1 个人安装RabbitMQ总结: 安装编译工具 yum -y install make gcc gcc-c++ kernel-devel m4 nc ...
- 如何利用php array_multisort函数 对数据库排序
数据库中有4个字段分别是id,volume,edition,name. 要求对查询结果按照volume+edition从大到小排序.下面将一下array_multisort函数array_multis ...
- Linux之一条命令解决常见问题(持续更新)
# 1.删除0字节文件 find -type f -size 0 -exec rm -f {} \; # 2.批量文件重命名 find . -type f -name "*.txt" ...
- Linux之保留yum安装软件后的RPM包
yum安装软件很方便,但是下载下来的rpm包在安装后默认会被删除掉: 如果希望保留yum安装的软件包该如何做呢? 设置方法: 将/etc/yum.conf里对应的keepcache参数改为1即可,然后 ...
- 【转】Struts1.x系列教程(5):HTML标签库
转载地址:http://www.blogjava.net/nokiaguy/archive/2009/01/archive/2009/01/archive/2009/01/archive/2009/0 ...
- 使用BigDecimal进行精确计算工具类
package com.develop.util; import java.math.BigDecimal; import java.math.RoundingMode; public class M ...