Prince and Princess HDU - 4685(匹配 + 强连通)
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
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.
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.
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.
4 4
2 1 2
2 1 2
2 2 3
2 3 4
1 2
2 1 2
2 1 2
2 1 2
1 3
1 4
Case #2:
2 1 2
#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(匹配 + 强连通)的更多相关文章
- H - Prince and Princess - HDU 4685(二分匹配+强连通分量)
题意:有N个王子M个公主,王子喜欢一些公主,而且只能是王子喜欢的人,他们才可以结婚,现在让他们尽可能多的结婚的前提下找出来每个王子都可以和谁结婚. 分析:先求出来他们的最大匹配,因为给的数据未必是完备 ...
- hdu 4685(匹配+强连通分量)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4685 思路:想了好久,终于想明白了,懒得写了,直接copy大牛的思路了,写的非常好! 做法是先求一次最 ...
- HDU4685:Prince and Princess(二分图匹配+tarjan)
Prince and Princess Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Othe ...
- HDU 4685 Prince and Princess(二分匹配+强联通分量)
题意:婚配问题,但是题目并不要求输出最大匹配值,而是让我们输出,一个王子可以与哪些王妃婚配而不影响最大匹配值. 解决办法:先求一次最大匹配,如果有两个已经匹配的王妃,喜欢她们两个的有两个或者以上相同的 ...
- 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 ...
- HDU 4685 Prince and Princess 二分图匹配+tarjan
Prince and Princess 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4685 Description There are n pri ...
- 强连通+二分匹配(hdu4685 Prince and Princess)
Prince and Princess Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Othe ...
- POJ 1904 HDU 4685
这两道题差不多,POJ这道我很久以前就做过,但是比赛的时候居然没想起来.. POJ 这道题的题意是,N个王子每个人都有喜欢的公主,当他们选定一个公主结婚时,必须是的剩下的人也能找到他喜欢的公主结婚. ...
- UVA - 10635 Prince and Princess LCS转LIS
题目链接: http://bak.vjudge.net/problem/UVA-10635 Prince and Princess Time Limit: 3000MS 题意 给你两个数组,求他们的最 ...
随机推荐
- 洛谷P5108 仰望半月的夜空(后缀数组)
题意 题目链接 Sol warning:下面这个做法只有95分,本地拍了1w+组都没找到错误我表示十分无能为力 我们考虑每个串的排名去更新答案,显然排名为\(1\)的后缀的前缀一定是当前长度的字典序最 ...
- vivo如何录制手机视频 分享简单的操作方法
智能手机功能不断的发展更新,手机已经普及到每一个人,在日常的生活或者工作中都离不开手机,手机中的功能例如一些小视频软件都是非常有趣的,vivo如何录制手机视频?下面我们一起来看看吧! 使用工具:手机 ...
- (最简单)红米手机5A的USB调试模式在哪里开启的方法
当我们使用安卓手机链接Pc的时候,或者使用的有些APP比如我们公司营销小组当使用的APP引号精灵,之前使用的老版本就需要开启usb调试模式下使用,现当新版本不需要了,如果手机没有开启usb调试模式,P ...
- Android Studio集成Flutter
首先Flutter中文网教程地址:https://flutterchina.club/get-started/install/ 1.新建环境变量 变量名:PUB_HOSTED_URL 变量值:http ...
- [aspnetcore.apidoc]一款很不错的api文档生成工具
AspNetCore.ApiDoc 简单徐速一下为什么选用了aspnetcore.apidoc 而没有选用swagger 最初我们也有在试用swagger,但总是有些感觉,感觉有点不满意,就但从api ...
- Vue CLI 3.0脚手架如何在本地配置mock数据
前后端分离的开发模式已经是目前前端的主流模式,至于为什么会前后端分离的开发我们就不做过多的阐述,既然是前后端分离的模式开发肯定是离不开前端的数据模拟阶段. 我们在开发的过程中,由于后台接口的没有完成或 ...
- Elasticsearch源码分析 - 源码构建
原文地址:https://mp.weixin.qq.com/s?__biz=MzU2Njg5Nzk0NQ==&mid=2247483694&idx=1&sn=bd03afe5a ...
- mssql sqlserver 表增加列后,视图不会自动更新相关列的两种解决方法分享
摘要: 今天对物理数据表,进行增加列操作后,程序一直显示无法找到相应列,通过仔细比对发现,视图中无相应列更新,下文将具体的解决方法分享如下: 例: create view vw_test as sel ...
- 调试工具gdb
1.1 gdb符号调试器简介 gdb是一个用来调试C和C++程序的功能强大的调试器,它能在程序运行时观察程序的内部结构和内存的使用情况. gdb主要提供以下几种功能: 监视程序中变量值的变化 设置断点 ...
- poi包冲突问题(excel)
1. 所需jar包 涉及的poi (1)poi-3.14.jar (HSSF) 依赖:commons-logging-1.2.jar.log4j-1.2.17.jar.commons-codec.1 ...