标签:
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1151
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3951 Accepted Submission(s):
2626
解题思路:求解有向无环图的最小路径覆盖问题了。
小技巧:有向无环图的最小路径覆盖=该图的顶点数-最大匹配数
最小覆盖点=最大匹配数
详见代码。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 5 using namespace std; 6 7 int Map[220][220],ok[220],n,m; 8 int vis[220]; 9 10 bool Find(int x) 11 { 12 for (int i=1; i<=n; i++) 13 { 14 if (!vis[i]&&Map[x][i]) 15 { 16 vis[i]=1; 17 if (!ok[i]) 18 { 19 ok[i]=x; 20 return true; 21 } 22 else 23 { 24 if(Find(ok[i])) 25 { 26 ok[i]=x; 27 return true; 28 } 29 } 30 } 31 } 32 return false; 33 } 34 35 int main() 36 { 37 int t,ans,a,b; 38 scanf("%d",&t); 39 while (t--) 40 { 41 ans=0; 42 memset(Map,0,sizeof(Map)); 43 memset(ok,0,sizeof(ok)); 44 scanf("%d%d",&n,&m); 45 for (int i=1; i<=m; i++) 46 { 47 scanf("%d%d",&a,&b); 48 Map[a][b]=1; 49 } 50 for (int i=1; i<=n; i++) 51 { 52 memset(vis,0,sizeof(vis)); 53 if (Find(i)) 54 { 55 ans++; 56 } 57 } 58 printf ("%d\n",n-ans); 59 } 60 return 0; 61 }
标签:
原文地址:http://www.cnblogs.com/qq-star/p/4693773.html