hdu4685:http://acm.hdu.edu.cn/showproblem.php?pid=4685

题意:有n个王子和m个公主,每个王子都会喜欢若干个公主,也就是王子只跟自己喜欢的公主结婚公主就比较悲惨, 跟谁结婚都行,然后输出王子可能的结婚对象。

题解:这一题看了题解之后,也还是只知道是怎么做的,至于为什么那么做还是不懂啊。

解题步奏:首先让王子和喜欢的人之间建立一条边,然后,求一个最大匹配res,然后左边王子加入m-res个虚拟王子,右边加入n-res虚拟公主,所以新加入的王子喜欢所有的公主,所有加入的公主被所有的王子喜欢,然后再跑最大匹配。如果,对于第i个王子,把他喜欢的公主,然后建立一条边,然后缩点,在同一个连通块中的是可以交换的(这里不是很理解)。然后输出。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int N=;
const int M=;
const int INF=0xffffffff;
int n,m,u,cnt,dep,top,atype,newn,newm;
int dfn[N],low[N],vis[N],head[N],st[N],belong[N],lx[N],cy[N];
bool visit[N],g[N][N];
vector<int>ans;
int path(int u){
for(int i=;i<=newm;i++){
if(!visit[i]&&g[u][i]){
visit[i]=;
if(cy[i]==-||path(cy[i])){
cy[i]=u;
return ;
}
}
}
return ;
}
int maxmatch(){
memset(cy,-,sizeof(cy));
int res=;
for(int i=;i<=newn;i++){
memset(visit,,sizeof(visit));
res+=path(i);
}
return res;
}
struct Edge{
int to,next;
} edge[M];
void init(){
cnt=dep=top=atype=;
memset(head,-,sizeof(head));
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(vis,,sizeof(vis));
memset(belong,,sizeof(belong));
memset(g,,sizeof(g));
}
void addedge(int u,int v){
edge[cnt].to=v;
edge[cnt].next=head[u];
head[u]=cnt++;
} void Tarjan(int u){
dfn[u]=low[u]=++dep;
st[top++]=u;
vis[u]=;
for(int i=head[u]; i!=-; i=edge[i].next){
int v=edge[i].to;
if(!dfn[v]){
Tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(vis[v]){
low[u]=min(low[u],dfn[v]);
}
}
int j;
if(dfn[u]==low[u]){
atype++;
do{
j=st[--top];
belong[j]=atype;
vis[j]=;
}
while(u!=j);
}
}
int cas;
int main(){
scanf("%d",&cas);
int tt=;
while(cas--){
scanf("%d%d",&n,&m);
init();
for(int i=;i<=n;i++){
int temp;
scanf("%d",&temp);
for(int j=;j<=temp;j++){
scanf("%d",&u);
g[i][u]=;
}
}
newn=n;newm=m;
int ans1=maxmatch();
newn=n+m-ans1;
newm=n+m-ans1;
for(int i=n+;i<=newn;i++){
for(int j=;j<=newm;j++)
g[i][j]=;
}
for(int i=;i<=newn;i++){
for(int j=+m;j<=newm;j++)
g[i][j]=;
}
maxmatch();
memset(lx,-,sizeof(lx));
for(int i=;i<=newm;i++){
if(cy[i]!=-)
lx[cy[i]]=i;
}
for(int i=;i<=newn;i++){
for(int j=;j<=newm;j++){
if(g[i][j]&&j!=lx[i])
addedge(lx[i],j);
}
}
for(int i=;i<=newm;i++)
if(!dfn[i])
Tarjan(i);
printf("Case #%d:\n",tt++);
for(int i=;i<=n;i++){
ans.clear();
for(int j = ; j <= m;j++)
if(g[i][j] && belong[j] == belong[lx[i]])
ans.push_back(j);
int sz = ans.size();
printf("%d",sz);
for(int i = ;i < sz;i++)
printf(" %d",ans[i]);
printf("\n");
}
}
}

Prince and Princess的更多相关文章

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

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

  2. 10635 - Prince and Princess

    Problem D Prince and Princess Input: Standard Input Output: Standard Output Time Limit: 3 Seconds In ...

  3. UVa10653.Prince and Princess

    题目连接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  4. uva 10635 - Prince and Princess(LCS)

    题目连接:10635 - Prince and Princess 题目大意:给出n, m, k,求两个长度分别为m + 1 和 k + 1且由1~n * n组成的序列的最长公共子序列长的. 解题思路: ...

  5. Prince and Princess HDU - 4685(匹配 + 强连通)

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

  6. UVA - 10635 Prince and Princess LCS转LIS

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

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

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

  8. 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 ...

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

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

  10. Prince and princess——需要优化的DP

    一个时间效率为o(nlogn)的算法求公共子序列的应用 Prince and princess 题目大意(已翻译 ) 在nxn的棋盘上,王子和公主玩游戏.棋盘上的正方形编号为1.2.3 ... n * ...

随机推荐

  1. one problem about Apple Keychain in use

    解决方案 Add Security.framework, then rebuild. Sometimes I find I have to build clean and then rebuild. ...

  2. linux之ioctl函数解析

    [lingyun@localhost ioctl_1]$ ls ipconfig.c [lingyun@localhost ioctl_1]$ cat ipconfig.c  /*********** ...

  3. 网络学习笔记----01--pathping跟踪数据包路径

    操作系统win7 Pathping主要用于提供有关在来源和目标之间的中间跃点处的网络滞后和网络丢失的信息. Pathping将多个回显请求消息发送到来源和目标之间的各个路由器一段时间,然后根据各个路由 ...

  4. Oracle 设置archivelog错误解决方案

    在Oracle 数据库的实际应用中,开启archivelog模式是必不可少的,但是在设置archivelog的过程中,可能因为不小心出现错误,导致数据库无法启动,本案例就是一种情况. 误操作现象: 设 ...

  5. POSIX字符类型

    [:alnum:] 字母与数字 [:alpha:] 字母 [:blank:] 空格与制表符 [:cntrl:] 控制字符 [:digit:] 数字 [:graph:] 可打印的与可见的(不包括空格)字 ...

  6. 正则表达式工具类,正则表达式封装,Java正则表达式

    正则表达式工具类 正则表达式封装 Java正则表达式 >>>>>>>>>>>>>>>>>>& ...

  7. Parallel类(简化Task 操作)

    Parallel类 Parallel类是对线程的一个很好抽象.该类位于System.Threading.Tasks命名空间中,提供了数据和任务并行性. 1.用Parallel.For()方法循环 // ...

  8. ( 转 )Github配置

    以下转自 http://liuzhijun.iteye.com/blog/1457207 有问题请联系我删除. -----———————————————————————— 如果你的代码不知道放哪里好, ...

  9. 模版引擎(NVelocity)开发

    在net中用模版开发,在handler中用到了大量的html代码.为解决这个问题,我可以采用模版引擎(NVelocity)进行开发.1.首先需要将NVelocity.dll文件放入项目,其次引用.2. ...

  10. 如何写robots.txt?

    robin 发表在 八月 2, 2006 在国内,网站管理者似乎对robots.txt并没有引起多大重视,应一些朋友之请求,今天想通过这篇文章来简单谈一下robots.txt的写作. robots.t ...