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个王子每个人都有喜欢的公主,当他们选定一个公主结婚时,必须是的剩下的人也能找到他喜欢的公主结婚. ...
随机推荐
- alertview 添加图片
- (void)willPresentAlertView:(UIAlertView *)alertView { 在这个方法中, 绘制需要的东西 uiview *myView = [uiview all ...
- 【Python之路】第一篇--Linux基础命令
pwd 命令 查看”当前工作目录“的完整路径 pwd -P # 显示出实际路径,而非使用连接(link)路径:pwd显示的是连接路径 . 表示当前目录 .. 表示上级目录 / 表示根目录 ls ...
- Hibernate查询、连接池、二级缓存
Hibernate第三天: 1. 对象状态 2. session缓存 3. lazy懒加载 4. 映射 一对一对映射 组件/继承映射 目标: 一.hibernate查询 二.hibernate对连接池 ...
- Hadoop上的中文分词与词频统计实践 (有待学习 http://www.cnblogs.com/jiejue/archive/2012/12/16/2820788.html)
解决问题的方案 Hadoop上的中文分词与词频统计实践 首先来推荐相关材料:http://xiaoxia.org/2011/12/18/map-reduce-program-of-rmm-word-c ...
- HDU 3338 Kakuro Extension
网络最大流 TLE了两天的题目.80次Submit才AC,发现是刘汝佳白书的Dinic代码还可以优化.....瞬间无语..... #include<cstdio> #include< ...
- hdu_5787_K-wolf Number(数位DP)
题目链接:hdu_5787_K-wolf Number 题意: 给你一个区间,让你找满足任意k个数位内都没有相同的数字的个数 题解: 因为k不大,就直接将当前pos的前k-1个数传进去就行了 #inc ...
- form异步无刷新提交,提交后可以显示弹出框,否则弹出框会被刷新不见,使用 preventDefault
出错点:确认按钮上.加onclick事件.每次点击都会追加给form追加on监听方法.累加on方法,重复提交 suppress_exception:true 阻止异常 (百度推送 jdk) 向下按 p ...
- ADT(abstract data types)抽象数据类型
1.What is it? An abstract data type is a set of objects together with a set of operations. 抽象数据类型是带有 ...
- R语言笔记4--可视化
接R语言笔记3--实例1 R语言中的可视化函数分为两大类,探索性可视化(陌生数据集,不了解,需要探索里面的信息:偏重于快速,方便的工具)和解释性可视化(完全了解数据集,里面的故事需要讲解别人:偏重全面 ...
- js获取url传递参数,js获取url?号后面的参数
方法一.正则表达式 function getQueryString(name) { var reg = new RegExp("(^|&)" + name + " ...