Problem UVA818-Cutting Chains

Accept:393  Submit:2087

Time Limit: 3000 mSec

 Problem Description

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

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
 

 Sample Ouput

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

题解:一看到n不超过15,向二进制的方向想是很自然的,顺着思路就出来了,暴力枚举情况,关键在于如何判断一个情况是成立的首先判环是肯定的,然后就是判断断开的个数是否大于等于连通分支的个数-1。这两点都很好想,容易忽略的就是如果一个环的分支数大于2也是不行的。这个虽然不太容易一下想到,但是样例有提示(良心样例),也不是什么困难的问题,代码都是套路。

 #include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#define INF 0x3f3f3f3f using namespace std; const int maxn = ;
int n;
int gra[maxn][maxn];
int vis[maxn]; bool dfs(const int sit,int fa,int u){
vis[u] = -;
for(int v = ;v < n;v++){
if(!gra[u][v] || vis[v]== || v==fa || !(sit&(<<v))) continue;
if(vis[v] < ) return false;
if(!vis[v] && !dfs(sit,u,v)) return false;
}
vis[u] = ;
return true;
} bool check(const int sit,int &res){
memset(vis,,sizeof(vis));
for(int u = ;u < n;u++){
if(!(sit&(<<u))) continue;
if(!vis[u]){
if(!dfs(sit,u,u)) return false;
res++;
}
} for(int u = ;u < n;u++){
if(!(sit&(<<u))) continue;
int cnt = ;
for(int v = ;v < n;v++){
if(gra[u][v] && sit&(<<v)) cnt++;
if(cnt > ) return false;
}
}
return true;
} int iCase = ; int main()
{
while(~scanf("%d",&n) && n){
int x,y;
memset(gra,,sizeof(gra));
while(scanf("%d%d",&x,&y) && (x!=- && y!=-)){
x--,y--;
gra[x][y] = gra[y][x] = ;
}
int Min = INF;
for(int i = (<<n)-;i >= ;i--){
int res = ;
if(check(i,res)){
int cnt = ;
for(int j = ;j < n;j++){
if(!(i&(<<j))) cnt++;
}
if(res- <= cnt) Min = min(Min,cnt);
if(Min == ) break;
}
}
printf("Set %d: Minimum links to open is %d\n",iCase++,Min);
}
return ;
}

UVA818-Cutting Chains(二进制枚举+dfs判环)的更多相关文章

  1. Atcoder Grand Contest 032C(欧拉回路,DFS判环)

    #include<bits/stdc++.h>using namespace std;int vis[100007];vector<int>v[100007];vector&l ...

  2. cf1278D——树的性质+并查集+线段树/DFS判环

    昨天晚上本来想认真打一场的,,结果陪女朋友去了.. 回来之后看了看D,感觉有点思路,结果一直到现在才做出来 首先对所有线段按左端点排序,然后用并查集判所有边是否联通,即遍历每条边i,和前一条不覆盖它的 ...

  3. cf374C Inna and Dima dfs判环+求最长链

    题目大意是有一个DIMA四种字母组成的矩阵,要在矩阵中找最长的DIMADIMADIMA……串,连接方式为四方向连接,问最长能找到多少DIMA.字母可以重复访问,如果DIMA串成环,即可以取出无限长的D ...

  4. 洛谷2444(Trie图上dfs判环)

    要点 并没问具体方案,说明很可能不是构造. 思考不断读入这个文本串,然后中间不出现某些文法的串.啊,这就是个自动机. 将不合法串使用ac自动机构成一个Trie图,我们需要的字符串就是在这个自动机上无限 ...

  5. CodeForces-1217D (拓扑排序/dfs 判环)

    题意 https://vjudge.net/problem/CodeForces-1217D 请给一个有向图着色,使得没有一个环只有一个颜色,您需要最小化使用颜色的数量. 思路 因为是有向图,每个环两 ...

  6. UVA-818 Cutting Chains (位压缩+暴力搜索)

    题目大意:一种环能打开和闭合.现在有n(1<=n<=15)个编号为1~n的环错综复杂的连接着,要打开一些环重新连接使这n个环能构成一条链,问最少需要打开几次环可达到目的? 题目分析:用二进 ...

  7. 2018 计蒜之道复赛 贝壳找房魔法师顾问(并查集+dfs判环)

    贝壳找房在遥远的传奇境外,找到了一个强大的魔法师顾问.他有 22 串数量相同的法力水晶,每个法力水晶可能有不同的颜色.为了方便起见,可以将每串法力水晶视为一个长度不大于 10^5105,字符集不大于  ...

  8. HDU 5215 Cycle(dfs判环)

    题意 题目链接 \(T\)组数据,给出\(n\)个点\(m\)条边的无向图,问是否存在一个奇环/偶环 Sol 奇环比较好判断吧,直接判是否是二分图就行了.. 偶环看起来很显然就是如果dfs到一个和他颜 ...

  9. BZOJ 1064 假面舞会(NOI2008) DFS判环

    此题,回想Sunshinezff学长给我们出的模拟题,原题啊有木有!!此处吐槽Sunshinezff爷出题不人道!! 不过也感谢Sunshinezff学长的帮助,我才能做出来.. 1064: [Noi ...

随机推荐

  1. Again Prime? No Time.(uva10870+数论)

    Again Prime? No time.Input: standard inputOutput: standard outputTime Limit: 1 second The problem st ...

  2. 面试官:"谈谈分库分表吧?"

    原文链接:面试官:"谈谈分库分表吧?" 面试官:“有并发的经验没?”  应聘者:“有一点.”   面试官:“那你们为了处理并发,做了哪些优化?”   应聘者:“前后端分离啊,限流啊 ...

  3. ThinkPHP登录功能的实现方法

    登陆功能是PHP程序设计中常见的功能.本文ThinkPHP实例主要完成注册成功后进入首页,并告诉你是登录用户的功能.具体实现步骤如下: 第一步:在config.php文件中加上: 完整实现代码如下: ...

  4. laravel接值 get post

    laravel使用一种简单的方式来访问用户提交的信息. 你可以用统一的方式来访问用户提交的信息,而不用为用户提交信息的方式操心. 引用类:use Illuminate\Support\Facades\ ...

  5. MongoDB日志文件过大的解决方法

    MongoDB的日志文件在设置 logappend=true 的情况下,会不断向同一日志文件追加的,时间长了,自然变得非常大. 解决如下:(特别注意:启动的时候必须是--logpath指定了log路径 ...

  6. 使用adb命令通过IP地址连接手机

    前提:已经通过USB设备线连接过电脑,并成功安装驱动. adb连接手机进行调试有两种方式,一种是使用USB线,另一种是使用无线WiFi. 第一种  使用USB线连接 1. 在手机上启用USB调试 2. ...

  7. 喜闻乐见-Android LaunchMode

    launchMode,通俗点说,就是定义了Activity应该如何被launch的.那么这几种模式的区别以及应用场景,会有何不同呢?谷歌是基于什么原因设计这几种模式的呢?这几种模式背后的工作原理是什么 ...

  8. vs添加到附加进程调试(IIS页面调试)

    有时候单元测试不是很方便,通过页面调试接口会更直观,也跟容易发现问题(尤其是在页面传参的时候),这时vs添加到附加进程的调试方式就显得尤为重要了! 步骤如下: 1.首先是通过IIS建立网站,(前提是要 ...

  9. SQL Server最大内存设为0后的处置办法

    故障说明: 远程调整实例内存时疏忽,将实例最大内存调整为了0,因此最大内存变成了128MB的最小值. 解决方式: 1.正常关闭SQL Server服务,如果是集群,需要先关停止集群角色防止故障转移,然 ...

  10. Linux端口映射,80端口映射到8080端口

    iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080 其中eth0为外网网卡名称 ipt ...