-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaximum Bipartite Matching
112 lines (105 loc) · 1.95 KB
/
Maximum Bipartite Matching
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
#include<bits/stdc++.h>
using namespace std;
vector<int>v[110];
int Left[110],Right[110],visited[110];
int DFS(int n)
{
if(visited[n])
return false;
visited[n]=1;
int u,i,j;
for(i=0,j=v[n].size();i<j;i++)
{
u=v[n][i];
if(Left[u]==-1)
{
Left[u]=n;
Right[n]=u;
return true;
}
}
for(i=0,j=v[n].size();i<j;i++)
{
u=v[n][i];
if(DFS(Left[u]))
{
Left[u]=n;
Right[n]=u;
return true;
}
}
return false ;
}
int match(int m)
{
memset(Right,-1,sizeof Right);
memset(Left,-1,sizeof Left);
int i;
bool done;
do
{
done=true;
memset(visited,0,sizeof visited);
for(i=0;i<m;i++)
{
if(Right[i]==-1)
{
if(DFS(i))
{
done=false;//augmenting path exists
}
}
}
}while(!done);
int result=0;
for(i=0;i<m;i++)
{
if(Right[i]!=-1)
result++;
}
return result;
}
int main()
{
int i,j,k,m,n,t;
cin>>t;
vector<int>A,B;
for(i=1;i<=t;i++)
{
cin>>n;
A.clear();
B.clear();
for(j=1,v[0].clear();j<=n;j++)
{
v[j].clear();
cin>>k;
A.push_back(k);
}
cin>>m;
for(j=1;j<=m;j++)
{
cin>>k;
B.push_back(k);
}
for(j=0;j<n;j++)
{
for(k=0;k<m;k++)
{
if(A[j]==B[k])
{
v[j].push_back(k);
}
else if(A[j]!=0)
{
if(!(B[k]%A[j]))
{
v[j].push_back(k);
}
}
}
}
m=match(n);
cout<<"Case "<<i<<": "<<m<<endl;
}
return 0;
}