标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3661 Accepted Submission(s): 2408
/** 题意:有向无环图的最小路径覆盖 做法:二分图最大匹配 有向无环图的最小路径覆盖 = 该图的顶点数-该图的最大匹配。 **/ #include<iostream> #include<cmath> #include<algorithm> #include<string.h> #include<stdio.h> #define maxn 220 using namespace std; int g[maxn][maxn]; int linker[maxn]; int used[maxn]; int n,m; int dfs(int u) { for(int v = 0; v<n; v++) { if(g[u][v] && used[v] == 0) { used[v] = 1; if(linker[v] || dfs(linker[v])) { linker[v] = u; return 1; } } } return 0; } int hungary() { int res = 0; memset(linker,-1,sizeof(linker)); for(int i=0; i<n; i++) { memset(used,0,sizeof(used)); res += dfs(i); } return res; } int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif // ONLINE_JUDGE int T; scanf("%d",&T); while(T--) { scanf("%d %d",&n,&m); int u,v; memset(g,0,sizeof(g)); for(int i=0; i<m; i++) { scanf("%d %d",&u,&v); u--; v--; g[u][v] = 1; } int res = hungary(); printf("%d\n",n - res); } return 0; }
标签:
原文地址:http://www.cnblogs.com/chenyang920/p/4392745.html