前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >I'm Telling the Truth(二分图)- HDU 3729

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

作者头像
ACM算法日常
发布2018-08-07 18:15:22
2930
发布2018-08-07 18:15:22
举报
文章被收录于专栏:ACM算法日常ACM算法日常

二分图:设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

代码语言:javascript
复制
#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--;
            }
        }
    }
}
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-04-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 ACM算法日常 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档