-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSource.cpp
163 lines (152 loc) · 2.51 KB
/
Source.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
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
#pragma warning(disable: 4996)
#pragma once
#include <iostream>
#define NIL 0
#define INF INT_MAX
#define QUEUE_SIZE 5000
#include <cstdio>
class Queue
{
int *queue;
int size;
int first;
int last;
public:
Queue()
{
queue = new int[QUEUE_SIZE];
first = 0;
size = 0;
last = 0;
}
void pushBack(int k)
{
queue[last] = k;
last = (last + 1) % QUEUE_SIZE;
size++;
}
void popFront()
{
if(size!=0)
{
first = (first + 1) % QUEUE_SIZE;
size--;
}
}
bool empty()
{
return size == 0;
}
int getFirst()
{
return queue[first];
}
void clear()
{
last = 0;
first = 0;
size = 0;
}
~Queue()
{
delete[] queue;
}
};
inline bool dfs(int l, int** adj, int* dist, int* pairU, int* pairV);
int main()
{
int left, right, i, q, value, j;
scanf("%d %d", &left, &right);
int **adj = new int*[left + 1];
for (i = 1; i <= left; i++)
{
scanf("%d", &q);
adj[i] = new int[q + 2];
adj[i][0] = q;
for (j = 1; j <= q; j++)
{
scanf("%d", &value);
adj[i][j] = value;
}adj[i][j] = -1;
}
int *pairL = new int[left + 1];
int *pairR = new int[right + 1];
int *pathSize = new int[left + 1];
for (i = 0; i <= left; i++)
pairL[i] = NIL;
for (i = 0; i <= right; i++)
pairR[i] = NIL;
register unsigned int matchings = 0;
Queue queue;
while (true)
{
queue.clear();
for (i = 1; i <= left; i++)
{
if (pairL[i] == NIL)
{
pathSize[i] = 0;
queue.pushBack(i);
}
else
pathSize[i] = INF;
}
pathSize[NIL] = INF;
while (queue.empty() == false)
{
q = queue.getFirst();
queue.popFront();
if (pathSize[q] < pathSize[NIL])
{
for (i = 1; adj[q][i] != -1; i++)
{
if (pathSize[pairR[adj[q][i]]] == INF)
{
pathSize[pairR[adj[q][i]]] = pathSize[q] + 1;
queue.pushBack(pairR[adj[q][i]]);
}
}
}
}
if (pathSize[NIL] == INF)
break;
for (i = 1; i <= left; i++)
{
if (pairL[i] == NIL && dfs(i, adj, pathSize, pairL, pairR))
matchings++;
}
}
std::cout << matchings;
//getchar();
//getchar();
delete [] pairL;
delete [] pairR;
delete[] pathSize;
for (i = 1; i <= left; i++)
{
delete[]adj[i];
}
delete[]adj;
return 0;
}
inline bool dfs(int l, int** adj, int* dist, int* pairU, int* pairV)
{
if (l != NIL)
{
for (int q = 1; adj[l][q] != -1; q++)
{
if (dist[pairV[adj[l][q]]] == dist[l] + 1)
{
if (dfs(pairV[adj[l][q]], adj, dist, pairU, pairV) == true)
{
pairV[adj[l][q]] = l;
pairU[l] = adj[l][q];
return true;
}
}
}
dist[l] = INF;
return false;
}
return true;
}