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 当 它存在 ...
随机推荐
- 第七章 : Git 介绍 (上)[Learn Android Studio 汉化教程]
Learn Android Studio 汉化教程 [翻译]Git介绍 Git版本控制系统(VCS)快速成为Android应用程序开发以及常规的软件编程领域内的事实标准.有别于需要中心服务器支持的早期 ...
- 代做JSP课程设计,毕业设计
代做JSP课程设计,毕业设计,大家都是学生,绝对靠谱,有意者加我Q 279283855
- openLayers 3 之入门
openLayers 3 之入门 openlayer是web GIS客户端开发提供的javascript类库,也是开源框架,可以加载本地数据进行展示地图 1.下载相关引用的js.css文件 2.类似于 ...
- #if (DEBUG)
//DEBUG必须大写,其它是不认的#if (DEBUG) Console.WriteLine("Debug");#else Conso ...
- 运行Junit方法项目启动不了
从控制台看不出任何有用信息,通过JUnit右键”Copy Failure List”将信息拷贝出来 TestStart.start initializationError(org.junit.runn ...
- Using Mono DLLs in a Unity Project
[Using Mono DLLs in a Unity Project] The path to the Unity DLLs will typically be: 一个生成dll的例子如下: mcs ...
- easyui 验证框
转自:http://blog.csdn.net/pqszq1314/article/details/25896163 例如 校验输入框只能录入0-1000之间 最多有2位小数的数字 表单<inp ...
- JS的事件汇总
一.前言 事件的绑定触发有很多种方法,我们如何选择呢? 很多时候,我们会使用鼠标事件,但是鼠标事件只能在PC设备上使用,当我们需要对不同设备兼容时怎么办呢? 二.正文 1. 事件的几个概念: 事件流: ...
- 五分钟带你入门TensorFlow
TensorFlow是Google开源的一款人工智能学习系统.为什么叫这个名字呢?Tensor的意思是张量,代表N维数组:Flow的意思是流,代表基于数据流图的计算.把N维数字从流图的一端流动到另一端 ...
- Java基础知识常识总结
Java基础知识常识总结 1.面向对象的特征以及对他的理解? 封装,继承,多态,如果再增加一条就是抽象. 2.Object有几种方法,分别是什么? 在JDK1.5中的11种方法,分别有: toStri ...