题目链接:戳这里
题意:n个节点,m条路径,最少放几个伞兵使得每个节点都能被到达?
分析:最小路径覆盖问题。最小路径覆盖=节点数-最大匹配数。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int read()
{
char c;int sum=0,f=1;c=getchar();
while(c<'0' || c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0' && c<='9'){sum=sum*10+c-'0';c=getchar();}
return sum*f;
}
int T;
int n,m;
int match[305];
bool vis[305];
vector<int> edge[305];
int ans;
bool dfs(int x)
{
for(int i=0;i<edge[x].size();i++)
{
int nex=edge[x][i];
if(vis[nex]) continue;
vis[nex]=1;
if(!match[nex] || dfs(match[nex]))
{
match[nex]=x;
return true;
}
}
return false;
}
int main()
{
T=read();
while(T--)
{
n=read();m=read();
for(int i=1;i<=n;i++)
edge[i].clear();
memset(match,0,sizeof(match));
ans=0;
for(int i=1;i<=m;i++)
{
int u=read(),v=read();
edge[u].push_back(v);
}
for(int i=1;i<=n;i++)
{
memset(vis,0,sizeof(vis));
if(dfs(i))
ans++;
}
printf("%d\n",n-ans);
}
return 0;
}