Main topic:
There are N1 bulls and N2 cows, which give the m pair relationship between male and female, and the maximum number of matches is obtained. Data range: 1 <= n1, N2 <= 50000, M <= 150000
Algorithm discussion:
First reaction km directly on, the second reaction, KM is O (n^2 * M), will t become a dog.
The second reaction is to see how everyone is doing it. Later found a name called Hopcroft-carp the maximum matching algorithm of the binary graph. The maximum matching problem of the binary graph can be solved in the time of O (sqrt (n) * m). Ideal for binary matching of big data. So I learned a bit. We know that the reason why the average Hungarian is slow is because he searches for only one augmented path at a time, and the HC algorithm uses BFS to find multiple augmented paths at the same time.
The following are the principles of the HC algorithm provided by netizens:
SRBGA introduced this algorithm very early, it can do O (sqrt (n) *e) time complexity, and in the actual use of the effect is good and the algorithm itself is not complex.
Hopcroft-karp algorithm is Hopcroft and Karp in 1972, the main idea of the algorithm is not to find an augmented road at the time of each augmentation but at the same time to find a few disjoint the shortest augmented road, the formation of a great augmented road set, and then along these several augmented road at the same time augmented.
It can be proved that the shortest augmented path found in each phase of the search for an augmented road set has equal lengths, and as the algorithm's shortest augmentation path length is longer, further analysis can prove that the maximum number of matches can be obtained by augmenting Ceil (sqrt (n)) at most (the proof is omitted here).
So the main difficulty now is to find the maximal shortest augmented road set in the time complexity of O (e), the idea is not complex, first from all the X's not covered point after bfs,bfs to each x node and y node maintenance distance label, if the Y node is not covered point then found a shortest augmented road, After the BFS has been found the shortest augmented road set, you can then directly with DFS to all allowed arcs (dist[y]=dist[x]+1, can see high flow propulsion HLPP implementation) similar to the search for the augmented road in Hungary, so that the complexity of O (m) can be achieved.
Implementation is not complicated, for each of the 50,000 points on both sides, 200,000-side binary graph maximum matching can be within 1s solution ~ ~
Then the problem can be easily run with the HC algorithm. Caution, be sure to pay attention to constants. The spoj ran too slowly.
Sure enough, my constant is very big ...
Codes:
1#include <cstdio>2#include <cstring>3#include <cstdlib>4#include <iostream>5#include <algorithm>6 using namespacestd;7 8 structhopcroft_carp{9 Static Const intN =50000+5;Ten Static Const intM =150000+5; One Static Const intOO =0x3f3f3f3f; A - intN1, N2, res, tot; - intDsx[n], dsy[n], mx[n], my[n]; the intque[n<<1];BOOLvi[n<<1]; - intfirst[n<<1], next[m]; - intU[m], v[m]; - + voidClear () {tot =0; res =0;} - voidADD (int from,intTo ) { +++tot; AU[tot] = from; V[tot] =to ; atNext[tot] =First[u[tot]]; -First[u[tot]] =tot; - } - - BOOLBFs () { - for(inti =1; I <= N1; + + i) dsx[i] =-1; in for(inti =1; I <= N2; + + i) dsy[i] =-1; -res =Oo; to + inthead, tail; -Head = Tail =1; the for(inti =1; I <= N1; ++i) * if(Mx[i] = =-1) que[++ Tail] = i, dsx[i] =0; $ while(Head <=tail) {Panax Notoginseng intx =Que[head]; - if(Dsx[x] > Res) Break; the + for(inti = first[x]; I i =Next[i]) { A if(Dsy[v[i]] = =-1){ theDsy[v[i]] = dsx[x] +1; + if(My[v[i]] = =-1) res =Dsy[v[i]]; - Else{ $Dsx[my[v[i]] [= dsy[v[i]] +1; $que[++ tail] =My[v[i]]; - } - } the } -++head;Wuyi } the returnRes! =Oo; - } Wu - BOOLDfsintx) { About for(inti = first[x]; I i =Next[i]) { $ if(!vi[v[i] && dsy[v[i]] = = Dsx[x] +1){ -Vi[v[i]] =true; - if(My[v[i]]! =-1&& Dsy[v[i]] = = res)Continue; - if(My[v[i]] = =-1||DFS (My[v[i])) { AMy[v[i]] =x; +MX[X] =V[i]; the return true; - } $ } the } the return false; the } the - intMaxmatch () { in intAns =0; the for(inti =1; I <= N1; + + i) mx[i] =-1; the for(inti =1; I <= N2; + + i) my[i] =-1; About the while(BFS ()) { the for(inti =1; I <= n1+n2; + + i) vi[i] =false; the for(inti =1; I <= N1; ++i) + if(Mx[i] = =-1&& DFS (i)) ans + +; - } the returnans;Bayi } the }two; the - intN1, N2, M; - the intMain () { the intx, y; thescanf"%d%d%d", &n1, &N2, &m); the two.clear (); -TWO.N1 = N1; TWO.N2 =N2; the for(inti =1; I <= m; ++i) { thescanf"%d%d", &x, &y); the two.add (x, y);94 } theprintf"%d\n", Two.maxmatch ()); the return 0; the}
Spoj 4206
Spoj 4206 Fast Maximum Matching (binary graph max matching hopcroft-carp algorithm template)