-
Notifications
You must be signed in to change notification settings - Fork 174
/
Copy pathSchool Dance.cpp
49 lines (40 loc) · 883 Bytes
/
School Dance.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
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
const int maxN = 505;
bool used[maxN];
int N, M, K, cnt, mt[maxN];
vector<int> G[maxN];
vector<pii> pairs;
bool kuhns(int u){
if(used[u]) return false;
used[u] = true;
for(int v : G[u]){
if(!mt[v] || kuhns(mt[v])){
mt[v] = u;
return true;
}
}
return false;
}
int main(){
scanf("%d %d %d", &N, &M, &K);
for(int i = 0, a, b; i < K; i++){
scanf("%d %d", &a, &b);
G[b].push_back(a);
}
for(int i = 1; i <= M; i++){
fill(used+1, used+N+1, false);
kuhns(i);
}
cnt = 0;
for(int i = 1; i <= N; i++){
if(mt[i]){
pairs.push_back({i, mt[i]});
cnt++;
}
}
printf("%d\n", cnt);
for(pii P : pairs)
printf("%d %d\n", P.first, P.second);
}