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 权威指南学习笔记(十) - 栈帧
栈帧(stack frame)是在程序的运行时栈中分配的内存块,用于特定的函数调用 如果一个函数没有执行则不需要内存,当函数被调用时就需要用到内存 1.传给函数的参数的值需要存储到函数能够找到它们的位 ...
- js控制手机震动
js控制手机震动 <button onclick="vibrate()">震动</button> <script> function vibra ...
- Docker垃圾回收机制
由Docker垃圾回收机制引发的一场血案 AlstonWilliams 关注 2017.04.01 19:00* 字数 1398 阅读 253评论 0喜欢 0 今天早晨,在我还没睡醒的时候,我们团队中 ...
- RSA_JS_PHP加密解密
root@DESKTOP-I4OIMJC /cygdrive/e/html/RSA_JS_PHP/openssl/bin # ./openssl.exe OpenSSL> genrsa -out ...
- Flask之模板过滤器
3.2 过滤器: 过滤器的本质就是函数.有时候我们不仅仅只是需要输出变量的值,我们还需要修改变量的显示,甚至格式化.运算等等,这就用到了过滤器. 过滤器的使用方式为:变量名 | 过滤器. 过滤器名写在 ...
- Android Studio 2.3.3 调用asp.net webService实战(拒绝忽悠)
1.路径中不能包含localhost(本来想在本机调试,就是不行,没办法发布到远程服务器) 2.必须采用异步的办法(阻塞主线程的是肯定不行了) 3.以下是全部的源代码(毫不保留) package co ...
- Struts2 配置Action详解
Struts2的核心功能是action,对于开发人员来说,使用Struts2主要就是编写action,action类通常都要实现com.opensymphony.xwork2.Action接口,并实 ...
- MySQL 中随机获取数据
由于需要大概研究了一下MYSQL的随机抽取实现方法. 目前采用的方法: SELECT * FROM tablename ORDER BY RAND() LIMIT 实现原理: 通过ORDER BY R ...
- 增强MyEclipse提示功能
当我们使用MyEclipse编辑代码的时候按住“Ctrl+/”,就能获得代码提示,或者使用“.”的时候就会“点”出属性或方法等,而且提示还会有延迟. 如果我们想无论是按下“.”.“Ctrl+/”甚至是 ...
- 在MyEclipse中用debug调试应用程序
F5:单步测试,作用是跳入,比如说一大步中分为10小步,单击F5一次就会走完一小步,走完这一大步则需要单步10次.F6:与F5一样也是单步测试.只不过与F5不同的是F5追求的是过程,而F6追求的是结果 ...