HDU 5285 wyh2000 and pupil (二分图着色)
题意:
共有n个小学生,编号为1−n。将所有小学生分成2组,每组都至少有1个人。但是有些小学生之间并不认识,而且如果a不认识b,那么b也不认识a。Wyh2000希望每组中的小学生都互相认识。而且第一组的人要尽可能多。请你帮wyh2000求出第一组和第二组的人数是多少。如果找不到分组方案,则输出"Poor wyh"。
思路:
二分图着色。给的就是无向图,每次都累加人多的颜色即可。若不能着色,必定不能分成2组。如果全部都是1个颜色,那么要让其中1人过第2组。我勒个去,就因为赭色时颜色号码开小了而错。
//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <deque>
#include <algorithm>
#include <vector>
#define LL long long
#define pii pair<int,int>
#define INF 0x7f7f7f7f
using namespace std;
const int N=+; vector<int> vect[N];
int col[N];
set<int> mapp; int color(int u, int f)
{
mapp.clear();
deque<int> que;
que.push_back(u);
col[u]=f;
mapp.insert(u);
while(!que.empty())
{
int x=que.front();
que.pop_front(); for(int i=; i<vect[x].size(); i++)
{
int tmp=vect[x][i];
if(col[tmp]==col[x]) return ; //冲突
if(col[tmp]==) //没色
{
mapp.insert(tmp);//一个连同分量装进去
col[tmp]=-col[x];
que.push_back(tmp);
}
}
}
return ;
} int cal(int n)
{
memset(col,,sizeof(col));
int big=, small=, k=;
for(int i=; i<=n; i++)
{
if(!col[i])
{
if(!color(i, ++k)) return ; //统计人数
int a=, b=;
set<int>::iterator it=mapp.begin();
for(int j=; j<mapp.size(); j++)
{
if(col[*it]==k) a++;
if(col[*it]==-k) b++;
it++;
} if(a<b) swap(a, b);
big+=a;
small+=b;
}
}
if(!small) return big-;
else return big;
} int main()
{
freopen("input.txt", "r", stdin);
int t, n, m, a, b;
cin>>t;
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=; i<=n; i++) vect[i].clear(); for(int i=; i<m; i++)
{
scanf("%d%d",&a,&b);
vect[a].push_back(b);
vect[b].push_back(a);
}
if(n<=)
{
puts("Poor wyh");
continue;
}
else if(n==)
{
puts("1 1");
continue;
} int tmp=cal(n);
if(tmp) printf("%d %d\n",tmp, n-tmp);
else puts("Poor wyh"); }
return ;
}
AC代码
HDU 5285 wyh2000 and pupil (二分图着色)的更多相关文章
- HDU 5285 wyh2000 and pupil 判二分图+贪心
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5285 bc:http://bestcoder.hdu.edu.cn/contests/con ...
- HDU 5285 wyh2000 and pupil(dfs或种类并查集)
wyh2000 and pupil Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Other ...
- Hdu 5285 wyh2000 and pupil (bfs染色判断奇环) (二分图匹配)
题目链接: BestCoder Round #48 ($) 1002 题目描述: n个小朋友要被分成两班,但是有些小朋友之间是不认得的,所以规定不能把不认识的小朋友分在一个班级里面,并且一班的人数要比 ...
- HDU 5285 wyh2000 and pupil
题意:有一群人,已知某两人之间互相不认识,要把这群人分成两部分,每部分至少一人,且在每部分内没有人互不认识. 解法:图染色.某场bestcoder第二题……看完题觉得是个二分图……完全不会二分图什么的 ...
- hdu 5285 wyh2000 and pupil(二染色)
第一次用vector解得题.值得纪念,这道题是二染色问题,我用bfs解得.就是染色,推断,计数问题,其 实挺简单的,就是得判一下特殊情况,当n<2的时候就不能有解,由于题目要求每一个组至少有一个 ...
- ACM: HDU 5285 wyh2000 and pupil-二分图判定
HDU 5285 wyh2000 and pupil Time Limit:1500MS Memory Limit:65536KB 64bit IO Format:%I64d &a ...
- HDU 5313 Bipartite Graph (二分图着色,dp)
题意: Soda有一个n个点m条边的二分图, 他想要通过加边使得这张图变成一个边数最多的完全二分图. 于是他想要知道他最多能够新加多少条边. 注意重边是不允许的. 思路: 先将二分图着色,将每个连通分 ...
- HDU 5285:wyh2000 and pupil
wyh2000 and pupil Accepts: 93 Submissions: 925 Time Limit: 3000/1500 MS (Java/Others) Memory Lim ...
- 二分图判定+点染色/并查集 BestCoder Round #48 ($) 1002 wyh2000 and pupil
题目传送门 /* 二分图判定+点染色:因为有很多联通块,要对所有点二分图匹配,若不能,存在点是无法分配的,no 每一次二分图匹配时,将点多的集合加大最后第一个集合去 注意:n <= 1,no,两 ...
随机推荐
- MVC3 Model Binding验证方式
1.使用ModelState在Action中进行验证 [HttpPost] public ViewResult MakeBooking(Appointment appt) { if (string.I ...
- js获取时间
var myDate = new Date(); console.log(myDate.toLocaleString()); function getNowFormatDate() { var dat ...
- 【BZOJ】【2253】【WC 2010 BeijingWC】纸箱堆叠
树套树 Orz zyf 我的树套树不知道为啥一直WA……只好copy了zyf的写法TAT 这题还可以用CDQ分治来做……但是蒟蒻不会…… //y坐标的树状数组是按权值建的……所以需要离散化…… /** ...
- linux cmake 安装mysql5.5.11,以及更高版本
1.下载mysql5.5.12和cmake wget http://mirrors.sohu.com/mysql/MySQL-5.5/mysql-5.5.12-linux2.6-i686.tar.gz ...
- Unity3D脚本中文系列教程(一)
原地址:http://dong2008hong.blog.163.com/blog/static/46968827201403115643431/?suggestedreading&wumii ...
- What is XMLHTTP? How to use security zones in Internet Explorer
Types of Security Zones Internet Zone This zone contains Web sites that are not on your computer or ...
- SQL SERVER(MSSQLSERVER) 服务无法启用 特定服务错误:126
SQL SERVER(MSSQLSERVER) 服务无法启用 特定服务错误:126 对于这样一个错误google了一下 说是 要禁止掉via才行 回到SQL配置管理器中 禁止掉via 果然可以重新 ...
- [hackerrank]Service Lane
https://www.hackerrank.com/challenges/service-lane 用RMQ做的,其实暴力也能过~ #include <iostream> #includ ...
- Qt4升级Qt5注意问题
Qt4升级Qt5注意问题 Qt4过渡到Qt5的项目一开始就受阻,记录一下遇到的下面的问题 --->编译遇到类似错误: error: QCalendarWidget: No such file o ...
- SRAM与SDRAM的区别
http://www.cnblogs.com/spartan/archive/2011/05/06/2038747.html SDRAM SDRAM(Synchronous Dynamic Rando ...