CodeForces 907E Party(bfs+状压DP)
Arseny likes to organize parties and invite people to it. However, not only friends come to his parties, but friends of his friends, friends of friends of his friends and so on. That's why some of Arseny's guests can be unknown to him. He decided to fix this issue using the following procedure.
At each step he selects one of his guests A, who pairwise introduces all of his friends to each other. After this action any two friends of Abecome friends. This process is run until all pairs of guests are friends.
Arseny doesn't want to spend much time doing it, so he wants to finish this process using the minimum number of steps. Help Arseny to do it.
The first line contains two integers n and m (1 ≤ n ≤ 22; ) — the number of guests at the party (including Arseny) and the number of pairs of people which are friends.
Each of the next m lines contains two integers u and v (1 ≤ u, v ≤ n; u ≠ v), which means that people with numbers u and v are friends initially. It's guaranteed that each pair of friends is described not more than once and the graph of friendship is connected.
In the first line print the minimum number of steps required to make all pairs of guests friends.
In the second line print the ids of guests, who are selected at each step.
If there are multiple solutions, you can output any of them.
5 6
1 2
1 3
2 3
2 5
3 4
4 5
2
2 3
4 4
1 2
1 3
1 4
3 4
1
1
In the first test case there is no guest who is friend of all other guests, so at least two steps are required to perform the task. After second guest pairwise introduces all his friends, only pairs of guests (4, 1) and (4, 2) are not friends. Guest 3 or 5 can introduce them.
In the second test case guest number 1 is a friend of all guests, so he can pairwise introduce all guests in one step.
题意:给出n个人和他们之间的友谊关系,每次可以让一个人的所有朋友都认识,求最少要选出几个人才能让所有人都互相认识?(有先后顺序)并且输出方案
题解:这道题一开始想到的是zz无脑状压,结果有不能保证一定正解,接着想到了之前某场bfs式的状压,然后就过了,方案什么的写个栈记录一下就行了,dfs什么的也是可以做的,但是不如bfs直接啦~
代码如下:
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; int f[(<<)+],pre[(<<)+],key[(<<)+];
int a[],ans[],cnt,n,m;
queue<int> q; void bfs()
{
memset(f,,sizeof(f));
memset(key,-,sizeof(key));
memset(pre,-,sizeof(pre));
for(int i=;i<n;i++)
{
f[a[i]]=;
key[a[i]]=i;
q.push(a[i]);
}
int now,tmp;
while(!q.empty())
{
now=q.front();
q.pop();
for(int i=;i<n;i++)
{
if(now&(<<i))
{
tmp=now|a[i];
if(!f[tmp])
{
f[tmp]=f[now]+;
key[tmp]=i;
pre[tmp]=now;
q.push(tmp);
if(tmp==((<<n)-))
{
return ;
}
}
}
}
}
} int main()
{
scanf("%d%d",&n,&m);
int from,to;
for(int i=;i<n;i++)
{
a[i]|=(<<i);
}
for(int i=;i<=m;i++)
{
scanf("%d%d",&from,&to);
from--;to--;
a[from]|=(<<to);
a[to]|=(<<from);
}
if(m==n*(n-)/)
{
puts("");
return ;
}
bfs();
cnt=;
int now=(<<n)-;
while(~now)
{
ans[++cnt]=key[now];
now=pre[now];
}
printf("%d\n",cnt);
while(cnt)
{
printf("%d",ans[cnt--]+);
if(cnt)
printf(" ");
else
printf("\n");
}
}
CodeForces 907E Party(bfs+状压DP)的更多相关文章
- Codeforces 79D - Password(状压 dp+差分转化)
Codeforces 题目传送门 & 洛谷题目传送门 一个远古场的 *2800,在现在看来大概 *2600 左右罢( 不过我写这篇题解的原因大概是因为这题教会了我一个套路罢( 首先注意到每次翻 ...
- codeforces Diagrams & Tableaux1 (状压DP)
http://codeforces.com/gym/100405 D题 题在pdf里 codeforces.com/gym/100405/attachments/download/2331/20132 ...
- hdu 4856 Tunnels (bfs + 状压dp)
题目链接 The input contains mutiple testcases. Please process till EOF.For each testcase, the first line ...
- HDU-4856 Tunnels (BFS+状压DP)
Problem Description Bob is travelling in Xi’an. He finds many secret tunnels beneath the city. In hi ...
- 孤岛营救问题(BFS+状压DP)
孤岛营救问题 https://www.luogu.org/problemnew/show/P4011 用状压DP标记拿到钥匙的数量 #include<iostream> #include& ...
- QDUOJ 来自xjy的签到题(bfs+状压dp)
来自xjy的签到题 Description 爱丽丝冒险来到了红皇后一个n*n大小的花园,每个格子由'.'或'#'表示,'.'表示爱丽丝可以到达这个格子,‘#’表示爱丽丝不能到达这个格子,爱丽丝每1 ...
- HDU-3681-Prison Break(BFS+状压DP+二分)
Problem Description Rompire is a robot kingdom and a lot of robots live there peacefully. But one da ...
- Codeforces 917C - Pollywog(状压 dp+矩阵优化)
UPD 2021.4.9:修了个 typo,为啥写题解老出现 typo 啊( Codeforces 题目传送门 & 洛谷题目传送门 这是一道 *2900 的 D1C,不过还是被我想出来了 u1 ...
- Codeforces 544E Remembering Strings 状压dp
题目链接 题意: 给定n个长度均为m的字符串 以下n行给出字符串 以下n*m的矩阵表示把相应的字母改动成其它字母的花费. 问: 对于一个字符串,若它是easy to remembering 当 它存在 ...
随机推荐
- IDA Pro 权威指南学习笔记(十三) - 基本代码转换
IDA提供的代码转换包括: 1.将数据转换为代码 2.将代码转换为数据 3.指定一个指令序列为函数 4.更改现有函数的起始或结束地址 5.更改指令操作数的显示格式 代码显示选项 通过 Options ...
- 正则表达式RE与扩展正则表达式ERE——grep与egrep
grep 正则表达式规则: ^ 行首定位符,表示从行首开始进行模式匹配 . 一个非换行符的字符 [ ] 匹配属于此集合的任意一个字符 [^ ] 匹配不属于此集合的任意一个字符 [a-z] (其指定的集 ...
- mybatis No enum const class org.apache.ibatis.type.JdbcType.Date 坑爹的配置
转自:https://lihaiming.iteye.com/blog/2248059 在ibatis中不需要关注这些参数 而转到mybatis后 如果字段值为空 必须设置jdbcType如inser ...
- python 中类的初始化过程
首先元类中的__new__被调用 所有使用该元类的类都会调用一次,不管其有没有初始化,所以元类__new__的作用是修改/验证类的定义 返回的是一个元类的实例,即一个类的定义 元类的__init__由 ...
- Python与Go冒泡排序
#!/usr/bin/env python # -*- coding: utf-8 -*- # 冒泡排序法 def bubbling(array): # 时间复杂度:O(n^2) for i in r ...
- Mono在Full AOT模式下的限制
[Mono在Full AOT模式下的限制] 调试时遇到一个Mono运行时异常: ExecutionEngineException: Attempting to JIT compile method ' ...
- Python 爬虫之 Scrapy 分布式原理以及部署
Scrapy分布式原理 关于Scrapy工作流程 Scrapy单机架构 上图的架构其实就是一种单机架构,只在本机维护一个爬取队列,Scheduler进行调度,而要实现多态服务器共同爬取数据关键就是共享 ...
- Opencv 发现轮廓 findContours
vector<vector<Point>> vec_p; vector<Vec4i> vec_4f; findContours(img_canny1, vec_p, ...
- OceanBase
OceanBase 编辑 本词条缺少名片图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧! OceanBase是一个支持海量数据的高性能分布式数据库系统,实现了 数千亿条记录.数百TB数据上的 ...
- vmware Selinux配置错误,导致无法启动虚拟机
Linux 开机提示kernel panic - not syncing: Attempted to kill init! 解决方法: 系统启动的时候,按下‘e’键进入grub编辑界面,编辑grub菜 ...