【HDU - 2444】The Accomodation of Students(二分图判断 + 匈牙利算法求最大匹配)

题干:

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个学生。其中有些人相互认识,有些人相互之间不认识。现在有m对认识关系。问你能否把这n个人分成两个组。一个组中没有相互认识的人。如果不能做到的话,输出NO。如果可以的话,把这些人分到一些房间中,只有相互认识的人能分到一个房间里。问你需要多少房间。
 

解题报告:

   这是判断二分图和二分图匹配的裸题。

首先要判断该图是否为二分图。当该图为二分图时,才能分为两个组。当判断为二分图时,我们需要求出该二分图的最大匹配数目。得出的结果除2就得出了需要房间数。(或者只对col==1的进行find匹配,这样就不需要除以2了。)

二分图的判定:
    在判定一个图是否为二分图的时候只需要进行判断图中是否存在奇数长度的回路
    常用的办法是基于BFS的相邻染色法 即对任意一个点进行BFS如果uv之间有一条边 我们从u访问到v 如果v未被染色 则v将被染成和u相对的染色(比如1和0)
   判断出口:如果相邻节点(u和v)是相同颜色 则存在奇回路

   还有个优化(但是貌似没卵用),就是used数组不是bool的,设成int,然后每次就不需要置零used了,直接看是否等于i就可以了,相当于find中两个参数find(i,i),其中bool find(int x,int id),看是否是id,就可以看出是否是

AC代码:

#include<bits/stdc++.h>
using namespace std;
int nxt[205];
int col[205];
bool used[205];
int n,m;
vector<int> vv[205];
bool bfs() {memset(col,-1,sizeof col);queue<int> q;q.push(1);col[1] = 1;while(!q.empty()) {int cur = q.front();q.pop();for(int i = 0; i<vv[cur].size(); i++) {int now = vv[cur][i];if(col[now] == -1) {col[now] = !col[cur];q.push(now);}else {if(col[now] == col[cur]) return 0;}}} return 1;
}
bool find(int x) {for(int i = 0; i<vv[x].size(); i++) {int now = vv[x][i];if(used[now] == 1) continue;used[now] = 1;if(nxt[now] == 0 || find(nxt[now])) {nxt[now] = x;return 1;}}return 0 ;
}
int main()
{int u,v,ans;while(~scanf("%d%d",&n,&m)) {memset(nxt,0,sizeof nxt);for(int i = 0; i<=200; i++) vv[i].clear();for(int i = 1; i<=m; i++) {scanf("%d%d",&u,&v);vv[u].push_back(v);vv[v].push_back(u);}if(!bfs()) {puts("No");continue;}ans = 0;for(int i = 1; i<=n; i++) {memset(used,0,sizeof used);if(find(i)) ans++;}printf("%d\n",ans/2);}return 0;
}

总结:

   1WA了,因为忘记了清空vv和nxt数组。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441394.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

最长上升子序列 java_最长上升子序列 O(nlogn)解法 (java)

最长递增子序列问题&#xff1a;在一列数中寻找一些数&#xff0c;这些数满足&#xff1a;任意两个数a[i]和a[j]&#xff0c;若i 设dp[i]表示以i为结尾的最长递增子序列的长度&#xff0c;则状态转移方程为&#xff1a; dp[i] max{dp[j]1}, 1<j 这样简单的复杂度为O(n^2)&a…

C++关于引用的注意事项 总结知识点

对函数的引用&#xff1a; #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #def…

中介者模式java_图解Java设计模式之中介者模式

智能家庭项目1)智能家庭包括各种设备&#xff0c;闹钟、咖啡机、电视机、窗帘等2)主人要看电视时&#xff0c;各个设备可以协同工作&#xff0c;自动完成看电视的准备工作&#xff0c;比如流程为 &#xff1a;闹铃响起 - 》咖啡机开始做咖啡 -》窗帘自动落下 -》电视机开始播放…

python 自动驾驶线性识别路段

python视觉库 OpenCV:OpenCV是一个开源的计算机视觉库,提供了丰富的图像处理和计算机视觉算法。它可以用于图像处理、对象识别、特征提取、图像分割等。 Matplotlib:Matplotlib是一个绘图库,可用于创建高质量的二维图表和绘图。它提供了类似于MATLAB的绘图接口,使用户可以…

【CodeForces - 202A】LLPS (思维,字符串)

题干&#xff1a; This problems actual name, "Lexicographically Largest Palindromic Subsequence" is too long to fit into the page headline. You are given string s consisting of lowercase English letters only. Find its lexicographically largest p…

elementui表格宽度适应内容_elementui表格中的列怎么实现自适应列宽

elementui表格中的列怎么实现自适应列宽发布时间&#xff1a;2020-12-28 14:58:04来源&#xff1a;亿速云阅读&#xff1a;53作者&#xff1a;Leah这期内容当中小编将会给大家带来有关elementui表格中的列怎么实现自适应列宽&#xff0c;文章内容丰富且以专业的角度为大家分析和…

【POJ - 2398】Toy Storage (计算几何,二分找位置,叉积,点和直线的位置关系)

题干&#xff1a; Mom and dad have a problem: their child, Reza, never puts his toys away when he is finished playing with them. They gave Reza a rectangular box to put his toys in. Unfortunately, Reza is rebellious and obeys his parents by simply throwing …

【POJ - 1269 】Intersecting Lines (计算几何,直线间的位置关系)

题干&#xff1a; We all know that a pair of distinct points on a plane defines a line and that a pair of lines on a plane will intersect in one of three ways: 1) no intersection because they are parallel, 2) intersect in a line because they are on top of …

java servlet 转发和重定向_JavaWeb(一)Servlet中乱码解决与转发和重定向的区别

前言前面其实已经把Servlet中所有的内容都介绍完了&#xff0c;这篇讲补充一点乱码和重定向与转发之间的区别&#xff01;一、request请求参数出现乱码问题1.1、get请求1)乱码示例get请求的参数是在url后面提交过来的&#xff0c;也就是在请求行中。结果&#xff1a;Servlet_de…

【POJ - 3304 】Segments(计算几何,思想转化,直线和线段相交)

题干&#xff1a; Given n segments in the two dimensional space, write a program, which determines if there exists a line such that after projecting these segments on it, all projected segments have at least one point in common. Input Input begins with a…

java调用lingo_使用Lingo增强JMS

虽然activemqjencks的jms轻量级解决方案已经很好地在psa中work了&#xff0c;尤其spring的JmsTemplate使得代码更简单&#xff0c;但是还是存在问题。问题来自暑期做psa的时候&#xff0c;linke突然提出要求&#xff0c;需要MDP返回些处理信息&#xff0c;比如处理结果、异常&a…

jsk Star War (线段树维护区间最小最大值 + 二分)

Description 公元20XX年&#xff0c;人类与外星人之间的大战终于爆发。 现有一个人类军团&#xff0c;由n名士兵组成&#xff0c;第i个士兵的战斗力值对应一个非负整数ai (1 \leq i \leq n1≤i≤n)。 有一天&#xff0c;某个战力爆表的外星人NaN单独向地球人宣战&#xff0c…

java jsp导出pdf文件_JSP页面导出PDF格式文件

JSP页面导出PDF格式文件基本在前端页面可以全部完成添加下载链接的点击事件var downPdf document.getElementById("downLoad");downPdf.onclick function() {downPdf.parentNode.removeChild(downPdf);html2canvas(document.body, {onrendered:function(canvas) {v…

【51Nod - 1094】和为k的连续区间 (前缀和,二分查找)

题干&#xff1a; 一整数数列a1, a2, ... , an&#xff08;有正有负&#xff09;&#xff0c;以及另一个整数k&#xff0c;求一个区间i,ji,j&#xff0c;(1 < i < j < n)&#xff0c;使得aii ... ajj k。 Input 第1行&#xff1a;2个数N,K。N为数列的长度。K为需…

简述java的线程_Java多线程的简述

线程与进程进程&#xff1a;​ 是指一个内存中运行的应用程序&#xff0c;每个进程都有一个独立的内存空间&#xff0c;一个应用程序可以同时运行多个进程&#xff1b;进程也是程序的一次执行过程&#xff0c;是系统运行程序的基本单位&#xff1b;系统运行一个程序即是一个进程…

【POJ - 3253】Fence Repair(贪心,时光倒流)

题干&#xff1a; Description Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having some integer length Li (1 ≤ Li ≤ 50,000) units. He t…

java unsafe park_Java中Unsafe类详解

http://www.cnblogs.com/mickole/articles/3757278.htmlJava不能直接访问操作系统底层&#xff0c;而是通过本地方法来访问。Unsafe类提供了硬件级别的原子操作&#xff0c;主要提供了以下功能&#xff1a;1、通过Unsafe类可以分配内存&#xff0c;可以释放内存&#xff1b;类中…

【CodeForces - 124D】Squares (旋转坐标系,计算几何,思维)

题干&#xff1a; You are given an infinite checkered field. You should get from a square (x1; y1) to a square (x2; y2). Using the shortest path is not necessary. You can move on the field squares in four directions. That is, when you are positioned in any…

java图形设计_java图形界面设计含答案

十一章 图形用户界面程序设计入门一&#xff0e;选择题&#xff1a;1. 容器Panel和applet缺省使用的布局编辑策略是 BA、BorderLayout B、FlowLayoutC、GridLayout D、CarLayout2. .applet类的直接父类是&#xff1a; BA、Component类 B、Container类C、Frame类 D、Panel类3. .…

【qduoj - 夏季学期创新题】骑士游历(递推dp)

题干&#xff1a; 描述 输入 输入包含多组数据&#xff0c;第一行T表示数据组数接下来每行六个整数n&#xff0c;m&#xff0c;x1&#xff0c;y1&#xff0c;x2&#xff0c;y2(分别表示n&#xff0c;m&#xff0c;起点坐标&#xff0c;终点坐标) 输出 输出T行&#xff0c;表示…