hdu 3729 I'm Telling the Truth(最大匹配)

来源:互联网 发布:网络暴力的危害 编辑:程序博客网 时间:2024/06/05 00:07

I'm Telling the Truth

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 982    Accepted Submission(s): 491


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
245004 50055005 50065004 50065004 500674 52 31 22 24 42 33 4
 

Sample Output
32 3 451 3 5 6 7
题意:有n个学生,每个学生给出他的成绩所在的区间。输出最多有多少个学生说真话,并且按字典序输出说真话的学生编号,若有多种情况,则输出字典序最大的。
思路:二分图的最大匹配。将学生与成绩进行匹配。由于要求输出字典序最大的序列,我们可以从学生n开始匹配,这样就会使学生编号大的先匹配。
AC代码:
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <queue>#include <vector>#include <cmath>#include <stack>#include <cstdlib>using namespace std;const int INF=1000000000;const int maxn=100005;struct node{    int x,y;}score[65];int match1[maxn],match2[maxn];bool vis[maxn];int n;bool find(int x){    for(int i=score[x].x;i<=score[x].y;i++)    if(!vis[i])    {        vis[i]=true;        if(match1[i]==-1||find(match1[i]))        {            match1[i]=x;            match2[x]=i;            return true;        }    }    return false;}int MMG(){    int ans=0;    memset(match1,-1,sizeof(match1));    memset(match2,-1,sizeof(match2));    for(int i=n;i>=1;i--)    {        memset(vis,false,sizeof(vis));        if(find(i)) ans++;    }    return ans;}int main(){   int t;   scanf("%d",&t);   while(t--)   {       scanf("%d",&n);       for(int i=1;i<=n;i++)           scanf("%d%d",&score[i].x,&score[i].y);       int sum=MMG();       printf("%d\n",sum);       for(int i=1;i<=n;i++)       {           if(match2[i]!=-1)           {               if(sum>1)               printf("%d ",i);               else               printf("%d\n",i);               --sum;           }       }   }   return 0;}



原创粉丝点击