zoukankan      html  css  js  c++  java
  • 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
    
    ", ans1, ans2);
                         }
                 }
                 if(flag == 1 || n <= 1) printf("Poor wyh
    ");
                 else {
                 if(m == 0)  ans1--,ans2++;
                 printf("%d %d
    ", ans1, ans2);
                 }
         }
         return 0;
    }
                         
    

      

  • 相关阅读:
    【转】免费搭建独立博客,WordPress+独立域名+独立空间
    MongoDB索引
    面向对象
    浏览器为webapp
    转载Chrome浏览器IOS
    浅淡Webservice、WSDL三种服务访问的方式(附案例)
    Plupload
    转载Yale CAS + .net Client 实现 SSO(6)
    java 对Hbase的基本操作
    【精】iOS开发视频教程下载
  • 原文地址:https://www.cnblogs.com/zero-begin/p/4662572.html
Copyright © 2011-2022 走看看