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
   
   不认识
   
   
    
    y
   
   
   
   
    
    y
   
   不认识
   
   
    
    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
/*
Author: 2486
Memory: 7448 KB		Time: 592 MS
Language: G++		Result: Accepted
VJ RunId: 4055769		Real RunId: 14058285
Public:		No Yes
*/
/*
如果a不认识b,那么在a,b间连一条边,这样有解当且仅当这张图是二分图。
由于可能有多个二分图,而题目要求第一组的人尽可能多,所以贪心的选择即可。
要注意m=0的情况。
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=100000+5;
vector<int>G[maxn];
int col[maxn],T;
int n,m,a,b,acnt,bcnt;
void init(int x) {
    for(int i=1; i<=x; i++) {
        G[i].clear();
    }
}
bool bfs(int u) {
    queue<int>k;
    k.push(u);
    col[u]=1;
    while(!k.empty()) {
        int s=k.front();
        if(col[s]==1)acnt++;
        else bcnt++;
        k.pop();
        for(int i=0; i<G[s].size(); i++) {
            if(col[G[s][i]]==-1) {
                col[G[s][i]]=!col[s];
                k.push(G[s][i]);
                continue;
            }
            if(col[G[s][i]]==col[s])return false;
        }
    }
    return true;
}
void slove() {
    memset(col,-1,sizeof(col));
    bool flag=false;
    int Max=0;
    for(int i=1; i<=n; i++) {
        acnt=0,bcnt=0;
        if(col[i]==-1&&!bfs(i)) {
            flag=true;
            break;
        }
        Max+=max(acnt,bcnt);//必须这么做,因为这里面为1的或者为0的不一定就是同一阵营。
    }
    if(flag)printf("Poor wyh\n");
    else printf("%d %d\n",Max,n-Max);
}
int main() {
    scanf("%d",&T);
    while(T--) {
        scanf("%d%d",&n,&m);
        init(n);
        for(int i=0; i<m; i++) {
            scanf("%d%d",&a,&b);
            G[a].push_back(b);
            G[b].push_back(a);
        }
        if(n<2) {//题目要求
            printf("Poor wyh\n");
            continue;
        }
        if(m==0) {//题目要求
            printf("%d 1\n",n-1);
            continue;
        }
        slove();
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值