当前位置: 代码迷 >> 综合 >> HDU 3729 I'm Telling the Truth(二分图匹配+匈牙利算法+变形)
  详细解决方案

HDU 3729 I'm Telling the Truth(二分图匹配+匈牙利算法+变形)

热度:9   发布时间:2023-11-17 14:29:30.0

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, 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

题解:

题意:

有不超过60的学生,每个学生都不想说出自己的成绩,只说了自己的排名范围,让你找至少多少个人说的是真话,然后按照字典序最大输出说真话人的名字

思路:

这是我第一次没看题解做出来稍微复杂一点的匈牙利算法,也就感觉变了下形,也就是人匹配排名的问题,匈牙利算法搞的去,因为这里的排名数据范围比较大,我们就不用邻接矩阵了,改用了邻接表,然后就快了很多,至于字典序最大只要从后往前匹配就好了,后来搜了下题解的代码,比我的代码慢了一倍hhhhhh

代码:

#include<algorithm>
#include<iostream>
#include<cstring>
#include<stdio.h>
#include<math.h>
#include<string>
#include<stdio.h>
#include<queue>
#include<stack>
#include<map>
#include<deque>
#define M (t[k].l+t[k].r)/2
#define lson k*2
#define rson k*2+1
#define ll long long
using namespace std;
int num[61];//存每个同学对应可能有几个排名
int p[61][100001];//邻接表,存每个同学对应的排名是哪几个
int vis[100001];//遍历数组
int used[100001];//存排名i匹配的人
int find(int u)//改进的匈牙利算法
{int i;for(i=0;i<num[u];i++)//这里就只在那几个人里找不用全程扫一遍了{if(!vis[p[u][i]]){vis[p[u][i]]=1;if(!used[p[u][i]]||find(used[p[u][i]])){used[p[u][i]]=u;return 1;}}}return 0;
}
int main()
{int i,j,k,n,test,x,y;scanf("%d",&test);while(test--){scanf("%d",&n);memset(num,0,sizeof(num));memset(used,0,sizeof(used));stack<int>q;//用堆,因为先进后出for(i=1;i<=n;i++){scanf("%d%d",&x,&y);for(j=x;j<=y;j++){p[i][num[i]]=j;//邻接表存数据num[i]++;}}int ans=0;for(i=n;i>=1;i--)//从后往前{memset(vis,0,sizeof(vis));if(find(i)){q.push(i);ans++;}}printf("%d\n",ans);while(!q.empty())//从堆里面取{printf("%d",q.top());q.pop();if(q.empty())printf("\n");elseprintf(" ");}}return 0;
}