Bestcoders,bestcoderhdu

來源:互聯網
上載者:User

Bestcoders,bestcoderhdu
wyh2000 and pupil  Accepts: 93  Submissions: 925 Time Limit: 3000/1500 MS (Java/Others)  Memory Limit: 131072/65536 K (Java/Others)問題描述

青年理論電腦科學家wyh2000在教導他的小學生。共有n個小學生,編號為1−n。為了增加小學生之間的凝聚力,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)只會出現一次。T≤10,0≤n,m≤100000
輸出描述
對於每組資料,輸出答案。
輸入範例
28 53 45 61 25 83 55 42 34 53 42 4
輸出範例
5 3Poor wyh

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define maxn 100000+10int T, n, m, cnt;int tot, head[maxn], vis[maxn], a[3];struct Edge{    int v, next;}e[maxn<<1];void init(){    tot = cnt = 0;    memset(head, -1,  (n+2)*sizeof(int));    memset(vis, 0, (n+2)*sizeof(int));}void add(int u, int v){    e[tot].v = v; e[tot].next = head[u]; head[u] = tot++;}bool dfs(int u, int color){    vis[u] = color;    a[color+1]++;    for(int i=head[u]; i!=-1; i=e[i].next)    {        int v = e[i].v;        if(vis[v] == color) return false;        if(!vis[v] && !dfs(v, -color)) return false;    }    return true;}int main(){    scanf("%d", &T);    while(T--)    {        scanf("%d%d", &n, &m);        init();        int u, v;        for(int i=0; i<m; i++)        {            scanf("%d%d", &u, &v);            add(u, v);            add(v, u);        }        if(n<2)        {            printf("Poor wyh\n");            continue;        }        int flag = 0;        int ans = 0;        for(int i=1; i<=n; i++)            if(!vis[i])        {            a[0] = a[2] = 0;            if(!dfs(i, 1))                {                    flag = 1;                    break;                }            ans += max(a[0], a[2]);        }        if(flag) printf("Poor wyh\n");        else        {            if(ans == n)                ans -= 1;            printf("%d %d\n", ans, n-ans);        }    }    return 0;}

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.