题意:给你一张图,问最少保留多少条边,使得这张图是边双联通分量。

思路:如果一个点集中的点已经是边双联通分量,那么从这个点集中的点x出发,经过若干个不是点集中的点,回到点集中的点y(x可能等于y),那么这条路径上的点和原来的点就构成了一个新的边双联通分量。

设dp[i]是状态i中的点构成边双联通分量需要的最少的边数,那么我们需要枚举dp[i]的子集,然后判断剩下的点能不能通过一条链串起来,如果可以,那么就是剩下的链的点的个数 + 1.那么怎么知道有没有链呢?这个也需要处理,设dp2[i][j][k]是从i开始,途中经过了点集k中的点,能不能到j(k中不包括i和j),直接dp处理就可以了。

代码:

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define lowbit(x) (x & (-x))
using namespace std;
vector<int> G[14];
int dp[1 << 14], dp2[14][14][1 << 14];
vector<int> re[1 << 14];
pair<int, int> last_pair[1 << 14];
int pre[1 << 14];
int last[14][14][1 << 14];
bool v[1 << 14];
int main() {
int n, m, x, y;
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
scanf("%d%d", &x, &y);
G[x - 1].push_back(y - 1);
G[y - 1].push_back(x - 1);
}
for (int i = 0; i < n; i++)
v[1 << i] = 1;
memset(dp, 0x3f, sizeof(dp));
dp[1] = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
for (int k = 0; k < (1 << n); k++) {
dp2[i][j][k] = INF;
}
for (int i = 0; i < n; i++)
for (auto x : G[i]) {
dp2[i][x][0] = 1;
last[i][x][0] = i;
}
for (int i = 0; i < (1 << n); i++) {
for (int j = 0; j < n; j++) {
if((i >> j) & 1) continue;
for (int k = 0; k < n; k++) {
if((i >> k) & 1) continue;
if(j == k || dp2[j][k][i] == INF) continue;
for (auto z : G[k]) {
if((i >> z) & 1) continue;
if(z == last[j][k][i]) continue;
int tmp = i ^ (1 << k);
if(dp2[j][z][tmp] == INF) {
dp2[j][z][tmp] = 1;
last[j][z][tmp] = k;
}
}
}
}
}
for (int i = 0; i < n; i++)
dp[1 << i] = 0;
// dp[1] = 0;
for (int i = 0; i < (1 << n); i++)
for (int j = 0; j < n; j++) {
if((i >> j) & 1)
re[i].push_back(j);
}
for (int i = 0; i < (1 << n); i++) {
for (int j = i; j; j = (j - 1) & i) {
int tmp = i ^ j;
int cnt = __builtin_popcount(j) + 1;
if(dp[i] < dp[tmp] + cnt) continue;
for (auto x : re[tmp])
for (auto y : re[tmp]) {
if(dp2[x][y][j] == 1) {
dp[i] = min(dp[i], dp[tmp] + cnt);
last_pair[i] = make_pair(x, y);
pre[i] = tmp;
}
}
}
}
if(dp[(1 << n) - 1] == INF) {
printf("-1\n");
} else {
printf("%d\n", dp[(1 << n) - 1]);
int now = (1 << n) - 1;
while(!v[now]) {
int x = last_pair[now].first, y = last_pair[now].second;
int tmp = now ^ pre[now];
while(tmp) {
int tmp1 = last[x][y][tmp];
printf("%d %d\n", y + 1, tmp1 + 1);
y = tmp1;
tmp ^= (1 << tmp1);
}
printf("%d %d\n", x + 1, y + 1);
now = pre[now];
}
}
}

  

Codeforces 1155F 状压DP的更多相关文章

  1. Codeforces 678E 状压DP

    题意:有n位选手,已知n位选手之间两两获胜的概率,问主角(第一个选手)最终站在擂台上的概率是多少? 思路:一看数据范围肯定是状压DP,不过虽然是概率DP,但是需要倒着推:我们如果正着推式子的话,初始状 ...

  2. Codeforces 8C 状压DP

    题意:有个人想收拾行李,而n个物品散落在房间的各个角落里(n < 24).现在给你旅行箱的坐标(人初始在旅行箱处),以及n个物品的坐标,你一次只能拿最多两个物品,并且拿了物品就必须放回旅行箱,不 ...

  3. Codeforces 1215E 状压DP

    题意:给你一个序列,你可以交换序列中的相邻的两个元素,问最少需要交换多少次可以让这个序列变成若干个极大的颜色相同的子段. 思路:由于题目中的颜色种类很少,考虑状压DP.设dp[mask]为把mask为 ...

  4. CodeForces 11D(状压DP 求图中环的个数)

    Given a simple graph, output the number of simple cycles in it. A simple cycle is a cycle with no re ...

  5. codeforces 1185G1 状压dp

    codeforces 1185G1. Playlist for Polycarp (easy version)(动态规划) 传送门:https://codeforces.com/contest/118 ...

  6. Codeforces - 71E 状压DP

    参考官方题解 #include<bits/stdc++.h> #define rep(i,j,k) for(register int i=j;i<=k;i++) #define rr ...

  7. codeforces Diagrams & Tableaux1 (状压DP)

    http://codeforces.com/gym/100405 D题 题在pdf里 codeforces.com/gym/100405/attachments/download/2331/20132 ...

  8. Codeforces Gym 100015F Fighting for Triangles 状压DP

    Fighting for Triangles 题目连接: http://codeforces.com/gym/100015/attachments Description Andy and Ralph ...

  9. Codeforces Gym 100610 Problem K. Kitchen Robot 状压DP

    Problem K. Kitchen Robot Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10061 ...

随机推荐

  1. mysql基于GTIDS复制

    GTIDS的环境:一台master 192.168.200.111多个slave: 192.168.200.112 192.168.200.113 修改master服务器:[root@localhos ...

  2. 何时使用move

    https://zhidao.baidu.com/question/1514190267640555740.html 但编译器有copy elision优化,相当于原地构造,效率要高于move,手动指 ...

  3. 六、SpringBoot配置@ConfigurationProperties与@Value区别

    1.@Value的使用 三种用法 // ${key} 从环境变量.配置文件中取值 @Value("${person.last-name}") private String last ...

  4. Welcome to MarkdownPad 2

    Welcome to MarkdownPad 2 MarkdownPad is a full-featured Markdown editor for Windows. Built exclusive ...

  5. 力扣 ——4Sum (四数之和)python 实现

    题目描述: 中文: 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 targe ...

  6. 使用CSS3的@media来编写响应式的页面

    首先要知道,我们为什么要写自适应的页面(响应式页面) [直接看干货] 众所周知,电脑.平板.手机的屏幕是差距很大的,假如在电脑上写好了一个页面,在电脑上看起来不错,但是如果放到手机上的话,那可能就会乱 ...

  7. java中形参中的 “. . .” 是什么意思

    如这个jdbc中封装的绑定参数的方法: /** * 绑定参数 * @param pstmt * @param os */ public static void executebindParam(Pre ...

  8. 【leetcode】942. DI String Match

    题目如下: Given a string S that only contains "I" (increase) or "D" (decrease), let  ...

  9. 【Guava】Guava Cache用法

    背景 缓存的主要作用是暂时在内存中保存业务系统的数据处理结果,并且等待下次访问使用.在日长开发有很多场合,有一些数据量不是很大,不会经常改动,并且访问非常频繁.但是由于受限于硬盘IO的性能或者远程网络 ...

  10. JindoFS解析 - 云上大数据高性能数据湖存储方案

    JindoFS背景 计算存储分离是云计算的一种发展趋势,传统的计算存储相互融合的的架构存在一定的问题, 比如在集群扩容的时候存在计算能力和存储能力相互不匹配的问题,用户在某些情况下只需要扩容计算能力或 ...