//两道大水……哦不 两道结论题
结论:二部图的最小覆盖数=二部图的最大匹配数
有向图的最小覆盖数=节点数-二部图的最大匹配数
1 //hdu 1150 2 #include<cstdio> 3 #include<iostream> 4 #include<cmath> 5 #include<algorithm> 6 #include<cstring> 7 #include<cstdlib> 8 #include<queue> 9 #include<vector> 10 #include<map> 11 #include<stack> 12 #include<string> 13 14 using namespace std; 15 16 int n,m,k; 17 int f[101][101]; 18 int link[101]; 19 bool adj[101]; 20 bool used[101]; 21 22 bool work(int x){ 23 for (int i=1;i<m;i++){ 24 if (f[x][i] && adj[i]==false){ 25 adj[i]=true; 26 if (!used[i] || work(link[i])){ 27 link[i]=x; 28 used[i]=1; 29 //printf("%d %d\n",x,i); 30 return true; 31 } 32 } 33 } 34 return false; 35 } 36 37 int main(){ 38 while (scanf("%d%d%d",&n,&m,&k)==3){ 39 memset(f,0,sizeof(f)); 40 memset(used,0,sizeof(used)); 41 for (int i=0;i<k;i++){ 42 int x,y,z; 43 scanf("%d%d%d",&z,&x,&y); 44 if (x!=0 && y!=0){ 45 f[x][y]=1; 46 } 47 } 48 int ans=0; 49 for (int i=1;i<n;i++){ 50 memset(adj,0,sizeof(adj)); 51 if (work(i)) ans++; 52 } 53 //for (int i=1;i<m;i++) printf("%d %d\n",i,link[i]); 54 printf("%d\n",ans); 55 } 56 return 0; 57 } 58 /* 59 5 5 10 60 0 1 1 61 1 1 2 62 2 1 3 63 3 1 4 64 4 2 1 65 5 2 2 66 6 2 3 67 7 2 4 68 8 3 3 69 9 4 3 70 5 5 10 71 0 1 1 72 1 1 2 73 2 1 3 74 3 1 4 75 4 2 1 76 5 2 2 77 6 2 3 78 7 2 4 79 8 3 3 80 9 4 3 81 0 82 */
1 //hdu 1151 2 #include<cstdio> 3 #include<iostream> 4 #include<cmath> 5 #include<algorithm> 6 #include<cstring> 7 #include<cstdlib> 8 #include<queue> 9 #include<vector> 10 #include<map> 11 #include<stack> 12 #include<string> 13 14 using namespace std; 15 16 int T; 17 int n,m; 18 bool f[121][121]; 19 int link[121]; 20 bool adj[121]; 21 bool used[121]; 22 23 bool work(int x){ 24 for (int i=1;i<=n;i++){ 25 if (f[x][i] && !adj[i]){ 26 adj[i]=1; 27 if (!used[i] || work(i)){ 28 link[i]=x; 29 used[i]=1; 30 return true; 31 } 32 } 33 } 34 return false; 35 } 36 37 int main(){ 38 scanf("%d",&T); 39 for (int cas=1;cas<=T;cas++){ 40 memset(used,0,sizeof(used)); 41 memset(link,0,sizeof(link)); 42 memset(f,0,sizeof(f)); 43 scanf("%d",&n); 44 scanf("%d",&m); 45 for (int i=0;i<m;i++){ 46 int x,y; 47 scanf("%d%d",&x,&y); 48 f[x][y]=1; 49 } 50 int ans=n; 51 for (int i=1;i<=n;i++){ 52 memset(adj,0,sizeof(adj)); 53 if (work(i)) ans--; 54 } 55 printf("%d\n",ans); 56 } 57 return 0; 58 } 59 /* 60 2 61 4 62 3 63 3 4 64 1 3 65 2 3 66 3 67 3 68 1 3 69 1 2 70 2 3 71 */
时间: 2024-10-27 05:52:59