您的位置:首页 > 其它

hdu 5285 wyh2000 and pupil

2015-07-20 20:11 369 查看
题意:

青年理论计算机科学家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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: