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. 必须使用"角色管理工具"安装或配置microsoft.net framework 3.5

    windows 2008 R2 安装.net framework 3.5报错 : 解决方法: (1)打开服务器管理器 (2)右击左侧功能——>添加功能 (3)向导中勾选.NET Framewor ...

  2. Tuple,Array,Map,文件操作

    Tuple是一个元组: 1,)一组元组中支持多个元素: 2,)一组元组中可以包含不同类型的元素: 3,)返回下标从_1开始. Array 数组 var items=Array(1,2,3,4) Map ...

  3. AngularJS基础概要整理(下)

    五.AngularJS Scope(作用域) Scope(作用域)是应用在HTML(视图)和JavaScript(控制器)之间的纽带. Scope是一个对象,有可用的方法和属性. Scope可应用在视 ...

  4. java操作MySQL数据库(插入、删除、修改、查询、获取所有行数)

    插播一段广告哈:我之前共享了两个自己写的小应用,见这篇博客百度地图开发的两个应用源码共享(Android版),没 想到有人找我来做毕设了,年前交付,时间不是很紧,大概了解了下就接下了,主要用到的就是和 ...

  5. 用OOP设计以下场景。太阳发出太阳光,照射在墙壁上,在地面形成影子。

    首先分析出有哪些实体类,太阳.太阳光.墙壁.地面.影子 然后分析太阳应该继承自发光体类.太阳光继承自光类.墙壁继承自物体类 地面是一个承载影子的容器.

  6. HDU 1176 免费馅饼

    免费馅饼 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  7. [Effective JavaScript 笔记]第65条:不要在计算时阻塞事件队列

    第61条解释了异步API怎样帮助我们防止一段程序阻塞应用程序的事件队列.使用下面代码,可以很容易使一个应用程序陷入泥潭. while(true){} 而且它并不需要一个无限循环来写一个缓慢的程序.代码 ...

  8. nginx安装ssl

    http://wiki.nginx.org/Modules#Standard_HTTP_modules 这里面带有所有基本的模块,及需要额外增加的模块 1.安装带有ssl模块的 nginx wget  ...

  9. Python之logging模块

    一.引言 之前在写一些小程序的时候想把日志内容打到文件中,所以就自己写了一个logger.py的程序,如下: #!/usr/bin/python # -*- coding=utf-8 -*- impo ...

  10. Python之基础知识

    一.数据类型和变量 计算机顾名思义就是做数学计算的机器,因此,计算机程序理所当然地可以处理各种数值.但是,计算机能处理的远不止数值,还可以处理文本.图形.音频.网页等各种各样的数据,不同的数据,需要定 ...