HDU 4685 Prince and Princess
强连通分量,看大神的题解才会写的....
http://www.cnblogs.com/kuangbin/p/3261157.html
数据量有点大,第一次Submit 2995ms过的,时限3000ms,差一点就TLE了。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std; const int maxn = + ;
int N, M;
vector<int>Cun[maxn];
vector<int>G[maxn];
vector<int>FG[maxn];
int nx, ny, Time, Block;
int g[maxn][maxn];
int cx[maxn], cy[maxn];
int mk[maxn];
int flag[maxn], dfn[maxn], Belong[maxn];
struct Point
{
int id, dfn;
} point[maxn]; int Scan()
{
int res = , ch, flag = ; if ((ch = getchar()) == '-') //判断正负
flag = ; else if (ch >= '' && ch <= '') //得到完整的数
res = ch - '';
while ((ch = getchar()) >= '' && ch <= '')
res = res * + ch - ''; return flag ? -res : res;
} bool cmp(const Point&a, const Point&b)
{
return a.dfn>b.dfn;
} int path(int u)
{
for (int v = ; v <= ny; v++)
{
if (g[u][v] && !mk[v])
{
mk[v] = ;
if (cy[v] == - || path(cy[v]))
{
cx[u] = v;
cy[v] = u;
return ;
}
}
}
return ;
} int MaxMatch()
{
int res = ;
memset(cx, -, sizeof(cx));
memset(cy, -, sizeof(cy));
for (int i = ; i <= nx; i++)
{
if (cx[i] == -)
{
memset(mk, , sizeof(mk));
res = res + path(i);
}
}
return res;
} void dfs(int now)
{
flag[now] = ;
for (int i = ; i<G[now].size(); i++)
if (!flag[G[now][i]])
dfs(G[now][i]);
Time++;
dfn[now] = Time;
} void Dfs(int now)
{
Belong[now] = Block;
for (int i = ; i<FG[now].size(); i++)
if (!Belong[FG[now][i]])
Dfs(FG[now][i]);
} int main()
{
int CA;
CA = Scan();
for (int er = ; er <= CA; er++){
N = Scan();
M = Scan();
memset(flag, , sizeof(flag));
memset(dfn, , sizeof(dfn));
memset(Belong, , sizeof(Belong));
Time = , Block = ;
for (int i = ; i<maxn; i++) G[i].clear();
for (int i = ; i<maxn; i++) Cun[i].clear();
for (int i = ; i<maxn; i++) FG[i].clear();
memset(g, , sizeof(g));
nx = N, ny = M; for (int i = ; i <= N; i++)
{
int ToT, To;
ToT = Scan();
while (ToT--)
{
To = Scan();
Cun[i].push_back(To);
g[i][To] = ;
}
sort(Cun[i].begin(), Cun[i].end());
}
int res = MaxMatch(); memset(g, , sizeof(g));
int A = M - res, B = N - res;//A表示虚拟王子数量,B表示虚拟妹子数量
nx = N + A, ny = M + B;
if (B>)//王子有单身
{
for (int j = M + ; j <= M + B; j++)
for (int i = ; i <= N; i++)
{
g[i][j] = ;
G[i].push_back(j + nx);
FG[j + nx].push_back(i);
}
}
if (A>)
{
for (int i = N + ; i <= N + A; i++)
for (int j = ; j <= M; j++)
{
g[i][j] = ;
G[i].push_back(j + nx);
FG[j + nx].push_back(i);
}
}
for (int i = ; i <= N; i++)
for (int j = ; j<Cun[i].size(); j++)
{
g[i][Cun[i][j]] = ;
G[i].push_back(Cun[i][j] + nx);
FG[Cun[i][j] + nx].push_back(i);
}
MaxMatch();
for (int i = ; i <= ny; i++)
if (cy[i] != -)
{
G[i + nx].push_back(cy[i]);
FG[cy[i]].push_back(i + nx);
} for (int i = ; i <= nx + ny; i++) if (!dfn[i]) dfs(i);
for (int i = ; i<nx + ny; i++) point[i].id = i + , point[i].dfn = dfn[i + ];
sort(point, point + nx + ny, cmp);
for (int i = ; i<nx + ny; i++)
if (!Belong[point[i].id])
Block++, Dfs(point[i].id);
int RT;
int ans[maxn];
printf("Case #%d:\n", er);
for (int i = ; i <= N; i++)
{
RT = ;
for (int j = ; j<Cun[i].size(); j++)
{
if (Belong[i] == Belong[Cun[i][j] + nx])
{
ans[RT] = Cun[i][j];
RT++;
}
}
printf("%d", RT);
for (int x = ; x<RT; x++) printf(" %d", ans[x]);
printf("\n");
}
}
return ;
}
HDU 4685 Prince and Princess的更多相关文章
- HDU 4685 Prince and Princess 二分图匹配+tarjan
Prince and Princess 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4685 Description There are n pri ...
- 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(二分图+强连通分量)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4685 题意:给出n个王子和m个公主.每个王子有一些自己喜欢的公主可以匹配.设最大匹配为M.那么对于每个 ...
- hdu 4685 Prince and Princess(匈牙利算法 连通分量)
看了别人的题解.须要用到匈牙利算法的强连通算法 #include<cstdio> #include<algorithm> #include<vector> #pra ...
- HDU 4685 Prince and Princess(二分匹配+强联通分量)
题意:婚配问题,但是题目并不要求输出最大匹配值,而是让我们输出,一个王子可以与哪些王妃婚配而不影响最大匹配值. 解决办法:先求一次最大匹配,如果有两个已经匹配的王妃,喜欢她们两个的有两个或者以上相同的 ...
- Prince and Princess HDU - 4685(匹配 + 强连通)
Prince and Princess Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Othe ...
- HDU4685:Prince and Princess(二分图匹配+tarjan)
Prince and Princess Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Othe ...
- 强连通+二分匹配(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个王子每个人都有喜欢的公主,当他们选定一个公主结婚时,必须是的剩下的人也能找到他喜欢的公主结婚. ...
随机推荐
- js 中创建对象
对象是什么 从JavaScript定义上讲对象是无序属性的集合,其属性可以包含基本值.对象或函数.也就是说对象是一组没有特定顺序的属性,每个属性会映射到一个值上,是一组键值对,值可以是数据或对象. 最 ...
- PowerDesigner 的mysql PDM 的COMMENT注释
PowerDesigner 的mysql PDM 的COMMENT注释 2012-11-01 15:38 4447人阅读 评论(0) 举报 分类: 数据库相关(7) PowerDesigner 的my ...
- linux下写脚本时-gt是什么意思
-eq 等于-ne 不等于-gt 大于-ge 大于等于-lt 小于-le 小于等于
- Linux openvswitch性能调优
Increasing the flow-eviction threshold The threshold is a type of limit on the number of flows that ...
- JPA的介绍
一.JPA概述 1.JPA是什么? JPA:Java Persistence API:用于对象持久化的 API,JPA是Java EE 5.0 平台标准的 ORM 规范, 使得应用程序以统一的方式访问 ...
- HDU 3452 Bonsai
可以转化成最小割的求解,题目其实就是要求把点分成两个集合,增加一个超级汇点,一部分的点在根节点所在集合内,一部分节点在超级汇点所在的集合内,这两就分开了,又要求费用最小,那么就是最小割. #inclu ...
- bzoj1977
1977: [BeiJing2010组队]次小生成树 Tree Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 3001 Solved: 751[Su ...
- QTP脚本程序(原创自编)
'编写哨位台程序测试脚本,实现功能如下:'1.自动添加100条查哨换岗人员记录,'2.自动添加美电.海康,大华视频.'3.自动配置视频设备.'4.检查后台进程是否存在.'5.视频轮巡.'####### ...
- 再谈KMP
昨天讲解了字典树和AC自动机后感觉整个人都蒙掉了.还好就是自己今天在网上看见一篇对KMP讲解非常详细的帖子,果断收藏.(点击这里查看) 然后代码的实现也就简单分析一些了,具体的知识点大家直接自己链接过 ...
- Xcode6无法用xib得问题解决方法
1.创建一个新工程,选择singleView application 2.将storyboard和launchscreen删除,选择moveToTrash 3.删除info.plist文件中Main ...