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 当 它存在 ...
随机推荐
- Linux关闭iptables以及selinux
1.查看iptables状态 /etc/init.d/iptables status # 方法1 service iptables status # 方法2 2.重启后永久生效 chkconfig i ...
- 一个进程间同步和通讯的 C# 框架
转自原文 一个进程间同步和通讯的 C# 框架 threadmsg_demo.zip ~ 41KB 下载 threadmsg_src.zip ~ 65KB 下载 0.背景简介 微软在 .NE ...
- 留用 未验证 js适配根字体大小
方法一:<script> (function (doc, win) { var docEl = doc.documentElem ...
- Hibernate多对多关联映射的HQL中的in条件查询问题
群里有朋友求解一个问题,高分求一条HQL多对多查询语句 . 问题描述见 http://topic.csdn.net/u/20090621/16/4eac6fe0-bf3e-422e-a697-f758 ...
- ASP.NET Web API(C#)学习01
Web Api 记得去年公司有个分享会分享了这个,最近留意招聘信息的时候,发现有个招聘信息的要求是会用WebApi,然后花了半个小时不到,根据下面这篇文章了解了一下,觉得这个东西也不难啊. 突然发现在 ...
- 在struts2.3.4.1中使用注解、反射、拦截器实现基于方法的权限控制
权限控制是每一个系统都应该有的一个功能,有些只需要简单控制一下就可以了,然而有些却需要进行更加深入和细致的权限控制,尤其是对于一些MIS类系统,基于方法的权限控制就更加重要了. 用反射和自定义注解来实 ...
- IDEA快捷键【收藏】
Ctrl+Alt+L 格式化代码Ctrl+Shift+J 两行合成一行,删去不必要的空格匹配代码格式其他快捷键:[常规]Ctrl+Shift + Enter,语句完成“!”,否定完成,输入表达式时按 ...
- 【UVA11419 训练指南】我是SAM 【二分图最小覆盖,最小割】
题意 给出一个R*C大小的网格,网格上面放了一些目标.可以在网格外发射子弹,子弹会沿着垂直或者水平方向飞行,并且打掉飞行路径上的所有目标.你的任务是计算最少需要多少子弹,各从哪些位置发射,才能把所有目 ...
- TP3.1 一对多模型关联
TP3.1.3 的一对多的模型关联 老需求 --- 一个用户多个文章,查看这些文章 HasMany 首先定义Model 模型名字叫UserMode.class.php class UserMode ...
- Opencv 计算图片旋转角度
vector<vector<Point>> vec_point;vector<Vec4i> hireachy;findContours(img_canny1, ve ...