专栏首页ACM算法日常I'm Telling the Truth(二分图)- HDU 3729

I'm Telling the Truth(二分图)- HDU 3729

二分图:设G=(V,E)是一个无向图,如果顶点V可分割为两个互不相交的子集(A,B),并且图中的每条边(i,j)所关联的两个顶点i和j分别属于这两个不同的顶点集(i in A,j in B),则称图G为一个二分图

简单的说,一个图被分成了两部分,相同的部分没有边,那这个图就是二分图,二分图是特殊的图。

Problem Description

After this year’s college-entrance exam, the teacher did a survey in his class on students’ score. There are n students in the class. The students didn’t want to tell their teacher their exact score; they only told their teacher their rank in the province (in the form of intervals).

这学期的考试完了,老师在班上做了一个小调查:班上有n个学生,但是学生不想告诉老师自己考了多少分,他们只是告诉老师他们的成绩所在的区间。

After asking all the students, the teacher found that some students didn’t tell the truth. For example, Student1 said he was between 5004th and 5005th, Student2 said he was between 5005th and 5006th, Student3 said he was between 5004th and 5006th, Student4 said he was between 5004th and 5006th, too. This situation is obviously impossible. So at least one told a lie. Because the teacher thinks most of his students are honest, he wants to know how many students told the truth at most.

当问完所有的学生,老师发现有些学生撒谎了。比如,学生1号说他在5004到5005这两名中,学生2说他在5005到5006这两名中,学生3说他在5004到5006这3名中,学生4也说他在5004-5006。显然这种情况有问题,至少有一个学生撒谎了。老师认为大部分学生还是诚实的,所以他想知道有多少个学生说的是真话。

Input

There is an integer in the first line, represents the number of cases (at most 100 cases). In the first line of every case, an integer n (n <= 60) represents the number of students. In the next n lines of every case, there are 2 numbers in each line, Xi and Yi (1 <= Xi <= Yi <= 100000), means the i-th student’s rank is between Xi and Yi, inclusive.

第一行输入一个数,表示用例个数。

每个用例的第一行是学生n的个数,下面n行表示每个学生说的区间。

Output

Output 2 lines for every case. Output a single number in the first line, which means the number of students who told the truth at most. In the second line, output the students who tell the truth, separated by a space. Please note that there are no spaces at the head or tail of each line. If there are more than one way, output the list with maximum lexicographic. (In the example above, 1 2 3;1 2 4;1 3 4;2 3 4 are all OK, and 2 3 4 with maximum lexicographic)

每个用例输出2行,第一行输出最有有多少个学生说真话。

第二行输出说真话的学生,空格隔开。注意开头和结尾没有空格。如果有多个结果,输出词法上最大的那组。

Sample Input

2

4

5004 5005

5005 5006

5004 5006

5004 5006

7

4 5

2 3

1 2

2 2

4 4

2 3

3 4

Sample Output

3

2 3 4

5

1 3 5 6 7

解题思路:将每个学生报的名次区间作为二分图的左边,右边为所有名次,如果左边的区间顶点对应右边一个顶点,则表示该名次被占用。

解题虽然是二分图的思想,但是实现代码使用了数组链表,性能很好,能够做到0AC。具体参考源代码。

解题方法精要:从最后一个同学开始遍历,每次寻找空位,找到就返回。如果没有找到,A同学就mark标记一下表示这个位置X正在查找,然后深度搜索其他同学是否能够找到别的位置Y,如果同学B能找到,则B使用另外一个位置Y,那么A同学就使用这个位置X。

源码:G++ 0ms HDU排名19

#include<stdio.h>
#include<string.h>
#include<queue>
#include<iostream>

using namespace std;

typedef struct rank_s {
    int value;
    int prev;
} rank_t;

//数组-链表节点
rank_t ranks[100001];

int student_ids[100001], mark[100001], list_index[100], matches[100];

int find(int student_id, queue<int> & values)
{
    //遍历链表
    for (int i = list_index[student_id]; i != -1; i = ranks[i].prev)
    {
        //获得链表元素,value是排名
        int value = ranks[i].value;

        //排名是否被占用
        if (mark[value] == 0)
        {
            //标记占用
            mark[value] = 1;
            values.push(value);

            //排名指向的学生id
            if (student_ids[value] == 0)
            {
                student_ids[value] = student_id;
                return 1;
            }
            else {
                //深度搜索占用排名的学生是否能够找到别的排名
                if (find(student_ids[value], values) == 1)
                {
                    //如果能找到,则自己占用这个排名
                    student_ids[value] = student_id;
                    return 1;
                }
            }
        }
    }
    return 0;
}

int main()
{
    int test_count, student_count, sum;
    queue<int> values;

    //用例个数
    scanf("%d", &test_count);

    while (test_count--)
    {
        while (values.size()) {
            values.pop();
        }

        int current = 0;

        //输入学生数量
        scanf("%d", &student_count);

        memset(student_ids, 0, sizeof(student_ids));
        memset(list_index, -1, sizeof(list_index));
        memset(matches, -1, sizeof(matches));

        //输入学生说的区间
        for (int i = 1; i <= student_count; i++)
        {
            int a, b;
            scanf("%d%d", &a, &b);
            for (int j = a; j <= b; j++)
            {
                //数组链表
                ranks[current].value = j;
                ranks[current].prev = list_index[i];
                list_index[i] = current++;
            }
        }

        sum = 0;

        //从最后一个学生开始遍历
        for (int i = student_count; i >= 1; i--)
        {
            if (find(i, values) == 1) {
                //找到
                sum++;
                //记录匹配的学生id
                matches[i] = 1;
            }

            //清空标记
            while (values.size())
            {
                mark[values.front()] = 0;
                values.pop();
            }
        }

        printf("%d\n", sum);

        for (int i = 1; i <= student_count; i++)
        {
            if (matches[i] == 1)
            {
                printf("%d%s", i, sum == 1 ? "\n" : " ");
                sum--;
            }
        }
    }
}

本文分享自微信公众号 - ACM算法日常(acm-clan),作者:dansen

原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。

原始发表时间:2018-04-28

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 搜索专题2 | 3D地宫寻路 POJ - 2251

    上一篇我们做了一道棋子摆放的题目,采用的是DFS算法,本篇是一篇BFS算法,在刚开始学习搜索算法的时候,会觉得DFS和BFS算法非常相似,因为都是搜索然后得到结...

    ACM算法日常
  • 独角兽与数列(置换群循环)- HDU 4985

    群论是法国数学家伽罗瓦(Galois)的发明。伽罗瓦是一个极具传奇性的人物,年仅21岁就英年早逝于一场近乎自杀的决斗中。他用该理论,具体来说是伽罗瓦群,解决了五...

    ACM算法日常
  • 搜索专题4 | 旋转棋盘 POJ - 2286

    The rotation game uses a # shaped board, which can hold 24 pieces of square bloc...

    ACM算法日常
  • hduoj-----(1068)Girls and Boys(二分匹配)

    Girls and Boys Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/3...

    Gxjun
  • Code Forces 21C Stripe 2

    C. Stripe 2 time limit per test 1 second memory limit per test 64 megabyte...

    ShenduCC
  • POJ-1143(状态压缩)

    Number Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: ...

    ShenduCC
  • Java 实现十进制数转换为二进制

    一个会写诗的程序员
  • POJ 3694 Network(Tarjan求割边+LCA)

    Description A network administrator manages a large network. The network consist...

    attack
  • HDUOJ----(1031)Design T-Shirt

    Design T-Shirt Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/327...

    Gxjun
  • 多语种机器翻译: 缩小共享和特定语言编码解码器之间的差距(CS CL)

    最先进的多语种机器翻译依赖于通用的编码解码器,这需要重新训练整个系统来添加新的语言。 在本文中,我们提出了一种基于特定语言编码解码器的替代方法,从而可以更容易地...

    用户7095611

扫码关注云+社区

领取腾讯云代金券