【SSL_1340】最小路径覆盖

最小路径覆盖


Description

定义: 一个不含圈的有向图G中,G的一个路径覆盖是一个其结点不相交的路径集合P,图中的每一个结点仅包含于P中的某一条路径。路径可以从任意结点开始和结束,且长度也为任意值,包括0。请你求任意一个不含圈的有向图G的最小路径覆盖数。
提示:最小路径覆盖数=G的定点数-最小路径覆盖中的边数
最小路径覆盖数=原图G的顶点数-二分图的最大匹配数

Input

t 表示有t组数据;n 表示n个顶点(n<=120);m 表示有m条边;
接下来m行,每行有两个数 i,j表示一条有向边。

Output

最小路径覆盖数

Sample Input

2 
4
3
3 4
1 3
2 3
3
3
1 3
1 2
2 3

Sample Output

2
1

解题思路

从题目得知:我们直接套最大匹配的模板就好了

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

int T;
int n,m;
int tot,hd[130];
int cv[130],f[130];

struct abc{
    
    
	int to,next;
}s[1010000];

void add(int x,int y)
{
    
    
	s[++tot]=(abc){
    
    y,hd[x]};
	hd[x]=tot;
}

bool find(int x)
{
    
    
	for(int i=hd[x];i;i=s[i].next)
	{
    
    
		int y=s[i].to;
		if(!cv[y])
		{
    
    
			cv[y]=1;
			int q=f[y];
			f[y]=x;
			if(q==0||find(q))
				return 1;
			f[y]=q;
		}
	}
	return 0;
}

int main()
{
    
    
	cin>>T;
	while(T--)
	{
    
    
		cin>>n>>m;
		for(int i=1;i<=m;i++)
		{
    
    
			int x,y;
			scanf("%d%d",&x,&y);
			add(x,y);
		}
		int ans=0;
		for(int i=1;i<=n;i++)
		{
    
    
			memset(cv,0,sizeof(cv));
			if(find(i))
				ans++;
		}
		cout<<n-ans<<endl;
		tot=0;
		memset(f,0,sizeof(f));
		memset(hd,0,sizeof(hd));
	}
}

猜你喜欢

转载自blog.csdn.net/SSL_guyixin/article/details/108163587