-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquest4.cpp
71 lines (66 loc) · 1.14 KB
/
quest4.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
#include <iostream>
#include <vector>
using namespace std;
const int MAX = 120;
vector < int > edges[MAX];
bool visited[MAX];
int Left[MAX], Right[MAX];
bool dfs(int u) {
if(visited[u])
return false;
visited[u] = true;
int len = edges[u].size(), i, v;
for(i = 0; i < len; i++) {
v = edges[u][i];
if(Right[v] == -1) {
Right[v] = u;
Left[u] = v;
return true;
}
}
for(i = 0; i < len; i++) {
v = edges[u][i];
if(dfs(Right[v])) {
Right[v] = u, Left[u] = v;
return true;
}
}
return false;
}
// Our matching algorithm that calls dfs
int match() {
int i, value = 0;
for (i = 0; i < MAX; i++) {
Left[i] = Right[i] = -1;
visited[i] = 0;
}
bool done;
do {
done = true;
for (i = 0; i < MAX; i++)
visited[i] = 0;
for(i = 0; i < MAX; i++) {
if(Left[i] == -1 && dfs(i)) {
done = false;
}
}
} while(!done);
for(i = 0; i < MAX; i++)
value += (Left[i]!=-1);
return value;
}
int main() {
int t, n, i, x, y;
cin >> t;
while(t--) {
cin >> n;
for(i = 0; i < MAX; i++)
edges[i].clear();
for(i = 0; i < n; i++) {
cin >> x >> y;
edges[x].push_back(y);
}
cout << match() << endl;
}
return 0;
}