zoukankan      html  css  js  c++  java
  • courses二分图最大匹配

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    using namespace std;
    const int MAXN = 305;
    int uN, vN;
    bool g[MAXN][MAXN];
    int xM[MAXN], yM[MAXN];
    bool chk[MAXN];
    bool searchPath(int u)
    {
    	for(int v = 0; v < vN; v++)
    		if(g[u][v] && !chk[v])
    		{
    			chk[v] = true;
    			if(yM[v] == -1 || searchPath(yM[v]))
    			{
    				yM[v] = u;
    				xM[u] = v;
    				return true;
    			}
    		}
    	return false;
    }
    
    int maxMatch()
    {
    	int u, ret = 0;
    	memset(xM, -1, sizeof(xM));
    	memset(yM, -1, sizeof(yM));
    	for(u = 0; u < uN; u++)
    		if(xM[u] == -1)
    		{
    			memset(chk, false, sizeof(chk));
    			if(searchPath(u))ret++;
    		}
    	return ret;
    }
    int main()
    {
    	int icase;
    	scanf("%d", &icase);
    	while(icase--)
    	{
    		//矩阵一定要初始化 
    		memset(g, 0, sizeof(g));
    		int p, n;
    		scanf("%d%d", &p, &n);
    		uN = p;
    		vN = n;
    		int cnt, j;
    		for(int i = 0; i < p; i++)
    		{
    			scanf("%d", &cnt);
    			while(cnt--)
    			{
    				scanf("%d", &j);
    				//转换为下标从0开始 
    				j--;
    				g[i][j] = true;
    			}
    		}
    		//每一个课程都只有一个代表 
    		if(maxMatch() == p)puts("YES");
    		else puts("NO");
    	}
    	system("pause");
    	return 0;
    }
    

      

  • 相关阅读:
    Spring boot 请求接口404
    Windows下安装Redis
    利用maven的profiles灵活的配置多环境
    各个JSON技术的比较
    常用工具软件
    Java方法 传值方式
    JVM内存模型
    spring-task解决定时问题
    quartz Cron表达式解读
    maven 常用命令
  • 原文地址:https://www.cnblogs.com/hpustudent/p/2482030.html
Copyright © 2011-2022 走看看