-
Notifications
You must be signed in to change notification settings - Fork 0
/
OJ9.cc
80 lines (74 loc) · 1.73 KB
/
OJ9.cc
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
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
unsigned short N, M;
unsigned short need;
unsigned short haveNum[40000];
unsigned short have[40000];
unsigned temp;
unsigned short temp2;
unsigned char dp[65536];
unsigned short output[65536][2];
int main()
{
ios::sync_with_stdio(false);
cin >> N >> M;
for (unsigned short i = 0; i < M; i++)
cin >> haveNum[i];
need = 0;
for (unsigned short i = 0; i < N; i++)
{
cin >> temp;
need += 1 << temp;
}
for (unsigned short i = 0; i < M; i++)
{
have[i] = 0;
for (unsigned short j = 0; j < haveNum[i]; j++)
{
cin >> temp;
have[i] += 1 << temp;
}
}
for (unsigned i = 0; i < 65536; i++)
{
if ((i | have[0]) == have[0])
dp[i] = 1;
else
dp[i] = 200;
}
for (unsigned short i = 1; i < M; i++)
{
for (unsigned j = 0; j < 65536; j++)
{
temp = j & have[i];
if (temp == j)
{
dp[j] = 1;
output[j][0] = i;
}
else if (temp == 0)
continue;
else
{
temp2 = j ^ temp;
if (dp[j] < dp[temp2] + 1)
continue;
else
{
dp[j] = dp[temp2] + 1;
memcpy(output[j], output[temp2], 2);
output[j][dp[j] - 1] = i;
}
}
}
if (dp[need] <= 2)
break;
}
if (dp[need] > 2)
cout << -1;
else
for (unsigned short i = 0; i < dp[need]; i++)
cout << output[need][i] << ' ';
}