hdu 5285 wyh2000 and pupil
2015-07-20 20:11
369 查看
题意:
官方题解:
分析:
比赛的时候想到了二分图,但是逗逼的用了dfs去染色。。。。。。
因为整个图可能不连通,对于每一个连通分量只需用bfs染成两种不同的颜色,分别用0,1表示,分别求出两种颜色的个数,对于第一个分组取每个连通分量中数量较多的一种颜色累加,第二个分组取数量较少的一种颜色累加。若某个连通分量无法构成二分图,即染色有冲突,那么则可以输出“Poor wyh”。
以下附上代码:
青年理论计算机科学家wyh2000在教导他的小学生。 共有n个小学生,编号为1−n。为了增加小学生之间的凝聚力,wyh2000决定将所有小学生分成2组,每组都至少有1个人。 但是有些小学生之间并不认识,而且如果a不认识b,那么b也不认识a。 Wyh2000希望每组中的小学生都互相认识。而且第一组的人要尽可能多。 请你帮wyh2000求出第一组和第二组的人数是多少。如果找不到分组方案,则输出"Poor wyh"。
官方题解:
如果a不认识b,那么在a,b间连一条边,这样有解当且仅当这张图是二分图。 由于可能有多个二分图,而题目要求第一组的人尽可能多,所以贪心的选择即可。 要注意m=0的情况。
分析:
比赛的时候想到了二分图,但是逗逼的用了dfs去染色。。。。。。
因为整个图可能不连通,对于每一个连通分量只需用bfs染成两种不同的颜色,分别用0,1表示,分别求出两种颜色的个数,对于第一个分组取每个连通分量中数量较多的一种颜色累加,第二个分组取数量较少的一种颜色累加。若某个连通分量无法构成二分图,即染色有冲突,那么则可以输出“Poor wyh”。
以下附上代码:
#include <algorithm> #include <iostream> #include <sstream> #include <fstream> #include <cstring> #include <cstdio> #include <vector> #include <cctype> #include <cmath> #include <stack> #include <queue> #include <list> #include <map> #include <set> using namespace std; typedef long long ll; typedef unsigned long long ull; const int maxn = 100005; int n,m; vector<int> adlist[maxn]; bool color[maxn]; int vis[maxn]; int t1,t2; bool fg; void bfs(int s) { int u,v; queue<int> q; q.push(s); while(!q.empty()){ u = q.front(); q.pop(); if(color[u] == 1) t1++; else t2++; for(int i = 0; i < adlist[u].size(); i++){ v = adlist[u][i]; if(!vis[v]){ color[v] = !color[u]; vis[v] = 1; q.push(v); } else if(color[u] == color[v]){ fg = false; return; } } } } int main() { int t; scanf("%d",&t); while(t--){ scanf("%d%d",&n,&m); if(n < 2){ printf("Poor wyh\n"); continue; } if(m == 0){ printf("%d %d\n",n-1,1); continue; } for(int i = 0; i <= n; i++) adlist[i].clear(); fill(vis,vis+n+1,0); int u,v; fg = true; for(int i = 0; i < m; i++){ scanf("%d%d",&u,&v); if(!fg) continue; adlist[u].push_back(v); adlist[v].push_back(u); } int maximum = 0; int minimum = 0; for(int i = 1; i <= n; i++){ if(vis[i] == 0) { vis[i] = 1; t1 = 0,t2 = 0; bfs(i); if(!fg) break; maximum += max(t1,t2); minimum += min(t1,t2); } } if(fg){ printf("%d %d\n",maximum,minimum); } else{ printf("Poor wyh\n"); } } return 0; }
相关文章推荐
- ImageLoader配置(注释)
- Android onTouch事件与手势操作
- 欧拉函数简单应用
- 12天学好C语言——记录我的C语言学习之路(Day 5)
- Android系统启动过程(转载)
- 利用基于@AspectJ的AOP实现权限控制
- 12天学好C语言——记录我的C语言学习之路(Day 5)
- 用户与操作系统的接口是什么?一个分时系统提供什么接口?一个批处理系统又提供什么接口?
- 两个栈实现一个队列
- igrimace最新安装源地址apt.so/igrimace3
- java之设计模式---单例模式
- Light 1037 - Agent 47 (状态压缩dp)
- universal-image-loader图片异步加载
- erlang进程监控的实现原理
- 【CODEFORCES】 D. CGCDSSQ
- 【西祠日志】【04】还不错,今天asp基本功能完成了(2015.07.20周一)(第二周)
- arcgis 9.3 卸载 安装10.1
- [leetcode] Palindrome Number(不使用额外空间)
- nagios监控远程端口
- 点击按钮切换背景