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 ...
随机推荐
- struts2_模型驱动
一.注意点 建立实现ModelDriven接口的action类 在该action类中,创建实体对象并new 在getModel返回该对象 在显示页面中提交的表单name正常写 二.案例 创建实体类Us ...
- Java高并发 -- J.U.C.组件扩展
Java高并发 -- J.U.C.组件扩展 主要是学习慕课网实战视频<Java并发编程入门与高并发面试>的笔记 FutureTask Future模式,核心思想是异步调用.和同步调用的区别 ...
- JavaScript是如何工作的:Web Workers的构建块 + 5个使用他们的场景
摘要: 理解Web Workers. 原文:JavaScript是如何工作的:Web Workers的构建块 + 5个使用他们的场景 作者:前端小智 Fundebug经授权转载,版权归原作者所有. 这 ...
- Session的原理,大型网站中Session方面应注意什么?
一.Session和Cookie的区别Session是在服务器端保持会话数据的一种方法(通常用于pc端网站保持登录状态,手机端通常会使用token方式实现),存储在服务端. Cookie是在客户端保持 ...
- jQuery中$.ajax()方法参数解析
本文实例为大家讲解了jQuery $.ajax()方法参数,供大家参考,具体内容如下 $.ajax({ url:'test.do', data:{id:123,name:'xiaoming'}, ty ...
- Android为TV端助力 MediaPlayer的一些使用方法简历
这里提供一些MediaPlayer的简单方法,方便以后熟练的使用它! 1)如何获得MediaPlayer实例: 可以使用直接new的方式: MediaPlayer mp = new MediaPlay ...
- (后端)maven仓库
仓库网址:http://mvnrepository.com/artifact/org.springframework/spring-core 可以去选择评分高的jar,复制: <!-- http ...
- 关于正餐智能POS6.0.1.1改版后,点击反结账进入点菜界面后无法进行加菜的FAQ
适用版本:智能POS正餐V6.0.1.1+适用情况:点击反结账进入点菜界面后无法进行加菜.原因:为让报表统计的数据更准确解决方案:1.点击反结账,输入用户密码,进入点菜界面. 2.点击结算,进入收银台 ...
- jdk各版本特性
JDK Version 1.0 开发代号为Oak(橡树),于1996-01-23发行. JDK Version 1.1 于1997-02-19发行. 引入的新特性包括: 引入JDBC(Java Dat ...
- shell的case用法
今天给大家简单介绍一下结构条件语句的用法,实际上就是规范的多分支if语句,如下: case语法: case "字符串变量" in 值1)指令1... ;; 值2)指令2... ;; ...