-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhopcroft.sublime-snippet
113 lines (104 loc) · 1.82 KB
/
hopcroft.sublime-snippet
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
<snippet>
<content><![CDATA[
class hopcroftKarp
{
public:
const int inf = 1e18;
const int nil = 0;
vector<vector<int>>adj;
vector<int>dist;
vector<int>match;
vector<pair<int, int>>matching;
int ans = 0;
int N, M; // size of left and right set
bool dfs(int curr)
{
for (auto it : adj[curr])
{
if (match[it] == -1)
{
match[it] = curr;
match[curr] = it;
dist[curr]=inf;
return true;
}
else if (dist[match[it]] == dist[curr] + 1 && dfs(match[it]))
{
match[it] = curr;
match[curr] = it;
dist[curr]=inf;
return true;
}
}
dist[curr] = inf;
return false;
}
bool bfs()
{
queue<int>q;
bool freenode = false;
for (int i = 0; i < N; ++i)
{
if (match[i] == -1)
q.push(i), dist[i] = nil;
else
dist[i] = inf;
}
while (!q.empty())
{
int curr = q.front();
q.pop();
for (auto it : adj[curr])
{
if (match[it] == -1)
{
freenode = true;
}
else if (dist[match[it]] == inf)
{
dist[match[it]] = dist[curr] + 1;
q.push(match[it]);
}
}
}
return freenode;
}
void cal()
{
while (bfs())
{
for (int i = 0; i < N; ++i)
{
if (match[i] == -1 && dfs(i))
{
ans++;
}
}
}
}
hopcroftKarp(vector<vector<int>>&adj, int N, int M) // bipartite graph given
{
this->adj = adj;
this->N = N;
this->M = M;
dist.resize(N, inf);
match.resize(N + M, -1);
cal();
for(int i=0;i<N;i++)
{
if(match[i]!=-1)
{
matching.push_back({i,match[i]-N});
}
}
}
hopcroftKarp(vector<vector<int>>&adj) // necessarily given bipartite
{
}
};
]]></content>
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<tabTrigger>hopcroft</tabTrigger>
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<!-- <scope>source.python</scope> -->
</snippet>