对于每个可以同机飞行的正副驾驶员,连一条容量为1的边,可以跑一次最大匹配或所有正驾驶员向s连容量为1的边,所有副驾驶向t连容量为1的边跑一遍最大流即可。
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 1e5+10;
const int inf = 0x3f3f3f3f;
int n,m,s,t,tol,head[maxn],dep[maxn];
struct Edge
{
int v,w,nxt;
}E[maxn];
void add_edge(int u,int v,int w)
{
E[tol] = Edge{v,w,head[u]};
head[u] = tol++;
}
bool Bfs()
{
memset(dep,0, sizeof(dep));
queue<int>q;
while(!q.empty())
q.pop();
q.push(s);
dep[s] = 1;
while(!q.empty())
{
int u = q.front();
q.pop();
for(int i = head[u];i != -1;i = E[i].nxt)
{
if(E[i].w && !dep[E[i].v])
{
dep[E[i].v] = dep[u] + 1;
q.push(E[i].v);
if(E[i].v == t)
return true;
}
}
}
return false;
}
int Dfs(int u,int f)
{
if(u == t)
return f;
int used = 0,d = 0;
for(int i = head[u];i != -1;i = E[i].nxt)
{
if(dep[u] == dep[E[i].v] - 1 && E[i].w)
{
if((d = Dfs(E[i].v,min(f - used,E[i].w))))
{
used += d;
E[i].w -= d;
E[i^1].w += d;
}
}
}
if(!used)
dep[u] = 0;
return used;
}
int Dinic()
{
int max_flow = 0,d;
while(Bfs())
{
while((d = Dfs(s,inf)))
max_flow += d;
}
return max_flow;
}
signed main()
{
//freopen("in","r",stdin);
ios::sync_with_stdio(false);
cin.tie(0);
memset(head,-1, sizeof(head));
int a,b;
s = 110,t = 111;
cin >> n >> m;
while(cin >> a >> b)
{
add_edge(a,b,1);
add_edge(b,a,0);
}
for(int i = 1;i <= n; i++)
{
if(i <= m)
{
add_edge(s,i,1);
add_edge(i,s,0);
continue;
}
add_edge(i,t,1);
add_edge(t,i,0);
}
cout << Dinic() << endl;
return 0;
}