HDU 5285:wyh2000 and pupil

wyh2000 and pupil

 
 Accepts: 93
 
 Submissions: 925
 Time Limit: 3000/1500 MS (Java/Others)
 
 Memory Limit: 131072/65536 K (Java/Others)
问题描述
青年理论计算机科学家wyh2000在教导他的小学生。
共有n个小学生,编号为1n。为了增加小学生之间的凝聚力,wyh2000决定将所有小学生分成2组,每组都至少有1个人。
但是有些小学生之间并不认识,而且如果a不认识b,那么b也不认识a。
Wyh2000希望每组中的小学生都互相认识。而且第一组的人要尽可能多。
请你帮wyh2000求出第一组和第二组的人数是多少。如果找不到分组方案,则输出"Poor wyh"。
输入描述
第一行一个数T,表示数据组数。
对于每组数据,第一行两个数n,m,表示小学生数量和互相不认识的小学生的数量。
接下来m行,每行两个数x,y(x<y),表示x不认识yy不认识x。保证一对(x,y)只会出现一次。
T10,0n,m100000
输出描述
对于每组数据,输出答案。
输入样例
2
8 5
3 4
5 6
1 2
5 8
3 5
5 4
2 3
4 5
3 4
2 4
输出样例
5 3
Poor wyh


题解思路已经给出了:

如果a不认识b,那么在a,b间连一条边,这样有解当且仅当这张图是二分图。
由于可能有多个二分图,而题目要求第一组的人尽可能多,所以贪心的选择即可。
要注意m=0的情况。
自己做这道题的时候只顾着“贪“了,每组至少1个人啊。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <vector>
using namespace std;

vector <int> segment[100005];
int color[100005];
int Test,num,seg,i,flag,temp1,temp2,color0,color1,color2;

void tu(int hao,int tu_color)
{
	if((tu_color==0&&color[hao])||flag==0)
		return;
	
	int size = segment[hao].size();

	if(tu_color==1)
	{
		color[hao]=1;
		color1++;
	}
	else if(tu_color==2)
	{
		color[hao]=2;
		color2++;
	}
	else if(size)
	{
		color[hao]=1;
		color1++;
	}

	
	if(color[hao]==1)
	{
		int wang;
		for(wang=0;wang<size;wang++)
	    {
			if(color[segment[hao][wang]]==0 )
			{
				tu(segment[hao][wang],2);
			}
			else if(color[segment[hao][wang]]==1)
			{
				flag=0;
			}
	    }
	}
	else if(color[hao]==2)
	{
		int chong;
		for(chong=0;chong<size;chong++)
	    {
		    if(color[segment[hao][chong]]==0 )
			{
				tu(segment[hao][chong],1);
			}
			else if(color[segment[hao][chong]]==2)
			{
				flag=0;
			}
	    }
	}

}

void cal()
{
	for(i=1;i<=num;i++)
	{
		if(color[i]==0)
			color0++;
	}
}

int main()
{
	//freopen("i.txt","r",stdin);
	//freopen("o.txt","w",stdout);
	
	cin>>Test;

	while(Test--)
	{
		scanf_s("%d %d",&num,&seg);
		
		for(i=1;i<=100004;i++) segment[i].clear();
		memset(color,0,sizeof(color));

		for(i=1;i<=seg;i++)
		{
			scanf_s("%d %d",&temp1,&temp2);
			segment[temp1].push_back(temp2);
			segment[temp2].push_back(temp1);
		}
		if(num<= 1) {
            puts("Poor wyh");
            continue;
        }
		flag=1;
		int max_c=0,min_c=0,xun;
		for(xun=1;xun<=num;xun++)
		{
			color1=0;color2=0;
			tu(xun,0);
			max_c +=max(color1,color2);
			min_c +=min(color1,color2); 
		}
		if(flag)
		{
			color0=0;
			cal();
			if(min_c==0)
				cout<<num-1<<" "<<1<<endl;
			else
			    cout<<color0+max_c<<" "<<min_c<<endl;
		}
		else
		{
            puts("Poor wyh");
		}

	}

	return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

posted on 2015-07-20 19:06  光速小子  阅读(153)  评论(0编辑  收藏  举报

导航