题目链接:传送门
I'm Telling the Truth
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2305 Accepted Submission(s): 1147
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.
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, X
i and Y
i (1 <= X
i <= Y
i <= 100000), means the i-th student’s rank is between X
i and Y
i, 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 <iostream>
#include <cstring>
#include <stdio.h>
#include <map>
#include <queue>
using namespace std;
#define size1 1000060
#define size2 505
#define size3 100010
struct Edge
{
int node;
Edge*next;
}m_edge[size1];
int girl[size3];
Edge*head[size2];
int Flag[size3];
int Ecnt,cnt;
void init()
{
Ecnt = cnt = 0;
fill( girl , girl+size3 , 0 );
fill( head , head+size2 , (Edge*)0 );
}
//b能和g匹配
void mkEdge( int b , int g )
{
m_edge[Ecnt].node = g;
m_edge[Ecnt].next = head[b];
head[b] = m_edge+Ecnt++;
}
bool find( int x )
{
for( Edge*p = head[x] ; p ; p = p->next ){
int s = p->node; //有好感的女生
if( !Flag[s] ){
Flag[s] = true; //该女生在本轮匹配中被访问
if( girl[s] == 0 || find(girl[s]) ){
//女生没有对象或者另外一个男生能把这个妹子让给x男
girl[s] = x;
return true;
}
}
}
return false;
}
//构建二分图
void Build( int n )
{
int a,b;
for( int i = 1 ; i <= n ; ++i ){
scanf("%d%d",&a,&b);
for( int j = a ; j <= b ; ++j ){
mkEdge(i,j);
}
}
}
int rec[size2];
void solve( int n )
{
for( int i = n ; i >= 1 ; --i ){
fill( Flag , Flag+size3 , 0 );
if( find(i) ){
rec[cnt++] = i;
}
}
}
int main()
{
int T;
scanf("%d",&T);
while( T-- ){
init();
int n;
scanf("%d",&n);
Build(n);
solve(n);
printf("%d\n",cnt);
printf("%d",rec[cnt-1]);
for( int i = cnt-2 ; i >= 0 ; --i ){
printf(" %d",rec[i]);
}
printf("\n");
}
return 0;
}