HDU - 4685 Prince and Princess

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 k  i(0<=k  i<=m), and then k  i 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


这个题其实是poj1904的加强版,唯一的区别就是这个题中王子和公主不一定完全匹配,所以我们可以先求出他们的最大匹配,然后把没有匹配上的公主和建一个虚拟的点“虚拟的王子”和她匹配,然后这个虚拟王子和所有的公主建边,同理为没有匹配上的王子建立虚拟的公主节点,然后这个公主节点和所有的王子节点建边。然后就强连通分量即可。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <stack>
#include <vector>
using namespace std;
#define MS(a,x) memset(a,x,sizeof(a))
const int INF = 0x3f3f3f3f;
const int maxn = 2010;
const int maxm = 400010;
int n,m;
int head[maxn],edge_cnt;
int dfn[maxn],low[maxn],dfs_clock;
int bel[maxn],bel_cnt;
int Map[510][510],match[maxn],vist[maxn],num_mm,cho[maxn],ans[maxn];
vector<int> V[maxn];
stack<int>S;
struct Edge{
    int from,to,next;
}edge[maxm<<1];
void init(){
    MS(head,-1);
    MS(dfn,-1);
    MS(low,-1);
    MS(bel,-1);
    MS(Map,0);
    MS(cho,0);
    MS(ans,0);
    MS(match,-1);
    for(int i = 0; i < maxn; i++)
        V[i].clear();
    while(!S.empty())S.pop();
    edge_cnt = dfs_clock = bel_cnt = 0;
}
inline void ADD(int u,int v){
    edge[edge_cnt].from = u;
    edge[edge_cnt].to = v;
    edge[edge_cnt].next = head[u];
    head[u] = edge_cnt++;
}
int dfs(int now){
    for(int i = 1; i <= m; i++){
        if(!vist[i] && Map[now][i]){
            vist[i] = 1;
            if(match[i] == -1 || dfs(match[i])){
                match[i] = now;
                return 1;
            }
        }
    }
    return 0;
}
void Tarjan(int now){
    dfn[now] = low[now] = ++dfs_clock;
    S.push(now);
    for(int i = head[now]; ~i; i = edge[i].next){
        int v = edge[i].to;
        if(dfn[v] == -1){
            Tarjan(v);
            low[now] = min(low[now],low[v]);
        }
        else if(bel[v] == -1)
            low[now] = min(low[now],dfn[v]);
    }
    if(low[now] == dfn[now]){
        ++bel_cnt;
        while(S.top() != now){
            bel[S.top()] = bel_cnt;
            S.pop();
        }
        bel[now] = bel_cnt;
        S.pop();
    }
}
void solve(){
    for(int i = 1; i <= n+m+num_mm; i++){
        if(dfn[i] == -1)
            Tarjan(i);
    }
    for(int i = n+1; i <= n+m; i++) {
        V[bel[i]].push_back(i-n);
    }
    int cnt = 0;
    for(int i = 1; i <= n; i++){
        cnt = 0;
        for(int j = 0; j < V[bel[i]].size(); j++) {
            if(Map[i][V[bel[i]][j]])
                ans[cnt++] = V[bel[i]][j];
        }
        printf("%d",cnt);
        for(int j = 0; j < cnt; j++)
            printf(" %d",ans[j]);
        printf("\n");
    }
}
int main(){
    int num,v,T,cas = 0;
    scanf("%d",&T);
    while(T--){
        init();
        scanf("%d %d",&n,&m);
        for(int i = 1; i <= n; i++){
            scanf("%d",&num);
            for(int j = 1; j <= num; j++){
                scanf("%d",&v);
                ADD(i,v+n);
                Map[i][v] = 1;
            }
        }
        int num = 0;
        for(int i = 1; i <= m; i++){
            MS(vist,0);
            if(dfs(i))
                num++;
        }
        num_mm = 0;
        for(int i = 1; i <= m; i++){
            if(match[i] == -1){
                num_mm++;
                ADD(n+m+num_mm,i+n);
                ADD(i+n,n+m+num_mm);
                for(int j = 1; j <= m; j++)
                    ADD(n+m+num_mm,j+n);
            }
            else
                cho[match[i]] = 1;
            ADD(i+n,match[i]);
        }
        for(int i = 1; i <= n; i++){
            if(cho[i] == 0){
                num_mm++;
                ADD(i,n+m+num_mm);
                ADD(num_mm+n+m,i);
                for(int j = 1; j <= n; j++)
                    ADD(j,n+m+num_mm);
            }
        }
        printf("Case #%d:\n",++cas);
        solve();
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值