forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathG.cpp
More file actions
151 lines (140 loc) · 3.43 KB
/
G.cpp
File metadata and controls
151 lines (140 loc) · 3.43 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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--)
#define REP(i,a) for(int i=0,_a=(a); i<_a; i++)
#define EACH(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define DEBUG(x) { cout << #x << " = "; cout << (x) << endl; }
#define PR(a,n) { cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl; }
#define PR0(a,n) { cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl; }
#define sqr(x) ((x) * (x))
#define ll long long
using namespace std;
const int MN = 211;
const int inf = 1e6;
int n;
#define arg __arg
long long c[MN][MN];
long long fx[MN], fy[MN];
int mx[MN], my[MN];
int trace[MN], qu[MN], arg[MN];
long long d[MN];
int front, rear, start, finish;
void init() {
FOR(i,1,n) {
fy[i] = mx[i] = my[i] = 0;
FOR(j,1,n) c[i][j] = inf;
}
}
void addEdge(int i, int j, long long cost) {
c[i][j] = min(c[i][j], cost);
}
inline long long getC(int i, int j) {
return c[i][j] - fx[i] - fy[j];
}
void initBFS() {
front = rear = 1;
qu[1] = start;
memset(trace, 0, sizeof trace);
FOR(j,1,n) {
d[j] = getC(start, j);
arg[j] = start;
}
finish = 0;
}
void findAugPath() {
while (front <= rear) {
int i = qu[front++];
FOR(j,1,n) if (!trace[j]) {
long long w = getC(i, j);
if (!w) {
trace[j] = i;
if (!my[j]) {
finish = j;
return ;
}
qu[++rear] = my[j];
}
if (d[j] > w) {
d[j] = w;
arg[j] = i;
} } } }
void subx_addy() {
long long delta = inf;
FOR(j,1,n)
if (trace[j] == 0 && d[j] < delta) delta = d[j];
// xoay
fx[start] += delta;
FOR(j,1,n)
if (trace[j]) {
int i = my[j];
fy[j] -= delta;
fx[i] += delta;
}
else d[j] -= delta;
FOR(j,1,n)
if (!trace[j] && !d[j]) {
trace[j] = arg[j];
if (!my[j]) { finish = j; return ; }
qu[++rear] = my[j];
}
}
void enlarge() {
do {
int i = trace[finish];
int next = mx[i];
mx[i] = finish;
my[finish] = i;
finish = next;
} while (finish);
}
int mincost() {
FOR(i,1,n) fx[i] = *min_element(c[i]+1, c[i]+n+1);
FOR(i,1,n) {
start = i;
initBFS();
while (finish == 0) {
findAugPath();
if (!finish) subx_addy();
}
enlarge();
}
int res = 0;
FOR(i,1,n) res += c[i][mx[i]];
return res; }
bool has[MN][MN];
int initial[MN];
int main() {
ios :: sync_with_stdio(false);
freopen("keeper.in", "r", stdin);
freopen("keeper.out", "w", stdout);
int left, right;
cin >> left >> right;
n = max(left, right);
init();
FOR(i,1,left) {
int k; cin >> k;
while (k--) {
int u; cin >> u;
has[i][u] = true;
}
}
int existing = 0;
FOR(i,1,left) {
cin >> initial[i];
if (initial[i]) ++existing;
}
FOR(i,1,left) FOR(j,1,right)
if (has[i][j]) {
if (initial[i] == j) addEdge(i, j, -1);
else addEdge(i, j, 0);
}
int res = -mincost(), cnt = 0;
FOR(i,1,n) {
int j = mx[i];
if (c[i][j] == inf) res += inf;
else ++cnt;
}
cout << cnt << ' ' << existing - res << endl;
FOR(i,1,left) if (c[i][mx[i]] < inf) cout << mx[i] << ' '; else cout << "0 ";
cout << endl;
}