CodeForces 906C Party(状态压缩+dfs)

来源:互联网 发布:网络农场 编辑:程序博客网 时间:2024/06/05 10:10
C. Party
time limit per test:1 second
memory limit per test:256 megabytes
input:standard input
output:standard output

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 ofA become 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 andm (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 integersu and v (1 ≤ u, v ≤ n;u ≠ v), which means that people with numbersu 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.

Examples
Input
5 61 21 32 32 53 44 5
Output
22 3 
Input
4 41 21 31 43 4
Output
11 
Note

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. Guest3 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.



        日常打cf……

        大致题意:给你一堆一对对的关系,然后每一个关系对代表两个人认识。然后你每次可以选择一个人i,让i认识的所有人都相互认识,即i把介绍自己所有的朋友给其他人。然后现在问你最少需要选择多少个这样的i,使得所有的人都相互认识。

        看到只有22的数据范围,很容易想到状态压缩,但是如何利用这个状态压缩却没那么好考虑。如果考虑状态压缩dp,显然无法表示连通的状态。于是考虑用搜索,22个点要么最后被选要么没有,做多2^22=400W+,在承受范围内。然后就是看如何处理这个关系了,对于每一个人,我们用一个22位的2进制数字表示他是否与其他的人有关系。对于一个人x,每次选择他的时候,枚举所有与其相连的点i,然后i的状态或上x的状态即可。这样子复杂度就是O(N*2^N),可以通过。具体见代码:

#include<bits/stdc++.h>#define N 23using namespace std;stack<int> ans,res;int dp[N][N],n,m;void dfs(int x){    if (res.size()>=ans.size()) return;    if (x==n)    {        for(int i=0;i<n;i++)            if (dp[x][i]!=(1<<n)-1) return;        ans=res; return;    }    memcpy(dp[x+1],dp[x],sizeof(dp[x]));    dfs(x+1);    for(int i=0;i<n;i++)        if (dp[x][x]&(1<<i)) dp[x+1][i]|=dp[x][x];    res.push(x+1); dfs(x+1); res.pop();}int main(){    scanf("%d%d",&n,&m);    for(int i=1;i<=n;i++)    {        ans.push(1);        dp[0][i-1]=(1<<i-1);    }    for(int i=1;i<=m;i++)    {        int x,y;        scanf("%d%d",&x,&y);        dp[0][--x]|=(1<<--y);        dp[0][y]|=(1<<x);    }    dfs(0);    printf("%d\n",ans.size());    while(!ans.empty())    {        printf("%d\n",ans.top());        ans.pop();    }    return 0;}

阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 卡玛西平片 卡玛男装 卡玛汽车 卡玛丹 冠德卡玛 卡玛瑜伽馆 卡玛兹车队 亚卡玛塔神庙 菲斯克卡玛跑车报价 菲斯克卡玛跑车 卡玛吉他怎么样 亚卡玛塔神庙攻略 冠德卡玛瓷砖 阿塔卡玛沙漠 卡玛的衣服在什么档次 卡玛衣服贵吗 卡玛婚纱摄影 菲斯特卡玛汽车报价 冠德卡玛瓷砖怎么样 萨巴卡玛尔演员图片 卡玛吉他价格表 冠德卡玛陶瓷 菲斯科卡玛报价 卡玛王子蛋糕店 kama kama服装 kama衣服在哪个档次 卡琳相泽 卡琳 卡瓦 诺肯 卡瘦 瘦一斤要燃烧多少卡路里 消耗多少卡路里能瘦一斤 卡瘦生物科技有限公司 卡瘦棒代理多少钱一盒 每天消耗300大卡能瘦吗 卡盘 液压卡盘 三爪卡盘 卡盘三爪