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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// bal-krolewski-bal.cpp : Ten plik zawiera funkcję „main”. W nim rozpoczyna się i kończy wykonywanie programu.
//

#include <iostream>
#include <queue>
#include <vector>
#include <list>

using namespace std;

constexpr int INF = 100000000;
constexpr int NIL = 0;

bool** t;
int** vid;

std::vector<int> *edges;
std::vector<int> whites;

int hmb;

int sz;







// Mam nadzieje, ze cos takiego jest legalne na potyczkach (nie znalazlem zakazu kopiowania implementacji znanych algorytmow w regulaminie, a na pierwszym etapie OI cos takiego jest chyba dozwolone)


// source: https://iq.opengenus.org/hopcroft-karp-algorithm/


class BGraph
{
    // m and n are number of vertices on left
    // and right sides of Bipartite Graph
    int m, n;

    // adj[u] stores adjacents of left side
    // vertex 'u'. The value of u ranges from 1 to m.
    // 0 is used for dummy vertex
    std::list<int>* adj;

    // pointers for hopcroftKarp()
    int* pair_u, * pair_v, * dist;

public:
    BGraph(int m, int n);     // Constructor
    ~BGraph();
    void addEdge(int u, int v); // To add edge

    // Returns true if there is an augmenting path
    bool bfs();

    // Adds augmenting path if there is one beginning
    // with u
    bool dfs(int u);

    // Returns size of maximum matching
    int hopcroftKarpAlgorithm();
};

// Returns size of maximum matching
int BGraph::hopcroftKarpAlgorithm()
{
    // pair_u[u] stores pair of u in matching on left side of Bipartite Graph.
    // If u doesn't have any pair, then pair_u[u] is NIL
    pair_u = new int[m + 1];

    // pair_v[v] stores pair of v in matching on right side of Biparite Graph.
    // If v doesn't have any pair, then pair_u[v] is NIL
    pair_v = new int[n + 1];

    // dist[u] stores distance of left side vertices
    dist = new int[m + 1];

    // Initialize NIL as pair of all vertices
    for (int u = 0; u <= m; u++)
        pair_u[u] = NIL;
    for (int v = 0; v <= n; v++)
        pair_v[v] = NIL;

    // Initialize result
    int result = 0;

    // Keep updating the result while there is an
    // augmenting path possible.
    while (bfs())
    {
        // Find a free vertex to check for a matching
        for (int u = 1; u <= m; u++)

            // If current vertex is free and there is
            // an augmenting path from current vertex
            // then increment the result
            if (pair_u[u] == NIL && dfs(u))
                result++;
    }
    return result;
}

// Returns true if there is an augmenting path available, else returns false
bool BGraph::bfs()
{
    std::queue<int> q; //an integer queue for bfs

    // First layer of vertices (set distance as 0)
    for (int u = 1; u <= m; u++)
    {
        // If this is a free vertex, add it to queue
        if (pair_u[u] == NIL)
        {
            // u is not matched so distance is 0
            dist[u] = 0;
            q.push(u);
        }

        // Else set distance as infinite so that this vertex is considered next time for availibility
        else
            dist[u] = INF;
    }

    // Initialize distance to NIL as infinite
    dist[NIL] = INF;

    // q is going to contain vertices of left side only.
    while (!q.empty())
    {
        // dequeue a vertex
        int u = q.front();
        q.pop();

        // If this node is not NIL and can provide a shorter path to NIL then
        if (dist[u] < dist[NIL])
        {
            // Get all the adjacent vertices of the dequeued vertex u
            std::list<int>::iterator it;
            for (it = adj[u].begin(); it != adj[u].end(); ++it)
            {
                int v = *it;

                // If pair of v is not considered so far
                // i.e. (v, pair_v[v]) is not yet explored edge.
                if (dist[pair_v[v]] == INF)
                {
                    // Consider the pair and push it to queue
                    dist[pair_v[v]] = dist[u] + 1;
                    q.push(pair_v[v]);
                }
            }
        }
    }

    // If we could come back to NIL using alternating path of distinct
    // vertices then there is an augmenting path available
    return (dist[NIL] != INF);
}

// Returns true if there is an augmenting path beginning with free vertex u
bool BGraph::dfs(int u)
{
    if (u != NIL)
    {
        std::list<int>::iterator it;
        for (it = adj[u].begin(); it != adj[u].end(); ++it)
        {
            // Adjacent vertex of u
            int v = *it;

            // Follow the distances set by BFS search
            if (dist[pair_v[v]] == dist[u] + 1)
            {
                // If dfs for pair of v also returnn true then
                if (dfs(pair_v[v]) == true)
                {   // new matching possible, store the matching
                    pair_v[v] = u;
                    pair_u[u] = v;
                    return true;
                }
            }
        }

        // If there is no augmenting path beginning with u then.
        dist[u] = INF;
        return false;
    }
    return true;
}

// Constructor for initialization
BGraph::BGraph(int m, int n)
{
    this->m = m;
    this->n = n;
    adj = new std::list<int>[m + 1];
}

BGraph::~BGraph()
{
    delete[] adj;
    delete[] pair_u;
    delete[] pair_v;
    delete[] dist;
}

// function to add edge from u to v
void BGraph::addEdge(int u, int v)
{
    adj[u].push_back(v); // Add v to u’s list.
}












int main()
{
    std::ios_base::sync_with_stdio(0);
    std::cin.tie(0);
    std::cout.tie(0);
	int n, m, q, x, y, a, b, hmw,hmb2;
	std::cin >> n >> m >> q;
	t = new bool*[n+7];
	vid = new int* [n + 7];
	edges = new std::vector<int>[(n + 7) * (n + 7)];
	/*matching = new int[(n + 7) * (n + 7)];
	distance = new int[(n + 7) * (n + 7)];
	*/
	sz = (n + 7) * (n + 7);
	for (size_t i = 0; i < n+7; i++)
	{
		t[i] = new bool[n + 7];
		vid[i] = new int[n + 7];
		for (size_t j = 0; j < n+7; j++)
		{
			t[i][j] = 0;
			vid[i][j] = 0;
		}
	}
	while (m--)
	{
		std::cin >> x >> y >> a >> b;
		t[x - 1][y - 1] ^= 1;
		t[a][b] ^= 1;
		t[a][y - 1] ^= 1;
		t[x - 1][b] ^= 1;
	}
	for (size_t i = n; i > 0; i--)
	{
		for (size_t j = n; j > 0; j--)
		{
			t[i][j - 1] ^= t[i][j];
			t[i][j] ^= t[i + 1][j];
		}
	}
    q++;
    while (q--)
    {
        hmb = 0;
        for (size_t i = 1; i <= n; i++)
        {
            for (size_t j = 1; j <= n; j++)
            {
                hmb += t[i][j];
            }
        }
        hmb2 = hmb;
        hmb = 0;
        hmw = 0;

        BGraph bg(hmb2, n * n - hmb2);


        for (size_t i = 1; i <= n; i++)
        {
            whites.clear();
            for (size_t j = 1; j <= n; j++)
            {
                if (!t[i][j])
                {
                    hmw++;
                    vid[i][j] = hmw;
                    whites.push_back(hmw);
                }
            }
            for (size_t j = 1; j <= n; j++)
            {
                if (t[i][j])
                {
                    hmb++;
                    vid[i][j] = hmb;
                    for (auto xd : whites)
                    {
                        bg.addEdge(hmb, xd);
                    }
                }
            }
        }

        for (size_t i = 1; i <= n; i++)
        {
            whites.clear();
            for (size_t j = 1; j <= n; j++)
            {
                if (!t[j][i])
                {
                    whites.push_back(vid[j][i]);
                }
            }
            for (size_t j = 1; j <= n; j++)
            {
                if (t[j][i])
                {
                    hmb = vid[j][i];
                    for (auto xd : whites)
                    {
                        bg.addEdge(hmb, xd);
                    }
                }
            }
        }

        std::cout << bg.hopcroftKarpAlgorithm() << '\n';
        if (q)
        {
            std::cin >> a >> b;
            t[a][b] ^= 1;
        }
        
    }
}

// Uruchomienie programu: Ctrl + F5 lub menu Debugowanie > Uruchom bez debugowania
// Debugowanie programu: F5 lub menu Debugowanie > Rozpocznij debugowanie

// Porady dotyczące rozpoczynania pracy:
//   1. Użyj okna Eksploratora rozwiązań, aby dodać pliki i zarządzać nimi
//   2. Użyj okna programu Team Explorer, aby nawiązać połączenie z kontrolą źródła
//   3. Użyj okna Dane wyjściowe, aby sprawdzić dane wyjściowe kompilacji i inne komunikaty
//   4. Użyj okna Lista błędów, aby zobaczyć błędy
//   5. Wybierz pozycję Projekt > Dodaj nowy element, aby utworzyć nowe pliki kodu, lub wybierz pozycję Projekt > Dodaj istniejący element, aby dodać istniejące pliku kodu do projektu
//   6. Aby w przyszłości ponownie otworzyć ten projekt, przejdź do pozycji Plik > Otwórz > Projekt i wybierz plik sln