Prince and Princess

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

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
 
Recommend
zhuyuanchen520
 
 
n 个王子, m个公主,王子只能娶他喜欢的,公主可以嫁给任何王子,
对每一个王子求出他能娶的公主的最大集合,使之他娶这个集合内的任何一个,都不影响其他王子的选择
 
首先想到的肯定是匹配,但显然不是多重匹配,
 把每个具有相同公主集合的王子放在一起发现
从任何一个王子出发都能到达所有的公主 那么如果我们从公主向她匹配的王子连一条边
是不是就从任何一个王子出发都能到达其它所有的点
因为是有向图  是不是就是 任何两点之前都至少存在一条可以相互到达的路径
那就是SCC了
 
正确做法是
每个王子向他喜欢的公主连边,进行一次匹配求出最大匹配cnt
然后剩下王子n - cnt个,建立(n - cnt)个虚拟的公主,所有的实体王子都向这些公主连边
剩下公主m - cntge, 建立(m - cnt)个虚拟的王子,这些王子都向所有的实体公主连边
再求一次匹配
最后每个公主向她嫁的王子连边 求强连通就好了

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define rb(a) scanf("%lf", &a)
#define rf(a) scanf("%f", &a)
#define pd(a) printf("%d\n", a)
#define plld(a) printf("%lld\n", a)
#define pc(a) printf("%c\n", a)
#define ps(a) printf("%s\n", a)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = , INF = 0x7fffffff, maxm = ;
int n, m, s, t;
int head[maxn], cur[maxn], vis[maxn], d[maxn], cnt, nex[maxm << ], nex2[maxm << ];
int head2[maxn], cnt2;
int vis1[maxn], vis2[maxn]; struct node
{
int u, v, c, flag;
}Node[maxm << ], Edge[maxm << ]; void add_(int u, int v, int c, int flag)
{
Node[cnt].u = u;
Node[cnt].v = v;
Node[cnt].c = c;
Node[cnt].flag = flag;
nex[cnt] = head[u];
head[u] = cnt++;
} void add(int u, int v, int c)
{
add_(u, v, c, );
add_(v, u, , );
} void add2(int u, int v)
{
Edge[cnt2].u = u;
Edge[cnt2].v = v;
nex2[cnt2] = head2[u];
head2[u] = cnt2++;
} bool bfs()
{
queue<int> Q;
mem(d, );
Q.push(s);
d[s] = ;
while(!Q.empty())
{
int u = Q.front(); Q.pop();
for(int i = head[u]; i != -; i = nex[i])
{
int v = Node[i].v;
if(!d[v] && Node[i].c > )
{
d[v] = d[u] + ;
Q.push(v);
if(v == t) return ;
}
}
}
return d[t] != ;
} int dfs(int u, int cap)
{
int ret = ;
if(u == t || cap == )
return cap;
for(int &i = cur[u]; i != -; i = nex[i])
{
int v = Node[i].v;
if(d[v] == d[u] + && Node[i].c > )
{
int V = dfs(v, min(cap, Node[i].c));
Node[i].c -= V;
Node[i ^ ].c += V;
ret += V;
cap -= V;
if(cap == ) break;
}
}
if(cap > ) d[u] = -;
return ret;
} int Dinic()
{
int ans = ;
while(bfs())
{
memcpy(cur, head, sizeof head);
ans += dfs(s, INF);
}
return ans;
} int pre[maxn], low[maxn], sccno[maxn], dfs_clock, scc_cnt;
stack<int> S; void dfs(int u)
{
pre[u] = low[u] = ++dfs_clock;
S.push(u);
for(int i = head2[u]; i != -; i = nex2[i])
{
int v = Edge[i].v;
if(!pre[v])
{
dfs(v);
low[u] = min(low[u], low[v]);
}
else if(!sccno[v])
low[u] = min(low[u], pre[v]);
}
if(low[u] == pre[u])
{
scc_cnt++;
for(;;)
{
int x = S.top(); S.pop();
sccno[x] = scc_cnt;
if(x == u) break;
}
}
}
int G[maxn], ans;
int main()
{
int T;
int kase = ;
rd(T);
while(T--)
{
mem(head, -), mem(head2, -);
mem(vis1, ), mem(vis2, );
cnt = cnt2 = ;
rd(n), rd(m);
s = , t = maxn - ;
bool flag = ;
int tmp, u, v;
int x = max(n, m);
for(int i = ; i <= n; i++)
{
add(s, i, );
rd(tmp);
for(int j = ; j <= tmp; j++)
{
rd(v);
add(i, x + v, );
add2(i, x + v);
}
}
for(int i = ; i <= m; i++) add(x + i, t, );
int max_cnt = Dinic(); int mx = x * ;
for(int i = ; i <= m - max_cnt; i++)
{
mx++;
add(s, mx, );
for(int j = ; j <= m; j++)
add(mx, x + j, ), add2(mx, x + j);
}
for(int i = ; i <= n - max_cnt; i++)
{
mx++;
add(mx, t, );
for(int j = ; j <= n; j++)
add(j, mx, ), add2(j, mx);
}
Dinic();
for(int i = ; i < cnt; i++)
{
if(!Node[i].flag || Node[i].u == s || Node[i].v == t || Node[i].c != ) continue;
add2(Node[i].v, Node[i].u);
}
dfs_clock = scc_cnt = ;
mem(sccno, );
mem(pre, );
for(int i = ; i <= mx; i++)
if(!pre[i]) dfs(i);
printf("Case #%d:\n", ++kase);
for(int i = ; i <= n; i++)
{
ans = ;
for(int j = head2[i]; j != -; j = nex2[j])
{
int v = Edge[j].v;
if(sccno[i] == sccno[v] && v - x <= m)
G[ans++] = v;
}
sort(G, G + ans);
printf("%d", ans);
for(int j = ; j < ans; j++)
{
printf(" ");
printf("%d", G[j] - x);
} printf("\n"); } } return ;
}

Prince and Princess HDU - 4685(匹配 + 强连通)的更多相关文章

  1. H - Prince and Princess - HDU 4685(二分匹配+强连通分量)

    题意:有N个王子M个公主,王子喜欢一些公主,而且只能是王子喜欢的人,他们才可以结婚,现在让他们尽可能多的结婚的前提下找出来每个王子都可以和谁结婚. 分析:先求出来他们的最大匹配,因为给的数据未必是完备 ...

  2. hdu 4685(匹配+强连通分量)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4685 思路:想了好久,终于想明白了,懒得写了,直接copy大牛的思路了,写的非常好! 做法是先求一次最 ...

  3. HDU4685:Prince and Princess(二分图匹配+tarjan)

    Prince and Princess Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Othe ...

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

    题意:婚配问题,但是题目并不要求输出最大匹配值,而是让我们输出,一个王子可以与哪些王妃婚配而不影响最大匹配值. 解决办法:先求一次最大匹配,如果有两个已经匹配的王妃,喜欢她们两个的有两个或者以上相同的 ...

  5. HDU 4685 Prince and Princess (2013多校8 1010题 二分匹配+强连通)

    Prince and Princess Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Othe ...

  6. HDU 4685 Prince and Princess 二分图匹配+tarjan

    Prince and Princess 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4685 Description There are n pri ...

  7. 强连通+二分匹配(hdu4685 Prince and Princess)

    Prince and Princess Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Othe ...

  8. POJ 1904 HDU 4685

    这两道题差不多,POJ这道我很久以前就做过,但是比赛的时候居然没想起来.. POJ 这道题的题意是,N个王子每个人都有喜欢的公主,当他们选定一个公主结婚时,必须是的剩下的人也能找到他喜欢的公主结婚. ...

  9. UVA - 10635 Prince and Princess LCS转LIS

    题目链接: http://bak.vjudge.net/problem/UVA-10635 Prince and Princess Time Limit: 3000MS 题意 给你两个数组,求他们的最 ...

随机推荐

  1. 监控工具之zabbix server3.4 部署配置

    [root@localhost src]# cat /etc/redhat-release CentOS Linux release 7.5.1804 (Core) [root@localhost s ...

  2. XPath Helper的安装与使用

    摘要 : XPath Helper可以支持在网页点击元素生成xpath,整个抓取使用了xpath.正则表达式.消息中间件.多线程调度框架的chrome插件. xpath:是一门XML和HTML文档中查 ...

  3. jenkins自动化工具使用教程(转)

    自动化构建.测试.部署.代码检测越来越重要.主要有一下几点原因 企业做大,项目变多,多端支持(web,h5,小程序等) 微服务提倡高内聚低耦合,项目因拆分变多 DevOps自动化运维流行 集群化,高可 ...

  4. 小小白搭建nextcloud云盘

    我是一名linux的小小白,今天就利用自己的所学搭建属于自己的云盘——nextcloud. 本人学生狗,普通的云盘也要几十块钱,既然我们只是拿来搭建巩固自己知识并不做为生产力,我们就用VMware W ...

  5. Bootstrap -- 插件: 模态框、滚动监听、标签页

    Bootstrap -- 插件: 模态框.滚动监听.标签页 1. 模态框(Modal): 覆盖在父窗体上的子窗体. 使用模态框: <!DOCTYPE html> <html> ...

  6. 我的第一个python web开发框架(25)——定制ORM(一)

    在开始编写ORM模块之前,我们需要先对db_helper进行重构,因为ORM最终生成的sql是需要转给db_helper来执行的,所以拥有一个功能完善.健壮的数据库操作类是非常必要的. 这是项目原db ...

  7. const int *p;int *const p;区别及用法

    在单片机程序设计中,我们经常会用到const这个关键字,在有些单片机的编译器中可能会是code(比如51系列单片机),但我们在学习C语言的时候,首先还是先学到的const.我们知道,const关键字的 ...

  8. 初学Django项目可能会遇到的问题

    1. 出现莫名其妙的 app01 我项目中的app名字并不是app01,可是运行python manage.py makemigrations的时候总是提示app01不是已安装的app Applyin ...

  9. Windows使用MongoDB,以及索引创建

    安装MongoDB https://www.mongodb.com/download-center#community 点击msi安装程序进行安装,可以进行自定义安装,选择安装位置,我选择的是D盘 在 ...

  10. Flafka: Apache Flume Meets Apache Kafka for Event Processing

    The new integration between Flume and Kafka offers sub-second-latency event processing without the n ...