HDOJ题目3729 I'm Telling the Truth(二分图)

I‘m Telling the Truth

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1629    Accepted Submission(s): 805

Problem 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 5004th and 5005th, Student2 said he was between 5005th and 5006th, Student3 said he was between 5004th and 5006th, Student4
said he was between 5004th and 5006th, 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 (n <= 60) 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

Source

2010 Asia Tianjin Regional Contest

Recommend

zhouzeyong   |   We have carefully selected several similar problems for you:  3724 3722 3723 3725 3726

题目大意:每个人都说出自己所在的名次范围,问最多多少个人说的是实话,并把说实话的人的号输出出来,如果有多种情况满足的话,就输出最大的序列

ac代码

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int head[100100],vis[100100],cnt,ans[100100],link[100100];
int cmp(const void *a,const void *b)
{
	return *(int *)a-*(int *)b;
}
struct s
{
	int u,v,next;
}edge[100010];
void add(int u,int v)
{
	edge[cnt].u=u;
	edge[cnt].v=v;
	edge[cnt].next=head[u];
	head[u]=cnt++;
}
int dfs(int u)
{
	int i;
	for(i=head[u];i!=-1;i=edge[i].next)
	{
		int v=edge[i].v;
		if(!vis[v])
		{
			vis[v]=1;
			if(link[v]==-1||dfs(link[v]))
			{
				link[v]=u;
				return 1;
			}
		}
	}
	return 0;
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n;
		scanf("%d",&n);
		int i,j;
		cnt=0;
		memset(head,-1,sizeof(head));
		memset(link,-1,sizeof(link));
		for(i=1;i<=n;i++)
		{
			int a,b;
			scanf("%d%d",&a,&b);
			for(j=a;j<=b;j++)
				add(i,j);
		}
		int top=0;
		for(i=n;i>0;i--)
		{
			memset(vis,0,sizeof(vis));
			if(dfs(i))
			{
				ans[top++]=i;
			}
		}
		printf("%d\n",top);
		qsort(ans,top,sizeof(ans[0]),cmp);
		for(i=0;i<top;i++)
		{
			if(i)
				printf(" %d",ans[i]);
			else
				printf("%d",ans[i]);
		}
		printf("\n");
	}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

HDOJ题目3729 I'm Telling the Truth(二分图)

时间: 2024-12-21 04:09:13

HDOJ题目3729 I'm Telling the Truth(二分图)的相关文章

HDU 3729 I&#39;m Telling the Truth(二分图最大匹配+结果输出)

题目地址:HDU 3729 二分图最大匹配+按字典序输出结果.只要从数字大的开始匹配就可以保证字典序最大了.群里有人问..就顺手写了这题.. 代码如下: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int vis[110000], head[110000], cnt, link[110000], n, a[

HDU - 3729 I&#39;m Telling the Truth(二分匹配)

题意:有n个人,每个人给出自己的名次区间,问最多有多少个人没撒谎,如果有多解,输出字典序最大的解. 分析: 1.因为字典序最大,所以从后往前分析. 2.假设后面的人没说谎,并将此作为已知条件,然后从后往前依次给每个人找到合适的名次,输出所有能找到合适名次的人即可. 3.假定给第i个人安排名次,第i+1~n个人名次已经安排好,假如第i个人想占的名次被第j个人所占,那就从第j个人可以占的名次中再找个合适的名次给j,然后把空出来的这个名次给i,如果i可以占的所有名次都被占且占领的人找不到其他可以占的名

hdu 3729 I&#39;m Telling the Truth

I'm Telling the Truth Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1377    Accepted Submission(s): 700 Problem Description After this year’s college-entrance exam, the teacher did a survey in

HDU 3729 I&#39;m Telling the Truth (二分匹配)

题意:给定 n 个人成绩排名区间,然后问你最多有多少人成绩是真实的. 析:真是没想到二分匹配,....后来看到,一下子就明白了,原来是水题,二分匹配,只要把每个人和他对应的区间连起来就好,跑一次二分匹配,裸的. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #inc

UVALive 5033 I&#39;m Telling the Truth 二分图最大匹配(略有修改)

I - I'm Telling the Truth Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVALive 5033 Description After this year's college-entrance exam, the teacher did a survey in his class on students' score. There

HDU3729 I&#39;m Telling the Truth(二分图最大匹配)

传送门 题目大意:有N个学生,老师询问每个学生的排名,每个学生都告诉了一个排名区间,求可能的最多的学生说实话的个数,以及那些学生的标号,有相同的则输出字典序最大的. 建模:对于每一个学生连上对应区间的每一个点,然后求最大匹配就行了.这里有一个优化,就是当某个学生给出的排名区间的范围大于N之后就可以不用加边了,因为他肯定是可以说真话的---–PS:不加这个优化也能过 给出代码 #include<cstdio> #include<cstring> #define MAXN 100 #d

hdu3729 I&#39;m Telling the Truth (二分图的最大匹配)

http://acm.hdu.edu.cn/showproblem.php?pid=3729 I'm Telling the Truth Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1427    Accepted Submission(s): 719 Problem Description After this year’s col

【I&#39;m Telling the Truth】【HDU - 3729】 【匈牙利算法,DFS】

思路 题意:该题主要说几个同学分别说出自己的名次所处区间,最后输出可能存在的未说谎的人数及对应的学生编号,而且要求字典序最大. 思路:刚刚接触匈牙利算法,了解的还不太清楚,附一个专门讲解匈牙利算法的博文,个人认为讲的比较清晰. AC代码 #include<iostream> #include<cstdio> #include<cstring> using namespace std; int T, n; struct Stue { int l, r; }; Stue p

HDOJ 题目3966 Aragorn&#39;s Story(Link Cut Tree成段加减点权,查询点权)

Aragorn's Story Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5505    Accepted Submission(s): 1441 Problem Description Our protagonist is the handsome human prince Aragorn comes from The Lor