-
Notifications
You must be signed in to change notification settings - Fork 29
/
2289.cc
executable file
·235 lines (204 loc) · 4.56 KB
/
2289.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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
//Result:wizmann 2289 Accepted 3692K 250MS G++ 1256B
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#define pb push_back
using namespace std;
int n,m;
vector<int> g[1010];
char visit[1010];
int num[1010];
int match[1010][505];
bool hungary(int pos,int val)
{
for(int i=0;i<(int)g[pos].size();i++)
{
int t=g[pos][i];
if(!visit[t])
{
visit[t]=1;
if(num[t]<val)
{
match[t][++num[t]]=pos;
return true;
}
for(int j=1;j<=val;j++)
{
if(hungary(match[t][j],val))
{
match[t][j]=pos;
return true;
}
}
}
}
return false;
}
int main()
{
freopen("input.txt","r",stdin);
char str[30];
int a;
while(1)
{
scanf("%d%d",&n,&m);
if(m==0) break;
for(int i=0;i<n;i++) g[i].clear();
for(int i=0;i<n;i++)
{
scanf("%s",str);
char c=getchar();
while(c!='\n')
{
scanf("%d",&a);
g[i].pb(a);
c=getchar();
}
}
int l=0,r=n;
while(l<=r)
{
int mid=(l+r)>>1;
memset(match,-1,sizeof(match));
memset(num,0,sizeof(num));
bool flag=true;
for(int i=0;i<n;i++)
{
memset(visit,0,sizeof(visit));
flag=hungary(i,mid);
if(!flag) break;
}
if(flag) r=mid-1;
else l=mid+1;
}
printf("%d\n",l);
}
return 0;
}
////
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <map>
#include <set>
#include <deque>
#include <string>
#include <sstream>
#include <cassert>
using namespace std;
typedef long long llint;
const int SIZE = 1111;
vector<string> split_string(const string& line, char delim) {
std::istringstream stream(line);
string token;
vector<string> res;
while (std::getline(stream, token, delim)) {
res.push_back(token);
}
return res;
}
class MultiHungary {
public:
MultiHungary(int left_, int right_) : left(left_), right(right_) {
g = vector<vector<int> >(left);
for (int i = 0; i < left; i++) {
g[i] = vector<int>(right, 0);
}
caps = vector<int>(right, 0);
}
void setCapacity(int idx, int cap) {
caps[idx] = cap;
}
void addEdge(int l, int r) {
g[l][r] = 1;
}
// `all_match` indicates if all nodes on the left
// are matched with one of the right nodes
int match(bool& match_all_left, bool must_match_all_left) {
int res = 0;
match_all_left = true;
pre = vector<vector<int> >(right);
for (int i = 0; i < left; i++) {
visited = vector<bool>(right, false);
if (find_path(i)) {
res++;
} else {
match_all_left = false;
}
if (must_match_all_left && !match_all_left) {
return -1;
}
}
return res;
}
private:
bool find_path(int cur) {
for (int i = 0; i < right; i++) {
if (g[cur][i] && !visited[i]) {
visited[i] = true;
if (pre[i].size() < caps[i]) {
pre[i].push_back(cur);
return true;
}
for (int j = 0; j < pre[i].size(); j++) {
int& p = pre[i][j];
if (find_path(p)) {
p = cur;
return true;
}
}
}
}
return false;
}
private:
int left, right;
vector<vector<int> > g;
vector<int> caps;
vector<vector<int> > pre;
vector<bool> visited;
};
char buffer[2048];
int main() {
int n, m;
while (scanf("%d%d\n", &n, &m) != EOF) {
if (n + m == 0) {
break;
}
MultiHungary mh(n, m);
for (int i = 0; i < n; i++) {
fgets(buffer, 2048, stdin);
string line(buffer);
// puts(line.c_str());
vector<string> tokens = split_string(line, ' ');
int tn = tokens.size();
for (int j = 1; j < tn; j++) {
int g = atoi(tokens[j].c_str());
mh.addEdge(i, g);
}
}
// binary search
int l = 1;
int r = n;
while (l <= r) {
int mid = (l + r) / 2;
for (int i = 0; i < m; i++) {
mh.setCapacity(i, mid);
}
bool match_all_left = true;
mh.match(match_all_left, true);
if (match_all_left) {
r = mid - 1;
} else {
l = mid + 1;
}
}
printf("%d\n", l);
}
return 0;
}