-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path6450.cpp
110 lines (79 loc) · 1.44 KB
/
6450.cpp
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>
#include <cstdint>
#include <queue>
#include <unordered_set>
using namespace std;
const uint32_t MAX_N = 21;
void solve();
uint32_t hammingWeight(uint32_t value);
int main()
{
uint32_t numCases;
cin >> numCases;
while(numCases--)
{
solve();
}
return 0;
}
void solve()
{
uint32_t fullCoverage = 0;
uint32_t socialNetwork[MAX_N];
uint32_t n;
cin >> n;
uint32_t d;
uint32_t connection;
for(uint32_t i = 1; i <= n; i++)
{
fullCoverage |= (1 << i);
cin >> d;
socialNetwork[i] = (1 << i);
for(uint32_t j = 0; j < d; j++)
{
cin >> connection;
socialNetwork[i] |= (1 << connection);
}
}
queue<uint32_t> bfs;
bfs.push(0);
bfs.push(0);
unordered_set<uint32_t> visited;
while(!bfs.empty())
{
uint32_t currentCombination = bfs.front();
bfs.pop();
uint32_t currentCoverage = bfs.front();
bfs.pop();
for(uint32_t i = 1; i <= n; i++)
{
uint32_t newCombination = currentCombination | (1 << i);
if(visited.count(newCombination))
{
continue;
}
uint32_t newCoverage = currentCoverage | socialNetwork[i];
if(newCoverage == fullCoverage)
{
cout << hammingWeight(newCombination) << endl;
return;
}
visited.insert(newCombination);
bfs.push(newCombination);
bfs.push(newCoverage);
}
}
}
uint32_t hammingWeight(uint32_t value)
{
uint32_t output = 0;
while(value > 0)
{
if((value & 1) == 1)
{
output++;
}
value >>= 1;
}
return output;
}