HDU 3729 I'm Telling the Truth(二分图最大匹配)
来源:互联网 发布:淘宝买东西后怎么评价 编辑:程序博客网 时间:2024/06/02 03:42
HDU 3729 I'm Telling the Truth(二分图最大匹配)
http://acm.hdu.edu.cn/showproblem.php?pid=3729
题意:
一位老师想问N位同学他们大学入学考试的成绩. 每位同学只告诉老师他在全省的排名区间,比如[5006,5008]. 最终老师发现有一些人在说谎,比如区间[5006,5008]中有4个同学都说自己排名在这个范围内,那肯定是不可能的.现在老师要你找出最多有多少个同学没有说谎,且输出字典序最大的同学序号结果.
分析:
刚看到这题目的时候,我的第一反应是将所有区间离散化分为一个一个最小不可再分的区间.然后在用每个学生去与每个区间连线.
然后看了别人的代码发现可以更简单的处理(关键在于本题的区间所覆盖的点范围比较小,即测试数据规模小). 想想本题的二分图的左边点集和右边点集分别是什么? 左边点集是学生序号,右边点集是1到100000的排名.
如果i学生排名在区间[5006,5008],那么应该左i与右5006,右5007,右5008 三个点连线.(想想是不是) 所以我们在该二分图表示右边点集的时候仍然用一个一个的排名点作为右边的点, 但是我们在写match函数遍历节点u所连接的右边的点的时候变通一下.(具体看代码)
最后结果要求输出字典序最大的解,所以我们二分匹配的时候需要从学生序号大到小这个顺序去找每个学生的匹配.
AC代码:
#include <cstdio>#include <algorithm>#include <iostream>#include <cstring>#include<stack>#include<vector>using namespace std;const int maxn=100+10;const int maxm=100000+10;struct node{int x,y;}nodes[maxn];int num[maxn];struct Max_Match{int n,m;//vector<int>g[maxn];bool vis[maxm];int left[maxm];void init(int n){this->n=n;//for(int i=1;i<=n;i++)//g[i].clear();memset(left,-1,sizeof left);}bool match(int u){for(int i=nodes[u].x;i<=nodes[u].y;i++){//int v=g[u][i];if(!vis[i]){vis[i]=true;if(left[i]==-1||match(left[i])){left[i]=u;num[u]=i;return true;}}}return false;}int solve(){int ans=0;for(int i=n;i>=1;i--){memset(vis,false,sizeof vis);if(match(i))ans++;}return ans;}}MM;int main(){int n,m; int t; scanf("%d",&t); while(t--) {scanf("%d",&n);for(int i=1;i<=n;i++){scanf("%d%d",&nodes[i].x,&nodes[i].y);}memset(num,0,sizeof num);MM.init(n);int ans=MM.solve();printf("%d\n",ans);for(int i=1;i<=n;i++)if(num[i]){ans--;if(ans>0){printf("%d ",i);}elseprintf("%d\n",i);} }}
阅读全文
0 0
- 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
- 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-周赛4补题-二分图最大匹配-邻接表
- HDOJ 3729 - I'm Telling the Truth 水二分图最大匹配
- 【HDOJ 3729】 I'm Telling the Truth (二分图最大匹配)
- 使用ShellExecute函数打开IE浏览器下的新窗口
- android开发--ProgressBar 颜色/样式的设置
- 3347-数据结构实验之数组三:快速转置
- 进程间通信之messenger
- freemarker基本命令学习
- HDU 3729 I'm Telling the Truth(二分图最大匹配)
- ios百度地图更改罗盘指南针位置
- 测试
- Spring注解@Component、@Repository、@Service、@Controller区别
- 例题 4-5 追踪电子表格中的单元格(Spreadsheet Tracking) UVa 512
- c# 编码那么事
- 鼠标与按键 事件触发顺序
- kubernetes1.6中redis-mong-zookeepe-rabbitmq集群部署
- 控件的Ontouch事件和onClick事件冲突