HDU I'm Telling the Truth (二分图最大匹配+字典序最大路径输出(好题))
来源:互联网 发布:退火算法有什么用 编辑:程序博客网 时间:2024/06/14 04:06
关键在于建图,把 每个学生和它的可能的名次之间连上边,然后最大匹配,匹配时逆序进行(保证字典序的最大)
#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 0
- HDU I'm Telling the Truth (二分图最大匹配+字典序最大路径输出(好题))
- hdu 3729 I'm Telling the Truth(二分图最大匹配,字典序最大输出)
- HDU 3729 I'm Telling the Truth(二分图最大匹配+结果输出)
- HDU 3729 I'm Telling the Truth -- 二分图最大匹配 输出方案
- hdu 3729 I'm Telling the Truth(二分图最大匹配)
- hdu3729 I'm Telling the Truth(二分图最大匹配)
- HDU3729 I'm Telling the Truth(二分图最大匹配)
- hdu 3729 I'm Telling the Truth(最大匹配)
- 二分图的最大匹配-hdu-3729-I'm Telling the Truth
- 二分图的最大匹配-hdu-3729-I'm Telling the Truth
- HDU 3729 I'm Telling the Truth(二分图最大匹配)
- HDU 3729 I'm Telling the Truth(二分图最大匹配)
- HDU 3729 I'm Telling the Truth(二分图最大匹配)
- HDU 3729-I'm Telling the Truth-周赛4补题-二分图最大匹配-邻接表
- hdu3729—I'm Telling the Truth(二分图最大匹配)
- hdu 3729 I'm Telling the Truth 二分图匹配
- hdu 3729 I'm Telling the Truth(二分图匹配)
- HDOJ 3729 - I'm Telling the Truth 水二分图最大匹配
- PHP位运算符
- POJ1265----Area
- HDU 4135 Co-prime(容斥定理)
- TMS320F241在混合动力汽车电机控制设计应用
- 虚拟机上为ubuntu 扩充硬盘容量
- HDU I'm Telling the Truth (二分图最大匹配+字典序最大路径输出(好题))
- iOS检测版本更新
- Linux学习
- java分类和几个术语
- poj-2367 The Cow Lexicon
- 深入理解Promise框架(解决js中的回调地域问题!)
- CentOS 6下HBase安装与使用
- 彻底明白Java的多线程-实现线程同步
- poj 1995 Raising Modulo Numbers 【快速幂】