天天看点

HDU 5285 wyh2000 and pupil

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

For each case, the first line contains two integers n,m

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)

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

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<string>
using namespace std;
const int maxn = 100005;
int T, n, m, x, y, sum, c[maxn], fa[maxn], dis[maxn], cnt[maxn][2];
bool flag;

int find(int x)
{
    if (x != fa[x]){
        int t = find(fa[x]);
        dis[x] = (dis[x] + dis[fa[x]]) & 1;
        fa[x] = t;
    }
    return fa[x];
}

void merge(int x, int y)
{
    int fx = find(x), fy = find(y);
    if (fx == fy)
    {
        if (dis[x] == dis[y]) flag = 0;
        return;
    }
    fa[fx] = fy;
    dis[fx] = (1 + dis[x] + dis[y]) & 1;
}

int main()
{
    cin >> T;
    while (T--)
    {
        cin >> n >> m;
        memset(dis, 0, sizeof(dis));
        memset(cnt, 0, sizeof(cnt));
        flag = true;
        for (int i = 1; i <= n; i++) fa[i] = i;
        for (int i = 1; i <= m; i++)
        {
            scanf("%d%d", &x, &y);
            merge(x, y);
        }
        sum = 0;    
        for (int i = 1; i <= n&&flag; i++) cnt[find(i)][dis[i]]++;
        for (int i = 1; i <= n&&flag; i++) sum += max(cnt[i][0], cnt[i][1]);
        if (flag&&n > 1)
        {
            if (sum == n) sum = n - 1;
            printf("%d %d\n", sum, n - sum);
        }
        else printf("Poor wyh\n");
    }
    return 0;
}