-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzl_pairs.cpp
80 lines (63 loc) · 1.15 KB
/
zl_pairs.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
#include <cstdio>
#include <list>
using namespace std;
int const size = 252;
int used[size];
int pairArr[size];
list<int> edges[size];
int n;
int m;
int cc = 1;
bool dfs(int v) {
used[v] = cc;
for (list<int>::iterator it = edges[v].begin(); it != edges[v].end(); it++) {
int x = *it;
if (
(pairArr[x] == -1) ||
( (used[pairArr[x]] != cc) && dfs(pairArr[x]) )
)
{
pairArr[x] = v;
return true;
}
}
return false;
}
void pairsCalculate() {
for (int i = 1; i <= n; i++) {
cc++;
dfs(i);
}
}
int main() {
for (int i = 0; i < size; i++) {
pairArr[i] = -1;
}
FILE* in = fopen("pairs.in", "r");
fscanf(in, "%d", &n);
fscanf(in, "%d", &m);
int curVertexA = 1;
while (!feof(in)) {
int tmp;
fscanf(in, "%d", &tmp);
if (tmp == 0) {
curVertexA++;
continue;
}
edges[curVertexA].push_back(tmp);
}
fclose(in);
pairsCalculate();
int count = 0;
for (int i = 1; i <= m; i++) {
count = pairArr[i] > 0 ? count + 1 : count;
}
FILE* out = fopen("pairs.out", "w");
fprintf(out, "%d\n", count);
for (int i = 1; i <= m; i++) {
if (pairArr[i] > 0) {
fprintf(out, "%d %d\n", pairArr[i], i);
}
}
fclose(out);
}