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 ...
随机推荐
- hdu 3657 最小割(牛逼!!!!)总算理解了
<strong></strong> 转载:http://blog.csdn.net/me4546/article/details/6662959 加颜色的太棒了!!! 在网上看 ...
- hdu 3697 贪心
#include<stdio.h> #include<stdlib.h> #include<string.h> #define inf 0x3fffffff #de ...
- SpringBoot入门系列~Spring-Data-JPA自动建表
1.pom.xml引入Spring-Data-Jpa和mysql依赖 <!-- Spring-data-jpa依赖 --> <dependency> <groupId&g ...
- msp430入门学习23
msp430的ADC(模数转换) msp430入门学习
- Linux下汇编语言学习笔记67 ---
这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...
- hdu——2586 How far away ?
How far away ? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- 洛谷 P1023 税收与补贴问题
P1023 税收与补贴问题 题目背景 每样商品的价格越低,其销量就会相应增大.现已知某种商品的成本及其在若干价位上的销量(产品不会低于成本销售),并假设相邻价位间销量的变化是线性的且在价格高于给定的最 ...
- Hdoj 3697 Selecting courses 【贪心】
Selecting courses Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 62768/32768 K (Java/Others ...
- 机器学习笔记——SVM
SVM(Support Vector Machine).中文名为 支持向量机.就像自己主动机一样.听起来异常神气.最初总是纠结于不是机器怎么能叫"机",后来才知道事实上此处的&qu ...
- 读书笔记:Information Architecture for the World Wide Web, 3rd Edition 北极熊 第一部分 1-3
Introducing Information Architecture 信息架构简介 Chapter 1 Defining Information Architecture 信息架构的意义(我们盖房 ...