Hdu 5285 wyh2000 and pupil (bfs染色判断奇环) (二分图匹配)
题目链接:
题目描述:
n个小朋友要被分成两班,但是有些小朋友之间是不认得的,所以规定不能把不认识的小朋友分在一个班级里面,并且一班的人数要比二班的人数多,每个班的人数都大于零。
解题思路:
hdu给出的题解是二分图匹配加上贪心,就不多说了。
还可以用bfs对节点染色,建好图后,对节点进行bfs分成,偶数成与奇数成染成不同的颜色,颜色相同的节点都可以分到同一个集合里面,但是要判断一下奇环,如果出现奇环的话,是无法进行分组的。在每次bfs的时候再加上贪心累计求和。
#include <queue>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = ;
vector < int > G[maxn]; int x, y, n, m, vis[maxn], flag;
void bfs (int s)
{
int p, q, zreo, one, num;
queue <int> Q;
zreo = ;
one = vis[s] = ;
Q.push(s);
while (!Q.empty())
{
p = Q.front();
Q.pop();
num = (vis[p] + ) % ;
for (int i=; i<G[p].size(); i++)
{
q = G[p][i];
if (vis[q]!=- && num != vis[q])
flag = ;
if (vis[q]==-)
{
Q.push(q);
vis[q] = num;
if (num == )
zreo ++;
else
one ++;
}
}
}
x += max (zreo, one);
y += min (one, zreo);
}
int main ()
{
int t;
scanf ("%d", &t);
while (t --)
{
scanf ("%d %d", &n, &m);
memset (vis, -, sizeof(vis));
for (int i=; i<=n; i++)
G[i].clear();
x = y = flag = ;
while (m --)
{
int a, b;
scanf ("%d %d", &a, &b);
G[a].push_back (b);
G[b].push_back (a);
}
for (int i=; i<=n; i++)
{
if (vis[i] == -)
bfs (i);
if (flag)
break;
}
if (x == n)
{
x --;
y ++;
}
if (flag || x < || y < )
printf ("Poor wyh\n");
else
printf ("%d %d\n", x, y);
}
return ;
}
用二分图匹配要比bfs快一半,内存也要少好多,所以再贴一个二分图匹配的代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn = ;
struct Edge
{
int to, next;
}edge[*maxn];
int tot, head[maxn], vis[maxn]; void init ()
{
tot = ;
memset (head, -, sizeof(head));
}
void Add (int from, int to)
{
edge[tot].to = to;
edge[tot].next = head[from];
head[from] = tot ++;
}
bool dfs (int x, int c, int &a, int &b)
{
b ++;
if (c)
a ++;
vis[x] = c;
for (int i=head[x]; i!=-; i=edge[i].next)
{
int num = edge[i].to;
if (vis[num] == c)
return false;
if (vis[num] == - && !dfs(num, c^, a, b))
return false;
}
return true;
}
int main ()
{
int t, n, m;
scanf ("%d", &t);
while (t --)
{
scanf ("%d %d", &n, &m);
init ();
while (m --)
{
int a, b;
scanf ("%d %d", &a, &b);
Add (a, b);
Add (b, a);
}
int x, y;
bool flag = true;
memset (vis, -, sizeof(vis));
x = y = ;
for (int i=; i<=n; i++)
{
int a, b;
a = b = ;
if (vis[i] == -)
{
if (!dfs (i, , a, b))
{
flag = false;
break;
}
}
x += max (a, b - a);
y += min (a, b - a);
}
if (x == n)
{
x --;
y ++;
}
if (!flag || x< || y<)
printf ("Poor wyh\n");
else
printf ("%d %d\n", x, y);
}
return ;
}
Hdu 5285 wyh2000 and pupil (bfs染色判断奇环) (二分图匹配)的更多相关文章
- 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(二染色)
第一次用vector解得题.值得纪念,这道题是二染色问题,我用bfs解得.就是染色,推断,计数问题,其 实挺简单的,就是得判一下特殊情况,当n<2的时候就不能有解,由于题目要求每一个组至少有一个 ...
- HDU 5285 wyh2000 and pupil
题意:有一群人,已知某两人之间互相不认识,要把这群人分成两部分,每部分至少一人,且在每部分内没有人互不认识. 解法:图染色.某场bestcoder第二题……看完题觉得是个二分图……完全不会二分图什么的 ...
- HDU 5285 wyh2000 and pupil (二分图着色)
题意: 共有n个小学生,编号为1−n.将所有小学生分成2组,每组都至少有1个人.但是有些小学生之间并不认识,而且如果a不认识b,那么b也不认识a.Wyh2000希望每组中的小学生都互相认识.而且第一组 ...
- ACM: HDU 5285 wyh2000 and pupil-二分图判定
HDU 5285 wyh2000 and pupil Time Limit:1500MS Memory Limit:65536KB 64bit IO Format:%I64d &a ...
- HDU - 3478 Catch(判奇环/二分图)
http://acm.hdu.edu.cn/showproblem.php?pid=3478 题意 给一个无向图和小偷的起点,小偷每秒可以向相邻的点出发,问有没有一个时间点小偷可能出现在任何点. 分析 ...
- [cf557d]Vitaly and Cycle(黑白染色求奇环)
题目大意:给出一个 n 点 m 边的图,问最少加多少边使其能够存在奇环,加最少边的情况数有多少种. 解题关键:黑白染色求奇环,利用数量分析求解. 奇环:含有奇数个点的环. 二分图不存在奇环.反之亦成立 ...
- 【POJ 2942】Knights of the Round Table(双联通分量+染色判奇环)
[POJ 2942]Knights of the Round Table(双联通分量+染色判奇环) Time Limit: 7000MS Memory Limit: 65536K Total Su ...
随机推荐
- POJ 1741 Tree【Tree,点分治】
给一棵边带权树,问两点之间的距离小于等于K的点对有多少个. 模板题:就是不断找树的重心,然后分开了,分治,至少分成两半,就是上限为log 然后一起统计就ok了 #include<iostream ...
- 基于HttpClient4.5.2实现的HttpClient工具类
1.maven依赖: <dependency> <groupId>org.apache.commons</groupId> <artifactId>co ...
- Jquery根据JSON生成Table
先说下背景 本人属于juqery小白中的极品小白.基本对于JS jquery这些不懂.用到时候基本百度下 拿过来改改OK. 上面这东西让我弄了三天.可能对于其他人来说 一天就搞定了 .看来还真得去学一 ...
- 【存储过程】MySQL存储过程/存储过程与自定义函数的区别
---------------------------存储过程-------------------- 语法: 创建存储过程: CREATE [definer = {user|current_user ...
- Linux下汇编语言学习笔记62 ---
这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...
- HDU——1498 50 years, 50 colors
50 years, 50 colors Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- cogs——2084. Asm.Def的基本算法
2084. Asm.Def的基本算法 ★☆ 输入文件:asm_algo.in 输出文件:asm_algo.out 简单对比时间限制:1 s 内存限制:256 MB [题目描述] “有句 ...
- 洛谷 P1883 函数
P1883 函数 题目描述 给定n个二次函数f1(x),f2(x),...,fn(x)(均形如ax^2+bx+c),设F(x)=max{f1(x),f2(x),...,fn(x)},求F(x)在区间[ ...
- Ubuntu 16.04安装ntopng流量监控软件
ntop官方在2012年就已经不再更新,取代的是ntopng.ntopng具有Web页面,适合做网络管理监控软件.比如监控局域网内多台机器的上网情况等. 不过这个东西感觉不太准,最好的方案应该把安装n ...
- registerServiceWorker创建的React项目中的registerServiceWorker作用?
1.安装create-react-app:npm/cnpm installl create-react-app -g 2.创建项目:create-react-app my-first-app 3.此时 ...