-
Notifications
You must be signed in to change notification settings - Fork 0
/
phatqua.cpp
108 lines (86 loc) · 2.12 KB
/
phatqua.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
#define oo 1000000007
#define maxN 207
#include <bits/stdc++.h>
using namespace std;
int n, m, s, t, ans;
int a[maxN][maxN], f[maxN][maxN], g[maxN][maxN], trace[maxN];
void BuildG(){
for (int u = 0; u <= n + m + 1; ++u){
for (int v = 0; v <= n + m + 1; ++v){
g[u][v] = 0;
}
}
for (int u = 0; u <= n + m + 1; ++u){
for (int v = 0; v <= n + m + 1; ++v){
if (a[u][v] > f[u][v]) g[u][v] = a[u][v] - f[u][v];
if (f[u][v] > 0) g[v][u] = f[u][v];
}
}
}
bool Find_Path(){
BuildG();
queue <int> q;
for (int i = 1; i <= n + m + 1; ++i){
trace[i] = -1;
}
q.push(s);
while(!q.empty()){
int u = q.front();
q.pop();
for (int v = 0; v <= n + m + 1; ++v){
if (trace[v] == -1 && g[u][v] > 0){
trace[v] = u;
q.push(v);
if (v == t) return true;
}
}
}
return false;
}
void Inc_Flow(){
int v = t;
int delta = oo;
while (v != s){
int u = trace[v];
delta = min(delta, g[u][v]);
v = u;
}
v = t;
while (v != s){
int u = trace[v];
if (f[u][v] < a[u][v]) f[u][v] += delta;
else f[v][u] -= delta;
v = u;
}
}
void Ford_Fulkerson(){
while(Find_Path()) Inc_Flow();
}
int main(){
freopen("phatqua.inp", "r", stdin);
freopen("phatqua.out", "w", stdout);
scanf("%d%d", &n, &m);
t = n + m + 1;
for (int i = 1; i <= n; ++i){
int k;
scanf("%d", &k);
for (int j = 0; j < k; ++j){
int temp;
scanf("%d", &temp);
a[i][temp + n] = 1;
a[temp + n][t] = 1;
}
a[s][i] = 1;
}
Ford_Fulkerson();
for (int i = 0; i <= n + m + 1; ++i){
ans += f[s][i];
}
printf("%d\n", ans);
for (int i = 1; i <= n; ++i){
for (int j = 1; j <= m; ++j){
if (f[i][j + n] == 1) printf("%d %d\n", i, j);
}
}
return 0;
}