-
Notifications
You must be signed in to change notification settings - Fork 1
/
BPM with Hopcroft.cpp
64 lines (60 loc) · 1.35 KB
/
BPM with Hopcroft.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
#include<bits/stdc++.h>
using namespace std;
const int MX=500005;
int num[MX],arr[MX];
vector<int>prime;
int n,m,k,x,vis[MX],start[MX],_left[MX],_right[MX],ee,a,b; // n means node
struct edge{
int v,_next;
edge(){}
edge(int _v,int __next){v=_v,_next=__next;}
}edgeList[500000];
void init(){
ee=0;
memset(start,-1,sizeof(start));
return;
}
int cnt=1;
void addEdge(int u,int v){
u++;
v++;
edgeList[ee]=edge(v,start[u]);start[u]=ee;ee++;
return;
}
bool dfs(int src){
if(vis[src]==x)return false;
vis[src]=x;
for(int i=start[src];i!=-1;i=edgeList[i]._next){
if(_right[edgeList[i].v]==-1){
_right[edgeList[i].v]=src;
_left[src]=edgeList[i].v;
return true;
}
}
for(int i=start[src];i!=-1;i=edgeList[i]._next){
if(dfs(_right[edgeList[i].v])){
_right[edgeList[i].v]=src;
_left[src]=edgeList[i].v;
return true;
}
}
return false;
}
int HopCroft(){
init();
memset(_left,-1,sizeof(_left));
memset(_right,-1,sizeof(_right));
bool done=false;
while(!done){
x++;
done=true;
for(int i=1;i<=n;i++){
if(_left[i]==-1 && dfs(i)){
done=false;
}
}
}
int ret=0;
for(int i=1;i<=n;i++)ret+=(_left[i]!=-1);
return ret>>1;
}