博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 5285 wyh2000 and pupil 判二分图+贪心
阅读量:4955 次
发布时间:2019-06-12

本文共 3768 字,大约阅读时间需要 12 分钟。

题目链接:

hdu:

bc:

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.
T10,0n,m100000
 
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
#include
#include
#include
using namespace std;const int maxn = 101010;const int INF = 0x3f3f3f3f;struct Edge { int v, ne; Edge(int v = 0, int ne = 0) :v(v), ne(ne) {};}egs[maxn * 2];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] = 1; if (vis[cur] == 0) cnt0++; else cnt1++; int p = head[cur]; while (p != -1) { 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 != -1) { Edge &e = egs[p]; if (vis[e.v] == -1) { vis[e.v] = vis[cur] ^ 1; dfs(e.v); } else if (vis[e.v] == vis[cur]) { return false; } p = e.ne; } return true;}void init() { tot = 0; memset(head, -1, sizeof(head)); memset(vis, -1, sizeof(vis)); memset(vis2, 0, sizeof(vis2));}int main() { int tc; scanf("%d", &tc); while (tc--) { init(); scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) { int u, v; scanf("%d%d", &u, &v); u--, v--; addEdge(u, v); addEdge(v, u); } int ans = 0; int flag = 0, adj = INF; for (int i = 0; i < n; i++) { if (vis[i] == -1) { vis[i] = 0; if (!dfs(i)) { flag = 1; break; } else { int cnt0 = 0, cnt1 = 0; dfs2(i, cnt0, cnt1); ans += max(cnt0, cnt1); if (abs(cnt0 - cnt1) > 0) { adj = min(adj, abs(cnt0 - cnt1)); } } } } //调整 if (adj == INF) adj = 0; if (n - ans < 1) { ans -= adj; } if (flag || n - ans<1 || ans<1) { printf("Poor wyh\n"); } else { printf("%d %d\n", max(ans, n - ans), min(ans, n - ans)); } } return 0;}
View Code

 

转载于:https://www.cnblogs.com/fenice/p/5339339.html

你可能感兴趣的文章
JS去除数组重复元素
查看>>
[八省联考2018]林克卡特树lct
查看>>
extjs动态改变样式
查看>>
PL/SQL Developer 查询的数据有乱码或者where 字段名=字段值 查不出来数据
查看>>
宏定义
查看>>
Nginx05---负载均衡 upsteam
查看>>
ubuntu12.04 串口登录系统配置
查看>>
poj3061
查看>>
linux--多进程进行文件拷贝
查看>>
笔记:git基本操作
查看>>
#webpack `Invalid Host/Origin header`问题
查看>>
javascript 闭包的学习
查看>>
dns等服务器搭建
查看>>
九度oj 题目1102:最小面积子矩阵
查看>>
php自定义截取中文字符串-utf8版
查看>>
入坑慢慢填
查看>>
Django 源码小剖: 应用程序入口 WSGIHandler
查看>>
bat 命令行方式生成带有日期的MSSQL数据库备份文件
查看>>
Web开发者不容错过的20段CSS代码
查看>>
python中使用selenium错误-Firefox浏览器
查看>>