HDU 5285 wyh2000 and pupil

66 篇文章 0 订阅
6 篇文章 0 订阅

用染色法判断一下就好,不认识的人不同的颜色即不同分组,人数最多的一组在前

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long LL;

int edgenum;
int n,m;
int flag;
int color[100010];
int colornum[2];
int head[100010];
int vis[100010];

struct data
{
    int to,next;
} edge[100010*2];

void add(int a,int b)
{
    edge[edgenum].to=b;
    edge[edgenum].next=head[a];
    head[a]=edgenum++;
}

void dfs(int u, int c)
{
    colornum[c]++;
    color[u]=c;
    vis[u]=1;
    for(int i=head[u]; ~i; i=edge[i].next)
    {
        int v=edge[i].to;
        if(vis[v])
        {
            if(color[v]==c)
            {
                flag=0;
                return;
            }
        }
        else
            dfs(v, !c);
    }
}

int main()
{
    int t;
    int x,y;
    int ans1,ans2;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        ans1=ans2=0;
        memset(head,-1,sizeof(head));
        memset(color,-1,sizeof(color));
        memset(vis,0,sizeof(vis));
        for(int i=1; i<=m; i++)
        {
            scanf("%d%d",&x,&y);
            add(x,y);
            add(y,x);
        }
        if(m==0)
        {
            if(n==0||n==1)
            {
                printf("Poor wyh\n");
            }
            else
            {
                printf("%d 1\n",n-1);
            }
            continue;
        }
        edgenum=0;
        flag=1;
        for(int i=1; i<=n; i++)
        {
            if(vis[i])
                continue;
            memset(colornum,0,sizeof(colornum));
            dfs(i,0);
            if(colornum[0]<colornum[1]) swap(colornum[0],colornum[1]);
            ans1+=colornum[0];
            ans2+=colornum[1];
        }
        if(flag)
        {
            printf("%d %d\n",ans1,ans2);
        }
        else
        {
            printf("Poor wyh\n");
        }
    }
    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值