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 ...
随机推荐
- ORA-00959: tablespace 'PSAPTEMP' does not exist
错误 : ORA-00959: tablespace 'PSAPTEMP' does not exist 解决办法: CREATE TEMPORARY TABLESPACE PSAPTEMP TEM ...
- Android破解学习之路(十六)—— dll破解的IL指令
IL指令介绍 IL是.NET框架中中间语言(Intermediate Language)的缩写. 使用.NET框架提供的编译器可以直接将源程序编译为.exe或.dll文件,但此时编译出来的程序代码并不 ...
- List集合遍历整理
遍历List集合的三种方式 0. 首先准备测试数据 List<String> list = new ArrayList<String>(); list.add("Ja ...
- 【Java基础】16、小数的浮点型和定点型
一.简介 1.对于不需要任何准确计算精度的数字可以直接使用浮点型,但是如果需要精确计算的结果,则必须使用定点型(BigDecimal类) 浮点型:float,double 定点型:BigDecimal ...
- Java静态数据的初始化
Java中无论创建多少对象,静态数据都只占一份存储区域. 下面程序示例静态存储区域的初始化: //: initialization/StaticInitialization.java // Speci ...
- java ee,包括js,html,jsp等等知识整合
为了方便修改和后续的包装套路 首先用户访问的页面从web.xml找到 <welcome-file-list> <welcome-file>index.html</we ...
- TS学习随笔(一)->安装和基本数据类型
去年学过一段时间的TS,但由于在工作中不常用.就生疏了,最近项目要求用TS,那我就再回去搞搞TS,写一篇记录一下自己学习TS的进度以及TS知识点 首先,关于TS的定义我就不在这描述了,想看百度一下你就 ...
- Android为TV端助力 清除本应用里的各种数据的方法
public class DataCleanManager { /** * * 清除本应用内部缓存(/data/data/com.xxx.xxx/cache) * * * * @param conte ...
- Echarts纵坐标显示为整数小数
chart.DoubleDeckBarChart = function (getIDParam, Legend, xAxisData, seriesName1, seriesName2, series ...
- LRU(最近最少使用淘汰算法)基本实现
LRU(Least Recently Used) 出发点:在页式存储管理中,如果一页很长时间未被访问,则它在最近一段时间内也不会被访问,即时间局部性,那我们就把它调出(置换出)内存. 为了实现LRU ...