UVALive 5033 I'm Telling the Truth 二分图最大匹配(略有修改)
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu
Description

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 5004-th and 5005-th, Student2 said he was between 5005-th and 5006-th, Student3 said he was between 5004-th and 5006-th, Student4 said he was between 5004-th and 5006-th, 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(n60) represents the number of students. In the next n lines of every case, there are 2 numbers in each line, Xi and Yi(1
Xi
Yi
100000), means the i-th student's rank is between Xi and Yi, 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)
Sample Input
2
4
5004 5005
5005 5006
5004 5006
5004 5006
7
4 5
2 3
1 2
2 2
4 4
2 3
3 4
Sample Output
3
2 3 4
5
1 3 5 6 7
好爽!
我在百度搜索hdu 二分图最大匹配,百度给出的第一个结果就是这道题。刚开始看这道题的时候,以我对二分图匹配非常浅薄的理解,我怎么也想不明白这道题怎么就二分图最大匹配了。
后来想明白了,对于第i个人,我们知道他的合法区间[a[i].x,a[i].y]。首先我们将其和他合法区间内的第一个值进行匹配,如果有后来的 人需要和这个值匹配的时候,我们就让这个人和别的合法区间内的值进行匹配,如果匹配成功,就将他之前所匹配的值让出来给后来的人,如果匹配失败,就继续占 着这个位置。
这里很重要的一点是,对于一个值的匹配,我们遵守先来后到的原则,对于先来的已经匹配成功的人,如果没有找到其他符合条件的位置,这个人占的位置是绝对不能让出来的。这点很关键,因为题目还要求输出所有可能性中字典序最大的。
感觉做完这道题对二分图最大匹配理解深刻了好多,之前就是看懂了模板而已。而且还是1A。爽!
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = ;
const int INF = ;
bool vis[maxn]; //查询右集合中的点有没有被访问过
int link[maxn]; //link[i]表示右集合中的i点是由左集合中的哪个点连接的
int G[][maxn]; //邻接矩阵
int x_cnt;
int y_cnt; //左右集合的点的个数
int a[];
int tmin,tmax;
bool find(int u) //用来寻找增广路 {
for(int i = tmin; i <= tmax; i++) //遍历右集合中的每个点
{
if(!vis[i] && G[u][i]) //没有被访问过并且和u点有边相连
{
vis[i] = true; //标记该点
if(link[i] == - || find(link[i]))
{
//该点是增广路的末端或者是通过这个点可以找到一条增广路
link[i] = u;//更新增广路 奇偶倒置
return true;//表示找到一条增广路
}
}
}
return false;//如果查找了右集合里的所有点还没找到通过该点出发的增广路,该点变不存在增广路
} int solve()
{
int num = ;
memset(link, -, sizeof(link));//初始化为-1表示 不与左集合中的任何元素有link
for(int i =x_cnt ; i >=; i--) //遍历左集合
{
memset(vis, false, sizeof(vis));//每一次都需要清除标记
if(find(i))
num++;//找到一条增广路便num++
}
return num;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
int n;
scanf("%d",&n);
tmin=,tmax=-;
int u,v;
x_cnt=n;
memset(G,,sizeof(G));
for(int i=;i<=n;i++){
scanf("%d%d",&u,&v);
if(u<tmin)
tmin=u;
if(v>tmax){
tmax=v;
}
for(int j=u;j<=v;j++)
G[i][j]=;
}
int cnt=;
int ans=solve();
printf("%d\n",ans);
memset(a,,sizeof(a));
for(int i=tmin;i<=tmax;i++){
if(link[i]!=-)
a[cnt++]=link[i];
}
sort(a,a+cnt); for(int i=;i<cnt;i++){
printf("%d%c",a[i],i==cnt-?'\n':' ');
} }
return ;
}
UVALive 5033 I'm Telling the Truth 二分图最大匹配(略有修改)的更多相关文章
- I'm Telling the Truth(二分图)
.I'm Telling the Truth Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- HDU 3729 I'm Telling the Truth(二部图最大匹配+结果输出)
职务地址:HDU 3729 二分图最大匹配+按字典序输出结果. 仅仅要从数字大的開始匹配就能够保证字典序最大了.群里有人问. . 就顺手写了这题. . 代码例如以下: #include <ios ...
- hdu 3729 I'm Telling the Truth 二分图匹配
裸的二分图匹配.需要输出方案. #include<cstdio> #include<cstring> #include<vector> #include<al ...
- hdu3729 I'm Telling the Truth (二分图的最大匹配)
http://acm.hdu.edu.cn/showproblem.php?pid=3729 I'm Telling the Truth Time Limit: 2000/1000 MS (Java/ ...
- hdu 3729 I'm Telling the Truth(二分匹配_ 匈牙利算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3729 I'm Telling the Truth Time Limit: 2000/1000 MS ( ...
- hdu 3729(二分图最大匹配)
I'm Telling the Truth Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- POJ 2226二分图最大匹配
匈牙利算法是由匈牙利数学家Edmonds于1965年提出,因而得名.匈牙利算法是基于Hall定理中充分性证明的思想,它是二部图匹配最常见的算法,该算法的核心就是寻找增广路径,它是一种用增广路径求二分图 ...
- POJ2239 Selecting Courses(二分图最大匹配)
题目链接 N节课,每节课在一个星期中的某一节,求最多能选几节课 好吧,想了半天没想出来,最后看了题解是二分图最大匹配,好弱 建图: 每节课 与 时间有一条边 #include <iostream ...
- poj 2239 二分图最大匹配,基础题
1.poj 2239 Selecting Courses 二分图最大匹配问题 2.总结:看到一个题解,直接用三维数组做的,很巧妙,很暴力.. 题意:N种课,给出时间,每种课在星期几的第几节课上 ...
随机推荐
- JS每点击一次添加多少条数据
很久不写文档,平时只写日记,所以对这个有点生疏,如果写的不好别介意. 今天闲的蛋疼,于是要写写白天的东西,并且以后也会一直更新(一直写)下去. 时间太仓促了,这几个月,今天算最晚的一次凌晨1点,吃不消 ...
- C#中?和??用法
在C#中“?”有三种用法. 1.可空类型修饰符(?):引用类型可以使用空引用表示一个不存在的值,而值类型通常不能表示为空,例如:string str=null;是正确的.int i= ...
- Portal简介
Portal 在英语中是入口的意思.Portal 认证通常也称为 Web 认证,一般将 Portal 认 证网站称为门户网站. 未认证用户上网时,设备强制用户登录到特定站点,用户可以免费访问其中的服务 ...
- 用virtualenv构建一个新的python环境,这个新的环境在这个创建的文件夹下
http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/00143271210830032 ...
- matlplotlib 为折线图填充渐变颜色
概要 本篇记录绘图时填充颜色时的一些常用设置,主要用到了 imshow,fill 函数. 填充图实例 填充的效果图如下: 图 1:渐变色效果图 可根据下方给出的代码进行自定义. #!/us ...
- CSS3和动画
定位: z-index叠层 数字越大越往上层 注意:要用z-index属性必须设position属性 溢出:overflow 属性值:visible 不剪切内容也不添加滚动条 Auto ...
- AngularJS 历经实例
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- gravity 使用操作。
gravity 使用操作.最近我司有一个比较奇葩的需求,我们的环境是主从,因为数据量较大会定期的删除数据,最近不行了,要求新建出来一个库 同步正事环境的数据,但是要剔除 delete ,drop,tr ...
- React学习记录二
环境基本弄清楚了以后,开始总会写个hello world什么的,开发做了这么久了,就跳过这一步吧. 还是从打开vscode说起吧,这里文件菜单打开一个文件夹Demos,查看菜单打开集成终端,也可以使用 ...
- HDU 1423 Greatest Common Increasing Subsequence(LCIS)
Greatest Common Increasing Subsequenc Problem Description This is a problem from ZOJ 2432.To make it ...