HDU 4685 Prince and Princess(二分图匹配+强联通分量)

                                           Prince and Princess

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 2078    Accepted Submission(s): 622


 

Problem Description

There are n princes and m princesses. Princess can marry any prince. But prince can only marry the princess they DO love.
For all princes,give all the princesses that they love. So, there is a maximum number of pairs of prince and princess that can marry.
Now for each prince, your task is to output all the princesses he can marry. Of course if a prince wants to marry one of those princesses,the maximum number of marriage pairs of the rest princes and princesses cannot change.

 Input

The first line of the input contains an integer T(T<=25) which means the number of test cases.
For each test case, the first line contains two integers n and m (1<=n,m<=500), means the number of prince and princess.
Then n lines for each prince contain the list of the princess he loves. Each line starts with a integer ki(0<=ki<=m), and then ki different integers, ranging from 1 to m denoting the princesses.

 Output

For each test case, first output "Case #x:" in a line, where x indicates the case number between 1 and T.
Then output n lines. For each prince, first print li, the number of different princess he can marry so that the rest princes and princesses can still get the maximum marriage number.
After that print li different integers denoting those princesses,in ascending order.

 Sample Input

2
4 4
2 1 2
2 1 2
2 2 3
2 3 4
1 2
2 1 2

Sample Output

Case #1:
2 1 2
2 1 2
1 3
1 4
Case #2:
2 1 2

Source

2013 Multi-University Training Contest 8

 题目大意:有n个王子和m个公主,王子只能和喜欢的公主结婚,公主可以和任何王子结婚,输出每个王子能结婚的人

写了好几天的一道题,先写了POJ 1904才开始写的这道题,两道题意思差不多,但是给的条件不同,POJ1904给出了一个完美匹配,所以我们可以通过建图求强连通分量来求得答案,但是这道题没有给出完美匹配,这也就要我们自己去求一个完美匹配,求得完美匹配后就tarjan得出最后的答案

需要注意的点:会有一些人没得匹配,这时虚拟出一个节点来与它相连,具体的可以参照 https://www.cnblogs.com/frog112111/p/3387173.html

#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
#include<vector>
#include<algorithm>
using namespace std;
const int maxm=4e5+7;
const int maxn=5e4+7;
struct Node
{
	int to;
	int next;
}edge[maxm];
int n,m;
int cnt,scc_cnt,iindex;
int head[maxn];
int choose[maxn];
int dfn[maxn];
int low[maxn];
bool vis[maxn];
int match[maxn];
int belong[maxn];
int map[510][510];
stack<int> sta;
void init()
{
	memset(head,-1,sizeof(head));
	memset(choose,0,sizeof(choose));
	memset(dfn,0,sizeof(dfn));
	memset(low,0,sizeof(low));
	memset(vis,false,sizeof(vis));
	memset(belong,-1,sizeof(belong));
	memset(map,0,sizeof(map));
	iindex=cnt=scc_cnt=0;
	while(!sta.empty()) sta.pop();
	return;
}
void add(int u,int v)
{
	edge[cnt].to=v;
	edge[cnt].next=head[u];
	head[u]=cnt++;
	return;
}
bool dfs(int node)
{
	for(int i=1;i<=m;i++)
	{
		if(!vis[i]&&map[node][i])
		{
			vis[i]=true;
			if(match[i]==-1||dfs(match[i]))
			{
				match[i]=node;
				return true;
			}
		}
	}
	return false;
}
int hungry()
{
	int ans=0;
	memset(match,-1,sizeof(match));
	for(int i=1;i<=n;i++)
	{
		memset(vis,false,sizeof(vis));
		if(dfs(i))
		{
			ans++;
		}
	}
	return ans;
}
void tarjan(int node)
{
	dfn[node]=low[node]=++iindex;
	sta.push(node);
	vis[node]=true;
	for(int i=head[node];~i;i=edge[i].next)
	{
		int v=edge[i].to;
		if(!dfn[v])
		{
			tarjan(v);
			low[node]=min(low[node],low[v]);
		}
		else if(vis[v])
		{
			low[node]=min(low[node],dfn[v]);
		}
	}
	if(dfn[node]==low[node])
	{
		int v=-1;
		++scc_cnt;
		while(node!=v)
		{
			v=sta.top();
			sta.pop();
			belong[v]=scc_cnt;
			vis[v]=false;
		}
	}
	return;
}
int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	int test;
	scanf("%d",&test);
	int cas=1;
	while(test--)
	{
		scanf("%d%d",&n,&m);
		init();
		for(int i=1;i<=n;i++)
		{
			int num;
			scanf("%d",&num);
			for(int j=1;j<=num;j++)
			{
				int tmp;
				scanf("%d",&tmp);
				add(i,n+tmp);
				map[i][tmp]=1;
			}
		}
		int res=hungry();
		int new_people=0;
		for(int i=1;i<=m;i++)
		{
			if(match[i]==-1)
			{
				add(n+i,n+m+new_people);
				add(n+m+new_people,n+i);
				for(int j=1;j<=m;j++)
				{
					add(n+m+new_people,n+j);
				}
			}
			else
			{
				choose[match[i]]=1;
				add(i+n,match[i]);
			}
		}
		for(int i=1;i<=n;i++)
		{
			if(choose[i]==0)
			{
				new_people++;
				add(new_people+n+m,i);
				add(i,new_people+n+m);
				for(int j=1;j<=n;j++)
				{
					add(j,n+m+new_people);
				}
			}
		}
		memset(vis,false,sizeof(vis));
		for(int i=1;i<=n+m+new_people;i++)
		{
			if(!dfn[i])
			{
				tarjan(i);
			}
		}
		printf("Case #%d:\n",cas++);
		vector<int> ans;
		for(int node=1;node<=n;node++)
		{
			for(int j=head[node];~j;j=edge[j].next)
			{
				int v=edge[j].to;
				if(n+1<=v&&v<=n+m&&map[node][v-n]&&belong[v]==belong[node])
				{
					ans.push_back(v-n);
				}
			}
			int len=ans.size();
			sort(ans.begin(),ans.end());
			printf("%d",len);
			for(int j=0;j<len;j++)
			{
				printf(" %d",ans[j]);
			}
			puts("");
			ans.clear();
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37943488/article/details/81190143