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在教导他的小学生。
共同拥有个小学生,编号为。为了添加小学生之间的凝聚力,wyh2000决定将全部小学生分成组,每组都至少有个人。
可是有些小学生之间并不认识,并且假设不认识,那么也不认识。

Wyh2000希望每组中的小学生都互相认识。并且第一组的人要尽可能多。
请你帮wyh2000求出第一组和第二组的人数是多少。假设找不到分组方案,则输出"Poor wyh"。

输入描写叙述

第一行一个数,表示数据组数。
对于每组数据,第一行两个数,表示小学生数量和互相不认识的小学生的数量。

接下来行,每行两个数,表示不认识,不认识。

保证一对仅仅会出现一次。

输出描写叙述

对于每组数据,输出答案。

输入例子

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;
}

Statistic | Submit | Clarifications | Back

时间: 2024-08-12 20:41:16

wyh2000 and pupil的相关文章

HDU 5285 wyh2000 and pupil(dfs或种类并查集)

wyh2000 and pupil Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 755    Accepted Submission(s): 251 Problem Description Young theoretical computer scientist wyh2000 is teaching his pupils. Wy

HDU 5285 wyh2000 and pupil (DFS染色判二分图 + 贪心)

wyh2000 and pupil Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 1040    Accepted Submission(s): 331 Problem Description Young theoretical computer scientist wyh2000 is teaching his pupils. W

二分图判定+点染色 BestCoder Round #48 ($) 1002 wyh2000 and pupil

题目传送门 1 /* 2 二分图判定+点染色:因为有很多联通块,要对所有点二分图匹配,若不能,存在点是无法分配的,no 3 每一次二分图匹配时,将点多的集合加大最后第一个集合去 4 注意:n <= 1,no,两个集合都至少有一人:ans == n,扔一个给另一个集合 5 */ 6 #include <cstdio> 7 #include <algorithm> 8 #include <cstring> 9 #include <cmath> 10 #in

HDU 5285 wyh2000 and pupil (二分图)

题意:共有n个小学生,编号为1−n.将所有小学生分成2组,每组都至少有1个人.但是有些小学生之间并不认识,而且如果a不认识b,那么b也不认识a.Wyh2000希望每组中的小学生都互相认识.而且第一组的人要尽可能多.请你帮wyh2000求出第一组和第二组的人数是多少.如果找不到分组方案,则输出"Poor wyh". 思路:二分图着色.给的就是无向图,每次都累加人多的颜色即可.若不能着色,必定不能分成2组.如果全部都是1个颜色,那么要让其中1人过第2组.我勒个去,就因为赭色时颜色号码开小了

hdu 5285 wyh2000 and pupil(二分图判定)

对每两个不认识的人连一条边,则此题可转化为二分图判定(二分图可有多个). 如果有一部分图判定为不是二分图,则输出“Poor wyh”. 否则,分别累加每个二分图的最多的颜色数. #include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <string> #include <stack> #include <cmat

HDU 5285 wyh2000 and pupil

题意:有一群人,已知某两人之间互相不认识,要把这群人分成两部分,每部分至少一人,且在每部分内没有人互不认识. 解法:图染色.某场bestcoder第二题……看完题觉得是个二分图……完全不会二分图什么的……但是为了挣扎一下百度了一下二分图的判定方法,知道了可以用染色法,这样如果是二分图的话将每个连通分量里点数量最多的颜色的点数量(像个绕口令诶)相加就可以了.然而激动万分的我早忘了还有每部分至少一人这个条件……直到我和队友研究怎么hack别人的时候他才告诉我还有这么个条件……(哭)还好来得及…… 代

hdu 5285 wyh2000 and pupil(二染色)

第一次用vector解得题,值得纪念,这道题是二染色问题,我用bfs解得,就是染色,判断,计数问题,其 实挺简单的,就是得判一下特殊情况,当n<2的时候就不能有解,因为题目要求每个组至少有一个人,当没有不认识的 人的时候就是一个组是n-1,另一个组人数为1 上代码: #include<stdio.h> #include<string.h> #include<stdlib.h> #include<algorithm> #include<vector

HDU3285——5285dfs——wyh2000 and pupil

/* 大意:给出m给不认识关系,要把他们分成两组,每组人都要认识 用一个col来记录是否相邻,如果出现奇数环会使得col[v] != 3 - color,不能省 如果不存在边,特判,分一个人过去 */ #include <cstdio> #include <cstring> #include <algorithm> #include <vector> using namespace std; const int MAX = 100000 + 10; vect

hdu 5258 wyh2000 and pupil(dfs)(待续)

题意:n个点,m条边,每条边连接的两点颜色不同, 思路: #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<vector> using namespace std; int t,n,m; vector<int>g[500010]; int vis[500010]; int cnt1,cnt2,sum; int dfs(int