CF1105E - Helping Hiasat

可以发现,在两个 1 操作之间的人中最多满足一个。我们可以两两连边,这样问题就转换为最大独立集问题,按照套路此时我们作补图,将最大独立集转换为求补图最大团

最大团的常用算法是 Bron Kerbosch ,我们甚至不需要很强的优化剪枝就可以过掉这题。

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// =================================
// author: memset0
// date: 2019.01.26 13:36:04
// website: https://memset0.cn/
// =================================
#include <bits/stdc++.h>
#define ll long long
namespace ringo {
template <class T> inline void read(T &x) {
x = 0; register char c = getchar(); register bool f = 0;
while (!isdigit(c)) f ^= c == '-', c = getchar();
while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
if (f) x = -x;
}
template <class T> inline void print(T x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) print(x / 10);
putchar('0' + x % 10);
}
template <class T> inline void print(T x, char c) { print(x), putchar(c); }

const int N = 45;
int n, m, ans;
bool G[N][N];
char s[N];
std::set <int> set;
std::map <std::string, int> map;

int id(std::string s) {
static int cnt = 0;
if (map.find(s) == map.end()) return map[s] = ++cnt;
return map[s];
}

void dfs(std::vector <int> all, std::vector <int> some, std::vector <int> none) {
if (!some.size()) {
if (!none.size()) ans = std::max(ans, (int)all.size());
return;
}
int u = *some.begin();
for (auto v : some) if (!G[u][v]) {
std::vector <int> all_t = all, some_t, none_t;
all_t.push_back(v);
for (auto t : some) if (G[v][t]) some_t.push_back(t);
for (auto t : none) if (G[v][t]) none_t.push_back(t);
dfs(all_t, some_t, none_t);
none.push_back(v);
}
}

void main() {
read(m), read(n);
for (int i = 1, op; i <= m; i++)
if (read(op), op == 1) {
for (auto i : set)
for (auto j : set)
G[i][j] = 1;
set.clear();
} else {
scanf("%s", s);
set.insert(id((std::string)s));
}
for (auto i : set)
for (auto j : set)
G[i][j] = 1;
set.clear();
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
G[i][j] ^= 1;
for (int i = 1; i <= n; i++) G[i][i] = 0;
std::vector <int> all, some, none;
for (int i = 1; i <= n; i++) some.push_back(i);
dfs(all, some, none), print(ans, '\n');
}

} signed main() { return ringo::main(), 0; }

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×