UVA818-Cutting Chains(二进制枚举+dfs判环)
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
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判环)的更多相关文章
- Atcoder Grand Contest 032C(欧拉回路,DFS判环)
#include<bits/stdc++.h>using namespace std;int vis[100007];vector<int>v[100007];vector&l ...
- cf1278D——树的性质+并查集+线段树/DFS判环
昨天晚上本来想认真打一场的,,结果陪女朋友去了.. 回来之后看了看D,感觉有点思路,结果一直到现在才做出来 首先对所有线段按左端点排序,然后用并查集判所有边是否联通,即遍历每条边i,和前一条不覆盖它的 ...
- cf374C Inna and Dima dfs判环+求最长链
题目大意是有一个DIMA四种字母组成的矩阵,要在矩阵中找最长的DIMADIMADIMA……串,连接方式为四方向连接,问最长能找到多少DIMA.字母可以重复访问,如果DIMA串成环,即可以取出无限长的D ...
- 洛谷2444(Trie图上dfs判环)
要点 并没问具体方案,说明很可能不是构造. 思考不断读入这个文本串,然后中间不出现某些文法的串.啊,这就是个自动机. 将不合法串使用ac自动机构成一个Trie图,我们需要的字符串就是在这个自动机上无限 ...
- CodeForces-1217D (拓扑排序/dfs 判环)
题意 https://vjudge.net/problem/CodeForces-1217D 请给一个有向图着色,使得没有一个环只有一个颜色,您需要最小化使用颜色的数量. 思路 因为是有向图,每个环两 ...
- UVA-818 Cutting Chains (位压缩+暴力搜索)
题目大意:一种环能打开和闭合.现在有n(1<=n<=15)个编号为1~n的环错综复杂的连接着,要打开一些环重新连接使这n个环能构成一条链,问最少需要打开几次环可达到目的? 题目分析:用二进 ...
- 2018 计蒜之道复赛 贝壳找房魔法师顾问(并查集+dfs判环)
贝壳找房在遥远的传奇境外,找到了一个强大的魔法师顾问.他有 22 串数量相同的法力水晶,每个法力水晶可能有不同的颜色.为了方便起见,可以将每串法力水晶视为一个长度不大于 10^5105,字符集不大于 ...
- HDU 5215 Cycle(dfs判环)
题意 题目链接 \(T\)组数据,给出\(n\)个点\(m\)条边的无向图,问是否存在一个奇环/偶环 Sol 奇环比较好判断吧,直接判是否是二分图就行了.. 偶环看起来很显然就是如果dfs到一个和他颜 ...
- BZOJ 1064 假面舞会(NOI2008) DFS判环
此题,回想Sunshinezff学长给我们出的模拟题,原题啊有木有!!此处吐槽Sunshinezff爷出题不人道!! 不过也感谢Sunshinezff学长的帮助,我才能做出来.. 1064: [Noi ...
随机推荐
- python基础学习(四)if判断语句
if判断语句的基本语法 在python中,if判断的格式如下: if 条件: 条件成立时,执行的语句 ...... 注意:代码的缩进要使用一个tab键或者四个空格(建议使用四个空格,tab和空格最好不 ...
- express入门
(1)express的安装 $ npm install express 或者 $ npm install -g express 或者 $ npm install express -gd 备注: -g ...
- angularJs学习笔记-路由
1.angular路由介绍 angular路由功能是一个纯前端的解决方案,与我们熟悉的后台路由不太一样. 后台路由,通过不同的 url 会路由到不同的控制器 (controller) 上,再渲染(re ...
- URL 与 URI 介绍
URL: 统一资源定位符 ( Uniform Resource Locator ) URI: 统一资源标识符 ( Uniform Resource Identifier ) URL 地址:https: ...
- es6 语法 (let 和const)
一.let 和const 1.let 只在自己声明的块作用域中有效: function test(){ let a = 'a'; var b = 'b'; for(let i =1;i<3;i+ ...
- 苹果8plus怎么录屏视频
现在越来越多的手机控,不管在什么地方,什么时候,都是低头看手机的居多,因为手机信息量太大了,一部手机就可以了解最新咨询,但是作为苹果8plus怎么录制手机屏幕,你们知道吗?今天就和大家一起分享苹果8p ...
- RPC框架学习总结
1.RPC是一种技术框架的称呼,不是某种具体协议,不局限于某种协议,RPC顾名思义就是远程过程调用,其核心思想是,RPC客户端调用远程服务器上的接口完成过程调用,远程服务器把结果返回. 2.RPC的最 ...
- Android为TV端助力 转载:Java 泛型
一. 泛型概念的提出(为什么需要泛型)? 首先,我们看下下面这段简短的代码: 1 public class GenericTest { 2 3 public static void main(Stri ...
- Testlink1.9.17使用方法(第十三章 使用中遇到的问题)
第十三章 使用中遇到的问题 一. 登录Testlink后,新建一个项目后,会出现如下提示: 解决办法:打开Testlink安装文件夹下的config.inc.php文件, 原来:$tlCfg-> ...
- Struts2之action 之 感叹号 ! 动态方法调用
struts2的动态方法调用的方式: 1.第一种方式:设置method属性 在Action类中定义一个签名与execute方法相同.只是名字不同的方法,如定义为: public String logi ...