/*First METHOD
Solved Using kuhn's Algorithm
Kho-Kho algorithm ~ Take a free vertex and find a augmentation which increase the match atleast by one.
Berge's Theorem ~ A matching M in a Bipartite graph is maximum if and only if there doesn exist any augmenting path.
Run in O(N*M+N*N) */
class PointyWizardHats {
public:
int parent[55],visited[55],rnode,lnode;
vector<int> graph[55];
bool can_be_kept(int bh,int br,int th,int tr) // General Constraint for the problem
{
if(br>tr and (th*br>bh*tr))
return true;
return false;
}
bool dfs(int u)
{
if(visited[u])
return false;
visited[u]=true;
for(int i=0;i<(int)graph[u].size();i++)
{
int v=graph[u][i];
if(parent[v]==-1 or dfs(parent[v]))
{
parent[v]=u;
return true;
}
}
return false;
}
int kuhn_salgo()
{
SET(parent);
for(int i=0;i<lnode;i++)
{
CLR(visited);
dfs(i);
}
return rnode-count(parent,parent+rnode,-1);
}
int getNumHats(vector <int> topHeight, vector <int> topRadius, vector <int> bottomHeight, vector <int> bottomRadius)
{
lnode=bottomHeight.size();rnode=topHeight.size();
LOOP(j,rnode)
LOOP(i,lnode)
if(can_be_kept(bottomHeight[i],bottomRadius[i],topHeight[j],topRadius[j]))
graph[i].push_back(j);
return kuhn_salgo();
}
};
Thursday, 2 January 2014
Bipartite-Matching
Labels:
Bipartite Matching,
graph
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment