您的位置:首页 > 其它

二分图最大匹配-变形题HDU - 3729

2020-05-11 04:10 239 查看

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)
题目大意:
有n个人,每个人都有一个自己能够呆的区间,区间中每个单元只能分配一个人,问最大分配数和最大字典序分配。
思路:
1、对于最大分配数,我们用人来匹配能够呆的区间,每个单位的区间作为一个匹配对象,求一次匈牙利算法,找到其最大匹配数。
我们可以把区间的左部分看做是二分图的左枝,区间的右部分看做是二分图的右枝。
2、对于最大字典序分配,我们只需要再用人匹配单位区间的时候,从n到1的去匹配即可。

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct node
{
int x,y;
}a[120];
int ans[100050];
int vis[100050];
int match[100050];
int n;
int find(int u)
{
for(int i=a[u].x;i<=a[u].y;i++)
{
if(vis[i]==0)
{
vis[i]=1;
if(match[i]==-1||find(match[i]))
{
match[i]=u;
return 1;
}
}
}
return 0;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(match,-1,sizeof(match));
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d%d",&a[i].x,&a[i].y);
}
int output=0;
for(int i=n;i>=1;i--)
{
memset(vis,0,sizeof(vis));
if(find(i))output++;
}
printf("%d\n",output);
int tt=0;
for(int i=1;i<100050;i++)
{
if(match[i]==-1)continue;
ans[tt++]=match[i];
}
sort(ans,ans+tt);
for(int i=0;i<tt;i++)
{
if(i==0)printf("%d",ans[i]);
else printf(" %d",ans[i]);
}
printf("\n");
}
}
Starry_Sky_Dream 原创文章 50获赞 4访问量 2330 关注 私信
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: