# I'm Telling the Truth（二分图）- HDU 3729

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).

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.

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.

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)

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

```#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--;
}
}
}
}```

0 条评论

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

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

• ### 独角兽与数列（置换群循环）- HDU 4985

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

• ### 搜索专题4 | 旋转棋盘 POJ - 2286

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

• ### hduoj-----(1068)Girls and Boys(二分匹配)

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

• ### Code Forces 21C Stripe 2

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

• ### POJ-1143(状态压缩)

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

• ### POJ 3694 Network(Tarjan求割边+LCA)

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

• ### HDUOJ----（1031）Design T-Shirt

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

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

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