需要稍加分析结论;还有一些小细节

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.

Input

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.

Output

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.


题目大意

给定一些关系(u,v)代表两个人是朋友。每次可以选择一个人i,使i的朋友都相互变成朋友。问最少需要选择多少人,使得所有的人都能够相互认识。

题目分析

一些初步的想法

一开始比较自然地会考虑完全子图的情况。

那么有一个想法是:能不能把这整个完全子图给看成一个人?既然他们互相认识,那么这样它连出的边可以等效吗?

但这个想法的问题在于,这个完全子图的关系“不可拓展”,也就是说在他们的朋友介绍新朋友时,新加进来的人并不能和其他人认识。

那么贪心?

考虑到新加进人的过程是不可逆的。那么能不能够贪心地每次选尽可能多的点呢?

这样似乎还没有找到反例。但是这个做法的潜在危险在于其复杂度基于答案大小,有可能会因为答案数过大而TLE/MLE.

 #include<bits/stdc++.h>
const int INF = 0x3f3f3f3f;
const int maxn = ; int n,m,ans,id;
struct node
{
int val,vis[maxn],pr[maxn],deg[maxn];
std::bitset<maxn> mp[maxn];
bool legal()
{
for (int i=; i<=n; i++)
if (mp[i].count() < n) return ;
return ;
}
void print()
{
for (int i=; i<=val; i++)
printf("%d ",pr[i]);
puts("");
}
}now,tmp,chg;
std::queue<node> q; int read()
{
char ch = getchar();
int num = ;
bool fl = ;
for (; !isdigit(ch); ch=getchar())
if (ch=='-') fl = ;
for (; isdigit(ch); ch=getchar())
num = (num<<)+(num<<)+ch-;
if (fl) num = -num;
return num;
}
int main()
{
freopen("party.in","r",stdin);
freopen("party.out","w",stdout);
n = read(), m = read(), now.val = , ans = INF;
for (int i=; i<=n; i++) now.mp[i][i] = ;
for (int i=; i<=m; i++)
{
int u = read(), v = read();
now.mp[u][v] = , now.mp[v][u] = ;
}
q.push(now);
while (q.size())
{
tmp = q.front(), q.pop();
if (tmp.legal()){
ans = tmp.val;
printf("%d\n",ans);
tmp.print();
break;
}
bool fnd = ;
while (!fnd){
id = ;
for (int i=; i<=n; i++)
if (!tmp.vis[i]){
tmp.deg[i] = tmp.mp[i].count();
if (tmp.deg[i] > tmp.deg[id]) id = i;
}
for (int i=; i<=n; i++)
if (tmp.deg[i]==tmp.deg[id]){
bool upd = ;
chg = tmp, chg.val++;
chg.vis[i] = , chg.pr[chg.val] = i;
for (int j=; j<=n; j++)
for (int k=; k<=n; k++)
if (j!=k&&chg.mp[i][j]&&chg.mp[i][k]&&!chg.mp[j][k])
chg.mp[k][j] = chg.mp[j][k] = , upd = ;
if (upd) fnd = , q.push(chg);
else tmp.vis[i] = ;
}
}
}
return ;
}

状压dp

注意到特殊的数据范围,考虑状压。

首先明确两个结论:

  1. 答案与合并顺序无关
  2. 对图$G=\{V,E\}$中一个完全子图$G'=\{V',E'\}$中的点$x\in V'$进行操作后,其所在的完全子图$G''=\{V'+V_x,E'+E_x\},E_x=\{x,V_x\}\in E$。换句话说就是操作集合内的点能使集合变大。

第一条是因为操作点$x$之后$\forall (x,y_i)$,$y_i$之间都存在边,那么下一步选取$y_i$时会把其他的$y_j$也都连在一起;第二条比较容易理解。

那么用$f[i]$表示$i$的二进制状态下,这些人相互认识的最小代价。

于是转移就是经典的状压转移;顺便再记录一下转移的位置就好了。

需要注意的细节是原图是否已经连通,那么这个就是预处理的时候再判断一下。

 #include<bits/stdc++.h>
const int maxn = ;
const int maxs = ;
const int INF = 0x3f3f3f3f; bool fnd;
int n,m,mx;
int s[maxn],f[maxs],fa[maxs],pr[maxs]; int read()
{
char ch = getchar();
int num = ;
bool fl = ;
for (; !isdigit(ch); ch=getchar())
if (ch=='-') fl = ;
for (; isdigit(ch); ch=getchar())
num = (num<<)+(num<<)+ch-;
if (fl) num = -num;
return num;
}
void fndScheme(int st)
{
if (fa[st]) fndScheme(fa[st]);
printf("%d ",pr[st]);
}
int main()
{
// freopen("party.in","r",stdin);
// freopen("party.out","w",stdout);
memset(f, 0x3f3f3f3f, sizeof f);
n = read(), m = read(), mx = (<<n)-;
for (int i=; i<n; i++) s[i+] = <<i;
for (int i=; i<=m; i++)
{
int x = read()-, y = read()-;
s[x+] |= <<y, s[y+] |= <<x;
}
fnd = ;
for (int i=; i<=n; i++)
{
f[s[i]] = , pr[s[i]] = i;
if (s[i]!=mx) fnd = ;
}
if (fnd){
puts("");
return ;
}
for (int st=; st<=mx; st++)
if (f[st]!=INF){
for (int i=; i<=n; i++)
if (((st>>(i-))&)&&(f[st]+ < f[st|s[i]])){
f[st|s[i]] = f[st]+;
fa[st|s[i]] = st;
pr[st|s[i]] = i;
}
}
printf("%d\n",f[mx]);
fndScheme(mx);
return ;
}

END

最新文章

  1. thinkPHP--CURD操作
  2. CentOS 7 安装php开发环境
  3. AngularJS 源码分析1
  4. 用了星型转换的sql跑了5小时---&gt;5mins的过程
  5. LVS + KEEPAlived 配置 DIR模式
  6. 12306外包给阿里巴巴/IBM到底是否可行?
  7. 什么是BI及哪些行业需要用到BI?
  8. 重温CSS:Border属性
  9. php遍历mysql资源
  10. 在redhat6.4下安装 Oracle&#174; Database 11g Release 2
  11. 交换机access和trunk的一些小结(转)
  12. Oracle 12C 新特性之move (非分区表)table online
  13. 学习总结---BGP协议
  14. 织梦dedecms如何去除版权中的Power by DedeCms
  15. Mongo字符串类型的数值查询---$Where查询介绍
  16. Codechef August Challenge 2018 : Chef at the River
  17. HTML 5 &lt;input&gt; list 属性
  18. BOM浏览器对象模型;
  19. oracle 、sql server 、mysql 复制表数据
  20. Node.js最新Web技术栈(2015年5月)

热门文章

  1. PJzhang:子域名发掘工具Sublist3r
  2. 输入apt-get update时出现Could not open lock file /var/lib/apt/lists/lock - open
  3. 如何使用sass
  4. Vuex+axios
  5. Codeforces 1114F(欧拉函数、线段树)
  6. Middleware-请求管道的构成
  7. 朱晔的互联网架构实践心得S2E7:漫谈平台架构的工作(基础架构、基础服务、基础平台、基础中间件等等)
  8. linux basename命令的使用
  9. Java的API及Object类、String类、字符串缓冲区
  10. 在jquery事件中修改Angular的model