HDU5285wyh2000 and pupil

wyh2000 and pupil

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

 

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

刚接触到二分图,看了别人的题解才搞定。

比较好的题解

求几个独立的二分图(可能有单点不过不碍事)合成一个大二分图,使得大二分图两侧点数差的绝对值最大,显然这里用贪心思想,对每个二分图,我把点数大的那一侧的全部并起来作为大二分图的一侧,另一侧用n减即可,判断二分图和记录点数采用的是DFS染色法,注意几个wa点,n<2时肯定没法分,还有m等于0时,显然可以有n个平凡图,这种特殊情况下,因为每组至少有一个人,直接n-1和1即可

//非连通图 
#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#define max(a,b) a>b?a:b
#define N 100010
using namespace std;
vector<int>G[N];
int n,m;
int color[N];
int num[2];//num[0]记录被染色为-1,num[1]记录被染色为1的点数 
void init()
{
    memset(color,0,sizeof(color));
    for(int i=0;i<=n;i++)
    {
        G[i].clear();
    }
}

bool dfs(int pos,int cnt)
{
    color[pos]=cnt;
    if(cnt==1)
    num[1]++;
    if(cnt==-1)
    num[0]++;
    for(int i=0;i<G[pos].size();i++)
    {
        if(color[G[pos][i]]==cnt)
        {
            return false;
        }
        if(color[G[pos][i]]==0&&!dfs(G[pos][i],-cnt))
        return false;
    }
    return true;
}

int main()
{
    int t,x,y;
    //freopen("in.txt","r",stdin);
    scanf("%d",&t);
    while(t--)
    {
    	scanf("%d%d",&n,&m);
        init();
        for(int i=0;i<m;i++)
        {
        	scanf("%d%d",&x,&y);
            G[x].push_back(y);
            G[y].push_back(x);
        }
        if(n<=1)//无法二分 
        {
        	printf("Poor wyh\n");
        	continue;
		}
		if(m==0)
		{
			printf("%d 1\n",n-1); 
			continue;
		}
        int ans=0;
        int flag=1;
        for(int i=1;i<=n;i++)
        {
            if(color[i]==0)
            {
            	num[1]=num[0]=0;
                if(!dfs(i,1))
                {
                    flag=0;
                    break;
                }
                ans+=max(num[0],num[1]);
            }
        }
        if(flag)
        printf("%d %d\n",ans,n-ans);
        else
        printf("Poor wyh\n");
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值