题解及代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define maxn 11111
using namespace std;
typedef long long ll;
int match[100010];
int chk[100010],n;
int st[66];
struct node
{
int l,r;
}map[62];
int dfs(int p)
{
int i,t;
for(int i=map[p].l;i<=map[p].r;i++)
if(!chk[i])
{
chk[i]=1;
t=match[i];
match[i]=p;
if(t==-1||dfs(t))
return 1;
match[i]=t;
}
return 0;
}
int Pro()
{
int i,res=0;
for(int i=n;i>=1;i--) //字典序最大从n到1遍历,字典序最小从1到n遍历
{
memset(chk,0,sizeof(chk));
res+=dfs(i);
}
return res;
}
int main()
{
memset(map,0,sizeof(map));
int t;
cin>>t;
while(t--)
{
scanf("%d",&n);
memset(match,-1,sizeof(match));
for(int i=1;i<=n;i++)
{
scanf("%d%d",&map[i].l,&map[i].r);
}
printf("%d\n",Pro());
int k=0;
for(int i=1;i<=n;i++)
{
for(int j=map[i].l;j<=map[i].r;j++)
if(match[j]>0)
{
st[k++]=match[j];
match[j]=-1;
}
}
sort(st,st+k);
for(int i=0;i<k;i++)
if(i<k-1)
{
printf("%d ",st[i]);
}
else printf("%d\n",st[i]);
}
return 0;
}
/*
二分图最大匹配的题目,直接使用模版就行了,这里主要是求出字典序最大的序列,
直接在代码中进行小修改就行了,在代码中给出了需要注意的地方。
*/