HDU4685:Prince and Princess(二分图匹配+tarjan)
Prince and Princess
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 2281 Accepted Submission(s): 677
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4685
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
题意:
给出n个王子,m个公主,然后每个王子都有自己喜欢的公主,公主可以接受所有的王子。。现在要求输出每个王子可以的结婚对象,并且他们结婚过后不影响到最大匹配数量。
题解:
先可以参考下POJ1904的题解
然后这个题和POJ1904的不同就在于,这个题n和m是不等的,一开始的最大匹配也没有给出。
在理解了POJ1904的做法过后,对于这道题就考虑一开始利用二分图匹配自己构造一个最大匹配出来。然后将模型转化为上个题的模型:构造虚拟结点使得n,m相等。
具体的构造方法就是有多少单身王子,就构造多少个虚拟公主;有多少个虚拟公主,就构造多少个虚拟王子,并且将虚拟生物与所有异性进行连边。这样就可以使得所有人中没有单身。那么这个问题就转化为上一个问题了,之后就利用POJ1904的方法来做,注意一下输出即可。
我这里二分图匹配写拐了。。太菜了啊,debug了好久。
代码如下:
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <queue> #include <stack> #include <vector> using namespace std; typedef long long ll; const int N = 2005; int t; int n,m,tot; int match[N],head[N],link[N][N],check[N]; stack <int> s; vector <int> ans; int T,num; int scc[N],dfn[N],low[N],vis[N]; struct Edge{ int u,v,next; }e[N*N]; void adde(int u,int v){ e[tot].v=v;e[tot].u=u;e[tot].next=head[u];head[u]=tot++; } void Tarjan(int u){ dfn[u]=low[u]=++T;vis[u]=1; s.push(u); for(int i=head[u];i!=-1;i=e[i].next){ int v=e[i].v; if(!vis[v]){ Tarjan(v); low[u]=min(low[u],low[v]); }else if(!scc[v]){ low[u]=min(low[u],dfn[v]); } } if(low[u]==dfn[u]){ num++;int now; do{ now = s.top();s.pop(); scc[now]=num; }while(!s.empty() && now!=u); } } int dfs(int x,int nown){ for(int i=1;i<=nown;i++){ if(!check[i] && link[x][i]){ check[i]=1; if(match[i]==-1 || dfs(match[i],nown)){ match[i]=x; return 1; } } } return 0; } int hungry(int n1,int m1){ memset(match,-1,sizeof(match)); int ans=0; for(int i=1;i<=n1;i++){ memset(check,0,sizeof(check)); ans+=dfs(i,m1); } return ans ; } void init(){ memset(link,0,sizeof(link)); memset(match,-1,sizeof(match)); memset(head,-1,sizeof(head)); memset(scc,0,sizeof(scc)); memset(dfn,0,sizeof(dfn)); memset(vis,0,sizeof(vis)); num=0;T=0;tot=0; } int main(){ cin>>t; int Case = 0; while(t--){ Case++; init(); scanf("%d%d",&n,&m); for(int i=1,k;i<=n;i++){ scanf("%d",&k); for(int j=1,l;j<=k;j++){ scanf("%d",&l); link[i][l]=1; } } int cnt=hungry(n,m); int nown,nowm; nown=nowm=n+m-cnt; for(int i=n+1;i<=nown;i++){ for(int j=1;j<=nown;j++){ link[i][j]=1; } } for(int i=1;i<=n;i++){ for(int j=m+1;j<=nowm;j++){ link[i][j]=1; } } hungry(nown,nowm); for(int i=1;i<=nown;i++){ for(int j=1;j<=nowm;j++){ if(link[i][j]) adde(i,nown+j); } } for(int i=1;i<=nown;i++){ if(match[i]!=-1) adde(i+nown,match[i]); } printf("Case #%d:\n",Case); while(!s.empty()) s.pop(); for(int i=1;i<=2*nown;i++){ if(!vis[i]) Tarjan(i); } for(int i=1;i<=n;i++){ ans.clear(); for(int j=head[i];j!=-1;j=e[j].next){ int v=e[j].v;v-=nown; if(v>m) continue ; if(scc[i]==scc[v+nown]) ans.push_back(v); } sort(ans.begin(),ans.end()); printf("%d",(int)ans.size()); for(int j=0;j<ans.size();j++){ printf(" %d",ans[j]); } printf("\n"); } } return 0; }
重要的是自信,一旦有了自信,人就会赢得一切。