forked from sitz/UVa-Online-Judge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path10779.cpp
180 lines (173 loc) · 2.92 KB
/
10779.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <bits/stdc++.h>
using namespace std;
#define REP(i, b, n) for (int i = b; i < n; i++)
#define rep(i, n) REP(i, 0, n)
const int N = 1500;
const int inf = (1 << 21);
int cap[N][N];
bool visited[N];
int parent[N];
int flow[N][N];
int layer[N];
void make_layer(int n, int s, int t)
{
queue<int> Q;
Q.push(s);
layer[s] = 0;
while (!Q.empty())
{
int now = Q.front();
Q.pop();
rep(i, n)
{
if (cap[now][i] - flow[now][i] > 0 && layer[i] == -1)
{
layer[i] = layer[now] + 1;
Q.push(i);
}
}
}
}
int augment(int now, int t, int n, int f)
{
if (now == t || f == 0)
{
return f;
}
if (visited[now])
{
return 0;
}
visited[now] = true;
rep(i, n)
{
if (layer[now] < layer[i])
{
int tmp = augment(i, t, n, min(f, cap[now][i] - flow[now][i]));
if (tmp > 0)
{
flow[now][i] += tmp;
flow[i][now] = -flow[now][i];
visited[now] = false;
return tmp;
}
}
}
return 0;
}
int dinic(int n, int s, int t)
{
int ansflow = 0;
bool flag = true;
rep(i, n) rep(j, n) flow[i][j] = 0;
while (flag)
{
fill(layer, layer + n, -1);
fill(visited, visited + n, false);
flag = false;
//make layer
make_layer(n, s, t);
if (layer[t] == -1)
{
break;//if no such path to go to sink
}
//find blocking flow
for (int f = 1; f; flag = true)
{
f = augment(s, t, n, inf);
if (f == 0)
{
break;
}
ansflow += f;
}
}
return ansflow;
}
#define FIRST(i, k, m) ((i) * (m)*4 + (k))
#define SECOND(i, k, m) ((i) * (m)*4 + (m) + (k))
#define THIRD(i, k, m) ((i) * (m)*4 + 2 * (m) + (k))
#define FOURTH(i, k, m) ((i) * (m)*4 + 3 * (m) + (k))
main()
{
int te;
cin >> te;
int card[50];
rep(tc, te)
{
int n, m;
cin >> n >> m;
int node = n * m * 4 + 2, src = node - 2, dst = node - 1;
rep(i, node) rep(j, node) cap[i][j] = 0;
rep(i, n)
{
rep(j, m) card[j] = 0;
int num, t;
cin >> num;
rep(j, num)
{
cin >> t;
card[t - 1]++;
}
rep(j, m)
{
if (i == 0)
{
cap[src][FIRST(0, j, m)] = inf;
cap[FIRST(i, j, m)][SECOND(i, j, m)] = card[j];
cap[SECOND(i, j, m)][THIRD(i, j, m)] = inf;
cap[THIRD(i, j, m)][FOURTH(i, j, m)] = 1;
cap[FOURTH(i, j, m)][dst] = 1;
}
else
{
if (card[j] > 0)
{
cap[THIRD(i, j, m)][FOURTH(i, j, m)] = card[j] - 1;
}
if (card[j] == 0)
{
cap[FIRST(i, j, m)][SECOND(i, j, m)] = 1;
}
cap[FOURTH(i, j, m)][THIRD(0, j, m)] = 1;
}
}
}
REP(j, 1, n)
{
rep(k, m)
{
cap[SECOND(0, k, m)][FIRST(j, k, m)] = inf;
}
}
REP(i, 1, n)
{
REP(j, 1, n)
{
if (i == j)
{
continue;
}
//trade
rep(k, m)
{
cap[FOURTH(i, k, m)][FIRST(j, k, m)] = inf;
}
}
//self
rep(j, m)
{
rep(k, m)
{
if (j != k)
{
cap[SECOND(i, j, m)][THIRD(i, k, m)] = inf;
}
}
}
}
int ans = dinic(node, src, dst);
cout << "Case #" << tc + 1 << ": " << ans << endl;
}
return false;
}