题目链接:

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

bc:http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=609&pid=1002

wyh2000 and pupil

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 1304    Accepted Submission(s): 418

Problem Description
Young theoretical computer scientist wyh2000 is teaching his pupils.
Wyh2000 has n pupils.Id of them are from 1 to n.In order to increase the cohesion between pupils,wyh2000 decide to divide them into 2 groups.Each group has at least 1 pupil.
Now that some pupils don't know each other(if a doesn't know b,then b doesn't know a).Wyh2000 hopes that if two pupils are in the same group,then they know each other,and the pupils of the first group must be as much as possible.
Please help wyh2000 determine the pupils of first group and second group. If there is no solution, print "Poor wyh".
 
Input
In the first line, there is an integer T indicates the number of test cases.
For each case, the first line contains two integers n,m indicate the number of pupil and the number of pupils don't konw each other.
In the next m lines,each line contains 2 intergers x,y(x<y),indicates that x don't know y and y don't know x,the pair (x,y) will only appear once.
T≤10,0≤n,m≤100000
 
Output
For each case, output the answer.
 
Sample Input
2
8 5
3 4
5 6
1 2
5 8
3 5
5 4
2 3
4 5
3 4
2 4
 
Sample Output
5 3 Poor wyh
 

题解:

有解条件:每个连通分量都为二分图,且能够分为人数都大于1的两组。

先用黑白染色法来判断每个连通分量是否都为二分图,之后对每个联通分量,分成黑白两组后人数多的进第一组,人数少的进第二组。

这样贪心会有一个问题,第二个组的人数少于1,所以还要做一些调整。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int maxn = ;
const int INF = 0x3f3f3f3f; struct Edge {
int v, ne;
Edge(int v = , int ne = ) :v(v), ne(ne) {};
}egs[maxn * ]; int n, m;
int head[maxn], tot;
//vis[i]==-1:没访问过,==0:白色,==1:黑色
int vis[maxn]; void addEdge(int u, int v) {
egs[tot] = Edge(v, head[u]);
head[u] = tot++;
} int vis2[maxn];
//统计该连通分量黑白两色的人数
void dfs2(int cur, int &cnt0, int &cnt1) {
vis2[cur] = ;
if (vis[cur] == ) cnt0++;
else cnt1++;
int p = head[cur];
while (p != -) {
Edge &e = egs[p];
if (!vis2[e.v]) {
dfs2(e.v, cnt0, cnt1);
}
p = e.ne;
}
}
//二染色判二分图
bool dfs(int cur) {
int p = head[cur];
while (p != -) {
Edge &e = egs[p];
if (vis[e.v] == -) {
vis[e.v] = vis[cur] ^ ;
dfs(e.v);
}
else if (vis[e.v] == vis[cur]) {
return false;
}
p = e.ne;
}
return true;
} void init() {
tot = ;
memset(head, -, sizeof(head));
memset(vis, -, sizeof(vis));
memset(vis2, , sizeof(vis2));
} int main() {
int tc;
scanf("%d", &tc);
while (tc--) {
init();
scanf("%d%d", &n, &m);
for (int i = ; i < m; i++) {
int u, v;
scanf("%d%d", &u, &v);
u--, v--;
addEdge(u, v);
addEdge(v, u);
}
int ans = ;
int flag = , adj = INF;
for (int i = ; i < n; i++) {
if (vis[i] == -) {
vis[i] = ;
if (!dfs(i)) {
flag = ; break;
}
else {
int cnt0 = , cnt1 = ;
dfs2(i, cnt0, cnt1);
ans += max(cnt0, cnt1);
if (abs(cnt0 - cnt1) > ) {
adj = min(adj, abs(cnt0 - cnt1));
}
}
}
}
//调整
if (adj == INF) adj = ;
if (n - ans < ) { ans -= adj; }
if (flag || n - ans< || ans<) {
printf("Poor wyh\n");
}
else {
printf("%d %d\n", max(ans, n - ans), min(ans, n - ans));
}
}
return ;
}

HDU 5285 wyh2000 and pupil 判二分图+贪心的更多相关文章

  1. Hdu 5285 wyh2000 and pupil (bfs染色判断奇环) (二分图匹配)

    题目链接: BestCoder Round #48 ($) 1002 题目描述: n个小朋友要被分成两班,但是有些小朋友之间是不认得的,所以规定不能把不认识的小朋友分在一个班级里面,并且一班的人数要比 ...

  2. HDU 5285 wyh2000 and pupil(dfs或种类并查集)

    wyh2000 and pupil Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Other ...

  3. HDU 5285 wyh2000 and pupil (二分图着色)

    题意: 共有n个小学生,编号为1−n.将所有小学生分成2组,每组都至少有1个人.但是有些小学生之间并不认识,而且如果a不认识b,那么b也不认识a.Wyh2000希望每组中的小学生都互相认识.而且第一组 ...

  4. HDU 5285 wyh2000 and pupil

    题意:有一群人,已知某两人之间互相不认识,要把这群人分成两部分,每部分至少一人,且在每部分内没有人互不认识. 解法:图染色.某场bestcoder第二题……看完题觉得是个二分图……完全不会二分图什么的 ...

  5. hdu 5285 wyh2000 and pupil(二染色)

    第一次用vector解得题.值得纪念,这道题是二染色问题,我用bfs解得.就是染色,推断,计数问题,其 实挺简单的,就是得判一下特殊情况,当n<2的时候就不能有解,由于题目要求每一个组至少有一个 ...

  6. ACM: HDU 5285 wyh2000 and pupil-二分图判定

     HDU 5285  wyh2000 and pupil Time Limit:1500MS     Memory Limit:65536KB     64bit IO Format:%I64d &a ...

  7. HDU 5285:wyh2000 and pupil

    wyh2000 and pupil  Accepts: 93  Submissions: 925  Time Limit: 3000/1500 MS (Java/Others)  Memory Lim ...

  8. 二分图判定+点染色/并查集 BestCoder Round #48 ($) 1002 wyh2000 and pupil

    题目传送门 /* 二分图判定+点染色:因为有很多联通块,要对所有点二分图匹配,若不能,存在点是无法分配的,no 每一次二分图匹配时,将点多的集合加大最后第一个集合去 注意:n <= 1,no,两 ...

  9. hdu 5285 二分图黑白染色

    题意:给出 n 个人,以及 m 对互不认识的关系,剩余的人都互相认识,要将所有人分成两组,组内不能有互不认识的人,要求每组至少有一人,并且第一组人数尽量多,问两组人数或不可能时单独输出 BC 48 场 ...

随机推荐

  1. linux 命令 sort

    Linux下的sort排序命令详解(一) 1 sort的工作原理 sort将文件的每一行作为一个单位,相互比较,比较原则是从首字符向后,依次按ASCII码值进行比较,最后将他们按升序输出. [zook ...

  2. 新增时json类型报错

    新增时出错:如下 实体类中字段类型没有对应上,vue页面中修改跳转页面的路径:使用params...

  3. PHP程序员的技术成长规划 第三阶段:高级阶段

    第三阶段:高级阶段 (高级PHP程序员)重点:除了基本的LNMP程序,还能够在某个方向或领域有深入学习.(纵深维度发展)目标:除了能够完成基本的PHP业务开发,还能够解决大部分深入复杂的技术问题,并且 ...

  4. 发现环——第八届蓝桥杯C语言B组(国赛)第四题

    原创 标题:发现环 小明的实验室有N台电脑,编号1~N.原本这N台电脑之间有N-1条数据链接相连,恰好构成一个树形网络.在树形网络上,任意两台 电脑之间有唯一的路径相连. 不过在最近一次维护网络时,管 ...

  5. 20155202 实验四 Android开发基础

    20155202 实验四 Android开发基础 实验内容 1.基于Android Studio开发简单的Android应用并部署测试; 2.了解Android.组件.布局管理器的使用: 3.掌握An ...

  6. 20155308 《Java程序设计》实验五 网络编程与安全

    20155308 <Java程序设计>实验五 网络编程与安全 实验内容 任务一 两人一组结对编程: 参考http://www.cnblogs.com/rocedu/p/6766748.ht ...

  7. C# JSON和对象之间互相转换

    1.首先是声明用户信息对象,DataContract修饰类,表示可以被解析成JSON,DataMember修饰属性,Order表示 解析的顺序,另外Lover是数组列表,表示女朋友个数 Address ...

  8. java开发工具使用

    一.MyEclipse软件的使用 1)ctrl+n新建文件 2)ctrl+d删除一行 3)alt+/提示补齐 (main/syso/syse/for遍历最近的数组) 4)ctrl+shift+f格式化 ...

  9. idea 严重: Error configuring application listener of class org.springframework.web.context.Context 后面省略

    根本原因:jar文件没有同步发布到自己项目的lib目录中 解决方案:把之前在这个位置的jar文件,put into 到 /WEB-INF/lib 目录下即可

  10. 润乾报表整合到Tomcat服务器的部署过程

    转载自:http://www.cnblogs.com/avivaye/archive/2012/11/16/2773681.html 使用第三方的报表设计器/服务器来快速的开发报表. 润乾服务器是使用 ...