紫书搜索 习题7-4 UVA - 818 Cutting Chains 暴力+dfs判环+位运算
题目链接:
https://vjudge.net/problem/UVA-818
题意:
选几个圆环去open。然后该圆环和其他就断开了。然后用这些open的圆环去连接剩下的圆环【最后打开的会合上】,看能不能连成一串。。求最少的open个数。
题解:
n为15.利用位运算去枚举哪几个圆环要open。然后判断剩下圆环有没有与超过2个圆环的连接或者形成环,如果没有,在判断剩下的链个数有没有超过open个数-1.如果条件都符合,那么保留下最小最为答案。
代码:
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 #define MS(a) memset(a,0,sizeof(a)) 5 #define MP make_pair 6 #define PB push_back 7 const int INF = 0x3f3f3f3f; 8 const ll INFLL = 0x3f3f3f3f3f3f3f3fLL; 9 inline ll read(){ 10 ll x=0,f=1;char ch=getchar(); 11 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 12 while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} 13 return x*f; 14 } 15 // 16 const int maxn = 1e5+10; 17 18 int n,vis[20],g[20][20],cnt; 19 20 bool two(int s){ 21 for(int i=0; i<n; i++){ 22 if(s & (1<<i)) continue; 23 int cnt = 0; 24 for(int j=0; j<n; j++){ 25 if(s & (1<<j)) continue; 26 if(g[i][j] && i!=j) cnt++; 27 } 28 if(cnt > 2) return true; 29 } 30 31 return false; 32 } 33 34 bool dfs(int s,int now,int fa){ 35 vis[now] = 1; 36 for(int i=0; i<n; i++){ 37 if(!g[now][i] || s&(1<<i) || i==fa) continue; 38 if(vis[i]) return true; 39 if(dfs(s,i,now)) return true; 40 } 41 return false; 42 } 43 44 bool circle(int s){ 45 for(int i=0; i<n; i++){ 46 if(s & (1<<i)) continue; 47 if(vis[i]) continue; 48 cnt++; 49 if(dfs(s,i,-1)) return true; 50 } 51 return false; 52 } 53 54 int cal(int x){ 55 return x==0 ? 0 : cal(x/2)+(x%2); 56 } 57 58 int main(){ 59 int cas = 1; 60 while(scanf("%d",&n) && n){ 61 MS(g); 62 int u,v; 63 while(scanf("%d%d",&u,&v)){ 64 if(u==-1 && v==-1) break; 65 g[u-1][v-1] = g[v-1][u-1] = 1; 66 } 67 int s = (1<<n); 68 int ans = INF; 69 for(int i=0; i<s; i++){ 70 MS(vis); cnt=0; // cnt 表示 如果剩下的环中,没有连成圈的, 那有几条链 71 if(two(i) || circle(i)) continue; 72 if(cal(i) >= cnt-1) // 如果拆下来的环 大于链数-1 才能连成一条链 73 ans = min(ans,cal(i)); 74 } 75 printf("Set %d: Minimum links to open is %d\n",cas++,ans); 76 } 77 78 return 0; 79 }
转载于:https://www.cnblogs.com/yxg123123/p/6827600.html
紫书搜索 习题7-4 UVA - 818 Cutting Chains 暴力+dfs判环+位运算相关推荐
- 紫书搜索 习题7-8 UVA - 12107 Digit Puzzle IDA*迭代加深搜索
题目链接: https://vjudge.net/problem/UVA-12107 题意: 给出一个数字谜,要求修改尽量少的数,使修改后的数字谜只有唯一解.空格和数字可以随意替换,但不能增删,数字谜 ...
- 紫书搜索 习题7-6 UVA - 12113 Overlapping Squares 暴力
题目链接: https://vjudge.net/problem/UVA-12113 题意: 能不能用不超过6张2x2的方纸在4x4的方格中摆出给定的图形? 题解: 最多放9个正方形,暴力枚举每个正方 ...
- UVA 818 Cutting Chains(状压 + 暴搜)题解
题意:有1~n个小环,他们中的有些互相扣在一起,问你至少切开几个能把这写小环串成一条链 思路:还是太菜了,题目给的n<=15,显然可以暴力解决. 用二进制表示每个环切还是不切,然后搜索所有情况. ...
- UVA 818 Cutting Chains 切断圆环链 (暴力dfs)
题意就是给一张无向图,去掉某些结点,然后连成一条链,问最少去掉几个结点. n很小n<=15,所以直接枚举2^15个状态就行啦. 链的条件是1.无环,2.没有度大于2的点,3.把n个散链连起来需要 ...
- 紫书搜索 例题7-10 UVA - 11212 Editing a Book 迭代加深搜索 IDA*
题目链接: https://vjudge.net/problem/UVA-11212 题意: 题解: IDA*,每次改变深度上限去剪枝 代码: 1 #include <bits/stdc++.h ...
- 紫书搜索 例题7-5 UVA - 129 Krypton Factor
题目链接: https://vjudge.net/problem/UVA-129 题意: 题解: 代码: #include <bits/stdc++.h> using namespace ...
- 紫书动规 例题9-10 UVA - 1626 Brackets sequence 区间dp
题目链接: https://vjudge.net/problem/UVA-1626 题意: 题解: dp[i][j]:= i~j需要最少的括号 区间dp: dp[i][j] = min(dp[i][j ...
- 16行代码AC——紫书| 例题7-3 Fractions Again?! (UVA - 10976)_时间复杂度O(n)
励志用尽量少的代码做高效表达 Problem describe It is easy to see that for every fraction in the form 1/k(k > 0), ...
- 紫书动规 例题9-13 UVA - 1220 Party at Hali-Bula 树形dp
题目链接: https://vjudge.net/problem/UVA-1220 题意: 题解: 树形dp: 树的最大独立集问题 d[u][0]:=不选u能得到的最大人数 d[u][0]=sum{m ...
最新文章
- android ble mesh,Android 蓝牙Mesh组网代码详解
- 在Ubuntu 14.04 64bit上安装下载管理器FlareGet 4.2.92
- 【部分原创】python实现视频内的face swap(换脸)
- volatile和内存屏障(dmb)
- HDU1002 Problem II(大数相加)(C++题解)
- ionic ajax 跨域,解决 ionic 中的 CORS(跨域) 问题
- Mybatis 学习日记(1)
- 地表地形对地下温度及地表热流的影响
- python学习中遇到的问题
- 傻子坐飞机问题的求解
- 津津的储蓄计划(洛谷-P1089)
- CSS flex 盒子在 Chrome 和 Safari 中的行为差异
- C/C++遍历Windows文件夹下的所有文件
- readelf命令解析及演示
- 一阶惯性环节如何实现跟踪性能与滤波性能共存(三)
- 新冠病毒对计算机的影响,人工智能给新冠病毒分类
- 三亚科技创新创业大赛完美落幕,珈和科技等五个企业获一等奖
- 蓝牙音乐和导航语音播放混音卡顿问题
- python 支付宝接口_python实现支付宝转账接口
- 计算机数据存储原理 以及 进制