I'm Telling the Truth
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2006 Accepted Submission(s): 1011
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).
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.
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)
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
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
2 3 4
5
1 3 5 6 7
Source
题意:有n个学生,老师想要知道他们的排名,但是学生不想说实话,所以他们都只告诉老师自己的排名所在的范围,但是有些学生说的范围明显与与另一些学生冲突,但是老师还是相信绝大部分学生没说谎,现在问最多有多少学生没说谎,并按照字典序最大输出这些学生编号.
题解:先对每个学生和其所在排名范围的每个名次连一条边,然后从n->1开始做二分图匹配,这样就能够得到最大的结果并且得到的字典序也是最大的。
#include<iostream>
#include<cstdio>
#include<cstring>
#include <algorithm>
#include <math.h>
#include <queue>
using namespace std;
const int INF = ;
const int N = ;
const int M = ;
int graph[N][M];
struct Rank{
int l,r;
}r[N];
int ans[N];
int n;
int linker[M];
bool vis[M];
bool dfs(int u){
for(int v = r[u].l;v<=r[u].r;v++){
if(!vis[v]&&graph[u][v]){
vis[v] = true;
if(linker[v]==-||dfs(linker[v])){
linker[v] = u;
return true;
}
}
}
return false;
}
int main()
{
int tcase;
scanf("%d",&tcase);
while(tcase--){
memset(graph,,sizeof(graph));
scanf("%d",&n);
for(int i=;i<=n;i++){
int a,b;
scanf("%d%d",&a,&b);
r[i].l = a,r[i].r = b;
for(int j=a;j<=b;j++){
graph[i][j] = ;
}
}
memset(linker,-,sizeof(linker));
int id = ,res=;
for(int i=n;i>=;i--){
memset(vis,false,sizeof(vis));
if(dfs(i)){
ans[id++] = i;
res++;
}
}
printf("%d\n",res);
for(int i=id-;i>=;i--){
printf("%d ",ans[i]);
}
printf("%d\n",ans[]);
}
return ;
}