Codeforces 1155F 状压DP
题意:给你一张图,问最少保留多少条边,使得这张图是边双联通分量。
思路:如果一个点集中的点已经是边双联通分量,那么从这个点集中的点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的更多相关文章
- Codeforces 678E 状压DP
题意:有n位选手,已知n位选手之间两两获胜的概率,问主角(第一个选手)最终站在擂台上的概率是多少? 思路:一看数据范围肯定是状压DP,不过虽然是概率DP,但是需要倒着推:我们如果正着推式子的话,初始状 ...
- Codeforces 8C 状压DP
题意:有个人想收拾行李,而n个物品散落在房间的各个角落里(n < 24).现在给你旅行箱的坐标(人初始在旅行箱处),以及n个物品的坐标,你一次只能拿最多两个物品,并且拿了物品就必须放回旅行箱,不 ...
- Codeforces 1215E 状压DP
题意:给你一个序列,你可以交换序列中的相邻的两个元素,问最少需要交换多少次可以让这个序列变成若干个极大的颜色相同的子段. 思路:由于题目中的颜色种类很少,考虑状压DP.设dp[mask]为把mask为 ...
- CodeForces 11D(状压DP 求图中环的个数)
Given a simple graph, output the number of simple cycles in it. A simple cycle is a cycle with no re ...
- codeforces 1185G1 状压dp
codeforces 1185G1. Playlist for Polycarp (easy version)(动态规划) 传送门:https://codeforces.com/contest/118 ...
- Codeforces - 71E 状压DP
参考官方题解 #include<bits/stdc++.h> #define rep(i,j,k) for(register int i=j;i<=k;i++) #define rr ...
- codeforces Diagrams & Tableaux1 (状压DP)
http://codeforces.com/gym/100405 D题 题在pdf里 codeforces.com/gym/100405/attachments/download/2331/20132 ...
- Codeforces Gym 100015F Fighting for Triangles 状压DP
Fighting for Triangles 题目连接: http://codeforces.com/gym/100015/attachments Description Andy and Ralph ...
- 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 ...
随机推荐
- eclipse调试代码无法查看jdk变量解决方法
1.无法查看jdk变量原因 oracle在公司在编译jdk的时候,把debug给关闭了,现在需要我们自己重新打包编译一次 2.eclipse创建一个普通java项目,取名叫jdk 3.导入jdk源码, ...
- 浅谈maven自动化构建工具
转载https://blog.csdn.net/zxm1306192988/article/details/76209062 Maven是什么[what] 1.Maven 是 Apache 软件基金会 ...
- oracle死锁查询
select sess.sid ||','|| sess.serial#, lo.oracle_username, lo.os_user_name, ao.object_name, lo.locked ...
- qt 学习(六) 数据库注册用户
做什么: 1 登陆按钮按下出现注册页面, 2 输入账号 判断是否可用 查询数据库,用户名是否已经注册 3 输入密码 判断密码格式 4 输入邮箱 判断邮箱格式 查询数据库,邮箱是否已经注册 ...
- uni-app获取元素宽高封装
getElSize(id) { //得到元素的size return new Promise((res, rej) => { uni.createSelectorQuery().select(' ...
- CentOS 7.0 lamp
CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙. 1.关闭firewall: systemctl stop firewalld.service #停止fir ...
- Qt获取文件路径、文件夹路径
1.首先是选择文件 QString file_path = QFileDialog::getOpenFileName(this, "请选择文件路径...", "默认路径( ...
- 改进持续交付中的CI环节
改进持续交付中的CI环节 在当前 DevOps 的趋势下,持续集成(CI)和持续部署(CD)具有支柱性地位,那么能够成功搭建 CI/CD 流水线就至关重要了. 今天我就讲一讲如何做好CI部分,让我们的 ...
- Mamen所需要的jar包怎么生成
Mamen所需要的jar包怎么生成 使用 mamen 难免碰到,不知道的 jar 包,不知道怎么在 pom 文件中写,分享一个网址,可以把你想要的 jar 包生成 pom 配置文件,个人感觉非常好用. ...
- PHP面试 MySQL的高可扩展和高可用
MySQL的高可扩展和高可用 面试题一 MySQL分表和分区的工作原理,分表和分区的使用场景和优缺点. 分区表的原理 对用户而言,分区表时一个独立的逻辑表,但是底层MySQL将其分成了多个物理子表,这 ...