HDU 3279 2010ACM天津赛区现场赛 图论 最大匹配

I'm Telling the Truth

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 340    Accepted Submission(s): 162


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, X i and Y i (1 <= X i <= Y i <= 100000), means the i-th student’s rank is between X i and Y i, 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

 

 

非常基本的最大匹配。

只不过要输出方案。

建图很容易,可以直接从每一个人连接到每一个排名

然后最大匹配,完事后,因为要输出最大的字典序

所以,在匹配的时候我们倒过来匹配,从最大的n匹配到1

这样存好答案之后在排个序,输出来就OK了

 

另外:

由于这个题的那段区间非常大。但是人却非常少。所以有一个节约内存和时间的方法

我们可以用vector来存图,然后用map来标记

这样节约时间和空间。

 

详细见代码:

#pragma warning(disable:4786)
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<map>

using namespace std;

vector<int>g[65];
map<int,bool>my;
int link[100005];
int n;

void swap(int &a,int &b)
{
	int temp;
	temp=a;
	a=b;
	b=temp;
}

void init()
{
	int i;
	memset(link,-1,sizeof(link));
	for(i=1;i<=n;i++)
		g[i].clear();
}

bool dfs(int u)
{
	int i,v;
	for(i=0;i<g[u].size();i++)
	{
		v=g[u][i];
		if(!my[v])
		{
			my[v]=true;
			if(link[v]==-1||dfs(link[v]))
			{
				link[v]=u;
				return true;
			}
		}
	}
	return false;
}

int main()
{
	int t,i,j,a,b;
	int out[65];
	scanf("%d",&t);
	while(t--)
	{
		init();
		scanf("%d",&n);
		for(i=1;i<=n;i++)
		{
			scanf("%d%d",&a,&b);
			if(a>b)
				swap(a,b);
			for(j=a;j<=b;j++)
				g[i].push_back(j);
		}
		int ans=0,len=0;
		for(i=n;i>=1;i--)
		{
			my.clear();
			if(dfs(i))
				ans++;
		}
		printf("%d\n",ans);
		for(i=1;i<=100000;i++)
		{
			if(link[i]!=-1)
				out[len++]=link[i];
		}
		sort(out,out+len);
		for(i=0;i<len-1;i++)
			printf("%d ",out[i]);
		printf("%d\n",out[len-1]);
	}
	return 0;
}


 

你可能感兴趣的:(vector,Integer,output,2010,Numbers,Intervals)