-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathbuddy_selection.cpp
More file actions
67 lines (53 loc) · 1.6 KB
/
buddy_selection.cpp
File metadata and controls
67 lines (53 loc) · 1.6 KB
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
// STL includes
#include <iostream>
#include <vector>
#include <set>
#include <string>
// BGL includes
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/max_cardinality_matching.hpp>
using namespace std;
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS> graph;
typedef boost::graph_traits<graph>::vertex_descriptor vertex_desc;
bool perfect_matching(const graph &G) {
int n = boost::num_vertices(G);
vector<vertex_desc> mate_map(n); // exterior property map
boost::edmonds_maximum_cardinality_matching(G,
boost::make_iterator_property_map(mate_map.begin(), boost::get(boost::vertex_index, G)));
int matching_size = boost::matching_size(G,
boost::make_iterator_property_map(mate_map.begin(), boost::get(boost::vertex_index, G)));
return 2 * matching_size == n;
}
void solve() {
int n; cin >> n;
int c; cin >> c;
unsigned int f; cin >> f;
graph G(n);
string x;
vector<set<string>> ch(n, set<string>());
for (int i = 0; i < n; ++i) {
for (int j = 0; j < c; ++j) {
cin >> x;
ch[i].insert(x);
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < i; ++j) {
vector<string> intersection;
set_intersection(ch[i].begin(), ch[i].end(), ch[j].begin(), ch[j].end(), back_inserter(intersection));
if (intersection.size() > f) {
boost::add_edge(i, j, G);
}
}
}
cout << (perfect_matching(G) ? "not optimal" : "optimal") << endl;
}
int main()
{
ios_base::sync_with_stdio(false);
int t; cin >> t;
for (int i = 0; i < t; ++i) {
solve();
}
return 0;
}