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
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
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

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 (位运算 + 判环)的更多相关文章

  1. UVA - 13022 Sheldon Numbers(位运算)

    UVA - 13022 Sheldon Numbers 二进制形式满足ABA,ABAB数的个数(A为一定长度的1,B为一定长度的0). 其实就是寻找在二进制中满足所有的1串具有相同的长度,所有的0串也 ...

  2. uva 10718 Bit Mask (位运算)

    uva 10718  Bit Mask  (位运算) Problem A Bit Mask Time Limit 1 Second In bit-wise expression, mask is a ...

  3. UVA 10718 Bit Mask 贪心+位运算

    题意:给出一个数N,下限L上限U,在[L,U]里面找一个整数,使得N|M最大,且让M最小. 很明显用贪心,用位运算搞了半天,样例过了后还是WA,没考虑清楚... 然后网上翻到了一个人家位运算一句话解决 ...

  4. POJ 1781 In Danger Joseph环 位运算解法

    Joseph环,这次模固定是2.假设不是固定模2,那么一般时间效率是O(n).可是这次由于固定模2,那么能够利用2的特殊性,把时间效率提高到O(1). 规律能够看下图: watermark/2/tex ...

  5. UVa 818Cutting Chains (暴力dfs+位运算+二进制法)

    题意:有 n 个圆环,其中有一些已经扣在一起了,现在要打开尽量少的环,使所有的环可以组成一条链. 析:刚开始看的时候,确实是不会啊....现在有点思路,但是还是差一点,方法也不够好,最后还是参考了网上 ...

  6. 位运算基础(Uva 1590,Uva 509题解)

    逻辑运算 规则 符号 与 只有1 and 1 = 1,其他均为0 & 或 只有0 or 0 = 0,其他均为1 | 非 也就是取反 ~ 异或 相异为1相同为0 ^ 同或 相同为1相异为0,c中 ...

  7. 【UVA】658 - It&#39;s not a Bug, it&#39;s a Feature!(隐式图 + 位运算)

    这题直接隐式图 + 位运算暴力搜出来的,2.5s险过,不是正法,做完这题做的最大收获就是学会了一些位运算的处理方式. 1.将s中二进制第k位变成0的处理方式: s = s & (~(1 < ...

  8. UVA 565 565 Pizza Anyone? (深搜 +位运算)

      Pizza Anyone?  You are responsible for ordering a large pizza for you and your friends. Each of th ...

  9. UVa 1590 IP网络(简单位运算)

    Description   Alex is administrator of IP networks. His clients have a bunch of individual IP addres ...

随机推荐

  1. NULLIF()函数使用讲解

    NULLIF()函数接受两个参数.如果它们相等,那么返回空值:否则,返回第一个参数. 等价于下面的表达式: case when expression1=expression2 then null el ...

  2. RW-50004 While Running adrunfmw during EBS 12.2 Installation

    安装过程中报错: 日志文件信息: Executing command: /app/R1220/startCD/Disk1/rapidwiz/jre/Linux_x64//bin/java -cp /a ...

  3. POJ - 1245 Programmer, Rank Thyself

    POJ - 1245 Programmer, Rank Thyself Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %I64d  ...

  4. 【转】PowerShell入门(二):PowerShell是Cmd命令行的加强版吗?

    转至:http://www.cnblogs.com/ceachy/archive/2013/01/31/PowerShell_vs_Cmd.html PowerShell是命令行的加强版吗?Power ...

  5. HTML5之FileReader的使用

    HTML5定义了FileReader作为文件API的重要成员用于读取文件,根据W3C的定义,FileReader接口提供了读取文件的方法和包含读取结果的事件模型. FileReader的使用方式非常简 ...

  6. 04-时序逻辑电路设计之计数器——小梅哥FPGA设计思想与验证方法视频教程配套文档

    芯航线--普利斯队长精心奉献 实验目的:以计数器为例学会简单的时序逻辑电路设计 实验平台:芯航线FPGA核心板 实验原理: 时序逻辑电路是指电路任何时刻的稳态输出不仅取决于当前的输入,还与前一时刻输入 ...

  7. mysql分区表的原理和优缺点

    1.分区表的原理 分区表是由多个相关的底层表实现,这些底层表也是由句柄对象表示,所以我们也可以直接访问各个分区,存储引擎管理分区的各个底层表和管理普通表一样(所有的底层表都必须使用相同的存储引擎),分 ...

  8. Spring整合Hibernate。。。。

    环境搭建,在eclipse中导入spring和hibernate框架的插件,和导入所有使用到的架包 首先,hibernate的创建: 建立两个封装类,其中封装了数据库中表的属性,这儿只写属性,gett ...

  9. CentOS下LAMP一键yum安装脚本

    本脚本适用环境: 系统支持:CentOS/Redhat/Fedora 内存要求:≥64M 硬盘要求:2GB以上的剩余空间 服务器必须配置好软件源和可连接外网 必须具有系统 root 权限 建议使用干净 ...

  10. TortoiseGit 连接每次都要输入用户名和密码

    当你配置好git后,在C:\Documents and Settings\Administrator\ 或者  C:\Users\Administrator 目录下有一个 .gitconfig 的文件 ...