【状压dp】cf906C. Party

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

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的朋友都相互变成朋友。问最少需要选择多少人,使得所有的人都能够相互认识。

扫描二维码关注公众号,回复: 3943920 查看本文章

题目分析

一些初步的想法

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

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

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

那么贪心?

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

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

 1 #include<bits/stdc++.h>
 2 const int INF = 0x3f3f3f3f;
 3 const int maxn = 31;
 4 
 5 int n,m,ans,id;
 6 struct node
 7 {
 8     int val,vis[maxn],pr[maxn],deg[maxn];
 9     std::bitset<maxn> mp[maxn];
10     bool legal()
11     {
12         for (int i=1; i<=n; i++)
13             if (mp[i].count() < n) return 0;
14         return 1;
15     }
16     void print()
17     {
18         for (int i=1; i<=val; i++)
19             printf("%d ",pr[i]);
20         puts("");
21     }
22 }now,tmp,chg;
23 std::queue<node> q;
24 
25 int read()
26 {
27     char ch = getchar();
28     int num = 0;
29     bool fl = 0;
30     for (; !isdigit(ch); ch=getchar())
31         if (ch=='-') fl = 1;
32     for (; isdigit(ch); ch=getchar())
33         num = (num<<1)+(num<<3)+ch-48;
34     if (fl) num = -num;
35     return num;
36 }
37 int main()
38 {
39     freopen("party.in","r",stdin);
40     freopen("party.out","w",stdout);
41     n = read(), m = read(), now.val = 0, ans = INF;
42     for (int i=1; i<=n; i++) now.mp[i][i] = 1;
43     for (int i=1; i<=m; i++)
44     {
45         int u = read(), v = read();
46         now.mp[u][v] = 1, now.mp[v][u] = 1;
47     }
48     q.push(now);
49     while (q.size())
50     {
51         tmp = q.front(), q.pop();
52         if (tmp.legal()){
53             ans = tmp.val;
54             printf("%d\n",ans);
55             tmp.print();
56             break;
57         }
58         bool fnd = 0;
59         while (!fnd){
60         id = 0;
61         for (int i=1; i<=n; i++)
62             if (!tmp.vis[i]){
63                 tmp.deg[i] = tmp.mp[i].count();
64                 if (tmp.deg[i] > tmp.deg[id]) id = i;
65             }
66         for (int i=1; i<=n; i++)
67             if (tmp.deg[i]==tmp.deg[id]){
68                 bool upd = 0;
69                 chg = tmp, chg.val++;
70                 chg.vis[i] = 1, chg.pr[chg.val] = i;
71                 for (int j=1; j<=n; j++)
72                     for (int k=1; k<=n; k++)
73                         if (j!=k&&chg.mp[i][j]&&chg.mp[i][k]&&!chg.mp[j][k])
74                             chg.mp[k][j] = chg.mp[j][k] = 1, upd = 1;
75                 if (upd) fnd = 1, q.push(chg);
76                 else tmp.vis[i] = 1;
77             }
78         }
79     }
80     return 0;
81 }

状压dp

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

首先明确两个结论:

猜你喜欢

转载自www.cnblogs.com/antiquality/p/9910103.html