zoukankan      html  css  js  c++  java
  • hdu2444:The Accomodation of Students(二分图判断+最大匹配)

    http://acm.hdu.edu.cn/showproblem.php?pid=2444

    Problem Description

    There are a group of students. Some of them may know each other, while others don't. For example, A and B know each other, B and C know each other. But this may not imply that A and C know each other.

    Now you are given all pairs of students who know each other. Your task is to divide the students into two groups so that any two students in the same group don't know each other.If this goal can be achieved, then arrange them into double rooms. Remember, only paris appearing in the previous given set can live in the same room, which means only known students can live in the same room.

    Calculate the maximum number of pairs that can be arranged into these double rooms.

    Input

    For each data set:
    The first line gives two integers, n and m(1<n<=200), indicating there are n students and m pairs of students who know each other. The next m lines give such pairs.

    Proceed to the end of file.
     

    Output

    If these students cannot be divided into two groups, print "No". Otherwise, print the maximum number of pairs that can be arranged in those rooms.

    Sample Input

    4 4
    1 2
    1 3
    1 4
    2 3
    6 5
    1 2
    1 3
    1 4
    2 5
    3 6

    Sample Output

    No
    3

    题意分析:

    有n个学生,有的学生互相认识,有的不认识,现在要求把学生分成两队,每一队之中的所有人互相都不认识,从两队之中各挑出一个人,他们互相认识,求最多可以挑出多少对人。

    解题思路:

    分成两队就是判断是否是二分图,判断二分图的方法:选取一个点涂成红色,把和它相连的点涂成蓝色,如果最后能够把所有的点涂色并且没有相邻的颜色相同,这个图就是二分图。

    然后求一下二分图的最短匹配即可。

    #include <stdio.h>
    #include <string.h>
    #include <vector>
    #define N 220
    using namespace std;
    int e[N][N], book[N], color[N], march[N], n, m;
    vector<int>a[N];
    int dfs(int u)
    {
    	int v;
    	for(v=1; v<=n; v++)
    	{
    		if(book[v]==0 && e[u][v]==1)
    		{
    			book[v]=1;
    			if(march[v]==0 || dfs(march[v]))
    			{
    				march[v]=u;
    				return 1;
    			}
    		}
    	}
    	return 0;
    }
    int main()
    {
    	int ans, i, j, u, v, l, temp;
    	while(scanf("%d%d", &n, &m)!=EOF)
    	{
    		memset(march, 0, sizeof(march));
    		memset(color, 0, sizeof(color));
    		memset(e, 0, sizeof(e));
    		ans=0;
    		for(i=1; i<=n; i++)
    			a[i].clear();
    		for(i=1; i<=m; i++)
    		{
    			scanf("%d%d", &u, &v);
    			a[u].push_back(v);
    			a[v].push_back(u);
    			e[u][v]=e[v][u]=1;
    		}
    		temp=1;
    		for(i=1; i<=n; i++)
    		{
    			l=a[i].size();
    			if(color[i]==0)
    				color[i]=1;
    			for(j=0; j<l; j++)
    			{
    				if(color[a[i][j]]==color[i] && color[i]!=0)
    				{
    					temp=0;
    					break;
    				}
    				if(color[a[i][j]]==0)
    				{
    					if(color[i]==1)
    						color[a[i][j]]=2;
    					else
    						color[a[i][j]]=1;
    				}
    			}
    			if(temp==0)
    				break;
    		}
    		if(!temp || n==1)
    		{
    			printf("No
    ");
    			continue;
    		}
    		for(i=1; i<=n; i++)
    		{
    			memset(book, 0, sizeof(book));
    			if(dfs(i))
    				ans++;
    		}	
    		printf("%d
    ", ans/2);
    	}
    	return 0; 
    } 
  • 相关阅读:
    sqlmap从入门到精通-第七章-7-20 绕过WAF脚本-space2mssqlblank.py&space2mssqlhash.py
    Vulnhub-靶机-TOPPO: 1
    sqlmap从入门到精通-第七章-7-19 绕过WAF脚本-space2morecomment.py&space2morehash.py
    session.getdefaultinstance和getinstance的区别
    <load-on-startup>1</load-on-startup>的作用
    html表格按长度换行
    关于ORA-12154: TNS:could not resolve the connect identifier specified的问题
    Xilinux PS与PL交互::Linux-App读写REG
    Xilinux PS与PL交互:裸机程序读写FPGA-REG
    Xilinx ZYNQ-7000 平台简介
  • 原文地址:https://www.cnblogs.com/zyq1758043090/p/11852629.html
Copyright © 2011-2022 走看看