HDU I'm Telling the Truth (二分图最大匹配+字典序最大路径输出(好题))

关键在于建图,把 每个学生和它的可能的名次之间连上边,然后最大匹配,匹配时逆序进行(保证字典序的最大)

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<set>
#include<map>
#include<string>
#include<cstring>
#include<stack>
#include<queue>
#include<vector>
#include<cstdlib>
#define lson (rt<<1),L,M
#define rson (rt<<1|1),M+1,R
#define M ((L+R)>>1)
#define cl(a,b) memset(a,b,sizeof(a));
#define LL long long
#define P pair<int,int>
#define X first
#define Y second
#define pb push_back
#define fread(zcc)  freopen(zcc,"r",stdin)
#define fwrite(zcc) freopen(zcc,"w",stdout)
using namespace std;
const int maxn=100005;
const int inf=999999;

vector<int> G[maxn];
int matching[maxn];
bool vis[maxn];
int num_left;
bool as[maxn];
int dfs(int u){
    int N=G[u].size();
    for(int i=0;i<N;i++){
        int v=G[u][i];
        if(vis[v])continue;
        vis[v]=true;
        if(matching[v]==-1||dfs(matching[v])){
            matching[v]=u;
            as[u]=true;
            return 1;
        }
    }
    return 0;
}
int hungar(){
    int ans=0;
    cl(matching,-1);
    cl(as,false);
    for(int i=num_left-1;i>=0;i--){
        cl(vis,false);
        ans+=dfs(i);
    }
    return ans;
}
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        int n,x,y;
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            scanf("%d%d",&x,&y);
            for(int j=x;j<=y;j++){
                G[i].pb(j);
            }
        }
        num_left=n;
        int sum=hungar();//匹配的时候逆序
        printf("%d\n",sum);
        for(int i=0;i<n-1;i++){
            if(as[i]){
                printf("%d ",i+1);
            }
        }
        printf("%d\n",n);
        for(int i=0;i<maxn;i++)G[i].clear();
    }
    return 0;
}






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值