当前位置:编程学习 > C/C++ >>

hdu 3729 I'm Telling the Truth (二分图匹配)

I'm Telling the Truth
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1109    Accepted Submission(s): 557
 
 
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
 
 
Source
2010 Asia Tianjin Regional Contest
 
 
思路:
建图+二分图匹配(匈牙利算法),从后往前匹配再逆序输出,能得到递增且最大的字典序。
 
ps:用Hopcroft_Karp()算法好像不能按题目条件输出。
 
代码:
 
#include <iostream>  
#include <cstdio>  
#include <cstring>  
#include <algorithm>  
#include <cmath>  
#include <string>  
#include <map>  
#include <stack>  
#include <vector>  
#include <set>  
#include <queue>  
//#pragma comment (linker,"/STACK:102400000,102400000")  
#define maxn 200005  
#define MAXN 6000005  
#define mod 1000000007  
#define INF 0x3f3f3f3f  
using namespace std;  
  
int n,m,cnt,ans;  
int anss[105];  
int from[maxn];  
bool use[maxn];  
vector<int>g[maxn];  
  
bool match(int x)  
{  
    int i;  
    for(i=0;i<g[x].size();i++)  
    {  
        if(!use[g[x][i]])  
        {  
            use[g[x][i]]=1;  
            if(from[g[x][i]]==-1||match(from[g[x][i]]))  
            {  
                from[g[x][i]]=x;  
                return true ;  
            }  
        }  
    }  
    return false ;  
}  
void hungry()  
{  
    int i;  
    memset(from,-1,sizeof(from));  
    for(i=n;i>=1;i--)  
    {  
        memset(use,0,sizeof(use));  
        if(match(i))  
        {  
            ans++;  
            anss[ans]=i;  
        }  
    }  
}  
int main()  
{  
    int i,j,t,u,v;  
    scanf("%d",&t);  
    while(t--)  
    {  
        scanf("%d",&n);  
        for(i=1;i<=n;i++)  
        {  
            g[i].clear();  
        }  
        for(i=1;i<=n;i++)  
        {  
            scanf("%d%d",&u,&v);  
            for(j=u;j<=v;j++)  
            {  
                g[i].push_back(j);  
            }  
        }  
        ans=0;  
        hungry();  
        printf("%d\n",ans);  
        for(i=ans;i>1;i--)  
        {  
            printf("%d ",anss[i]);  
        }  
        printf("%d\n",anss[i]);  
    }  
    return 0;  
}  

 

 
补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,