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;

vector <int> G[MAX];

int num[3];

int col[MAX];

bool dfs(int u, int color)

{

        num[color]++;

        col[u] = color;

        for(int i = 0; i < G[u].size(); i++){

                int v = G[u][i];

                if(col[v]){

                        if(col[v] != 3 - color)

                                return false;

                }

                if(!col[v]){

                        if(!dfs(v, 3 - color))

                                return false;

                }

        }

        return true;

}



int main()

{

     int T, a, b;

     int n, m;

     scanf("%d", &T);

     while(T--){

             scanf("%d%d", &n, &m);

             for(int i = 1; i <= n ;i++)

                     G[i].clear();

             memset(col, 0, sizeof(col));

             for(int i = 1; i <= m; i++){

                     scanf("%d%d", &a, &b);

                     G[a].push_back(b);

                     G[b].push_back(a);

             }

             int ans1 = 0, ans2 = 0;

             int flag = 0;

             for(int i = 1; i <= n; i++){

                     if(!col[i]){

                             num[1] = num[2] = 0;

                            if(!dfs(i, 1)){

                                     flag = 1;

                                     break;

                             }

                                     ans1 += max(num[1], num[2]);

                                     ans2 += min(num[1], num[2]);

                                    // printf("%d %d\n\n", ans1, ans2);

                     }

             }

             if(flag == 1 || n <= 1) printf("Poor wyh\n");

             else {

             if(m == 0)  ans1--,ans2++;

             printf("%d %d\n", ans1, ans2);

             }

     }

     return 0;

}

                     

  

你可能感兴趣的:(HDU)