zoukankan      html  css  js  c++  java
  • 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.
    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 indicates the number of test cases.
    For each case, the first line contains two integers n,m indicate the number of pupil and the number of pupils don't konw each other.
    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) will only appear once.
    T10,0n,m100000
     
    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
     
    Source
     
    题目链接:http://acm.hdu.edu.cn/showproblem.php?

    pid=5285

    题目大意:一些人要分成两组,(这群人真的好累啊,一天到晚被分来分去),给出他们之间的不认识关系。两个不认识的人不能分到一组,且每组都要至少有一个人,问分完后两组人数的最大可能值和最小可能值

    题目分析:题意化简一下,就是求几个独立的二分图(可能有单点只是不碍事)合成一个大二分图。使得大二分图两側点数差的绝对值最大,显然这里用贪心思想,对每一个二分图,我把点数大的那一側的所有并起来作为大二分图的一側,还有一側用n减就可以,推断二分图和记录点数採用的是DFS染色法。注意几个wa点,n<2时肯定没法分,还有m等于0时,显然能够有n个平庸图。这样的特殊情况下。由于每组至少有一个人,直接n-1和1就可以

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    int const MAX = 100005;
    int n, m, cnt;
    int head[MAX], color[MAX], num[2];
    bool vis[MAX], flag;
    
    struct EDGE
    {
        int v, next;
    }e[MAX * 2]; 
    
    void Add(int u, int v)
    {
        e[cnt].v = v;
        e[cnt].next = head[u];
        head[u] = cnt ++;
    }
    
    void DFS(int u, int col)
    {
        vis[u] = true;
        color[u] = col;
        num[col] ++;
        for(int i = head[u]; i != -1; i = e[i].next)
        {
            int v = e[i].v;
            if(vis[v])
            {
                if(color[v] == color[u])
                {
                    flag = true;
                    return;
                }
            }
            else
                DFS(v, col ^ 1);
        }
        return;
    }
    
    int main()
    {
        int T;
        scanf("%d", &T);
        while(T --)
        {
            memset(head, -1, sizeof(head));
            memset(color, -1, sizeof(color));
            memset(vis, false, sizeof(vis));
            cnt = 0;
            flag = false;
            scanf("%d %d", &n, &m);
            for(int i = 0; i < m; i++)
            {
                int u, v;
                scanf("%d %d", &u, &v);
                Add(u, v);
                Add(v, u);
            }
            if(n < 2)
            {
                printf("Poor wyh
    ");
                continue;
            }
            if(m == 0)
            {
                printf("%d 1
    ", n - 1);
                continue;
            }
            int ans = 0;
            for(int i = 1; i <= n; i++)
            {
                if(!vis[i])
                {
                    memset(num, 0, sizeof(num));
                    DFS(i, 0);
                    if(flag)
                        break;
                    ans += max(num[0], num[1]);
                }
            }
            if(flag)
                printf("Poor wyh
    ");
            else
                printf("%d %d
    ", ans, n - ans);
        }
    }



  • 相关阅读:
    深入了解JVMzz
    正则表达式和Java编程语言1zz
    全世界所有程序员都会犯的错误zz
    C++完美实现Singleton模式zz
    Visual C++6.0 API函数操作技巧集zz光标和鼠标操作
    用next_permutation()生成r组合数,兼VC7的一个bugzz
    基于逆向最大化词表中文分词法zz
    c#.net常用函数列表
    Windows多线程多任务设计初步zz
    在Linux中实现内部进程通信
  • 原文地址:https://www.cnblogs.com/wgwyanfs/p/6945914.html
Copyright © 2011-2022 走看看