https://codeforces.com/contest/1131/problem/D

题意

给你一个n*m二维偏序表,代表x[i]和y[j]的大小关系,根据表构造大小分别为n,m的x[],y[],使得两个数组中最大的数尽量小

题解

  • 按照偏序表,构造出从小到大的拓扑图
  • 如何解决相等的数的偏序关系?
    • 用并查集缩点后再进行拓扑排序
  • 如何解决最大的数最小?
    • 只需要使得同一层的数相同就行,可以一批处理栈中的元素,对于一批栈中的元素产生的新点,先放进一个容器里,然后等到这批栈清空了,再把这个容器中的点放进栈中

坑点

  • 需要标记已经进栈的并查集,防止同一个并查集重复进栈

代码

  1. #include<bits/stdc++.h>
  2. #define M 4005
  3. using namespace std;
  4. int n,m,i,j,in[M],u,v,c[M],cnt;
  5. char s[M][M];
  6. stack<int>S;
  7. queue<int>Q;
  8. vector<int>g[M];
  9. int fa[M];int fin(int u){return fa[u]==u?u:fa[u]=fin(fa[u]);}
  10. void merge(int u,int v){
  11. int x=fin(u),y=fin(v);
  12. if(x!=y)fa[x]=y;
  13. }
  14. int main(){
  15. cin>>n>>m;
  16. for(i=1;i<=n+m;i++)fa[i]=i;
  17. for(i=1;i<=n;i++)scanf("%s",s[i]+1);
  18. for(i=1;i<=n;i++){
  19. for(j=1;j<=m;j++){
  20. if(s[i][j]=='='){
  21. merge(i,j+n);
  22. }
  23. }
  24. }
  25. for(i=1;i<=n;i++){
  26. for(j=1;j<=m;j++){
  27. u=fin(i);v=fin(j+n);
  28. if(s[i][j]=='>'){
  29. in[u]++;g[v].push_back(u);
  30. }else if(s[i][j]=='<'){
  31. in[v]++;g[u].push_back(v);
  32. }
  33. }
  34. }
  35. cnt++;
  36. for(i=1;i<=n+m;i++){
  37. u=fin(i);
  38. if(in[u]==0&&!c[u]){
  39. S.push(u);
  40. c[u]=cnt;
  41. }
  42. }
  43. while(!S.empty()||!Q.empty()){
  44. if(!S.empty()){
  45. u=S.top();S.pop();
  46. for(i=0;i<g[u].size();i++){
  47. v=g[u][i];in[v]--;
  48. if(in[v]==0){
  49. Q.push(v);
  50. }
  51. }
  52. }else{
  53. cnt++;
  54. while(!Q.empty()){
  55. u=Q.front();Q.pop();
  56. c[u]=cnt;S.push(u);
  57. //cout<<u<<" "<<c[u]<<endl;
  58. }
  59. }
  60. }
  61. for(i=1;i<=n+m;i++){
  62. u=fin(i);
  63. if(in[u]>0){cout<<"No";return 0;}
  64. }
  65. cout<<"Yes"<<endl;
  66. for(i=1;i<=n+m;i++){
  67. u=fin(i);
  68. cout<<c[u]<<" ";
  69. if(i==n)cout<<endl;
  70. }
  71. }

Codeforces Round #541 (Div. 2) D 并查集 + 拓扑排序的更多相关文章

  1. Codeforces Round #376 (Div. 2) C. Socks---并查集+贪心

    题目链接:http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,每只都有一个颜色,现在他的妈妈要去出差m天,然后让他每天穿第 L 和第 R 只 ...

  2. Codeforces Round #286 (Div. 2) B 并查集

    B. Mr. Kitayuta's Colorful Graph time limit per test 1 second memory limit per test 256 megabytes in ...

  3. Codeforces Round #292 (Div. 1) B. Drazil and Tiles 拓扑排序

    B. Drazil and Tiles 题目连接: http://codeforces.com/contest/516/problem/B Description Drazil created a f ...

  4. Codeforces Round #285 (Div. 1) A. Misha and Forest 拓扑排序

    题目链接: 题目 A. Misha and Forest time limit per test 1 second memory limit per test 256 megabytes 问题描述 L ...

  5. Codeforces Round #292 (Div. 2) D. Drazil and Tiles [拓扑排序 dfs]

    传送门 D. Drazil and Tiles time limit per test 2 seconds memory limit per test 256 megabytes Drazil cre ...

  6. Codeforces Round #660 (Div. 2) Captain Flint and Treasure 拓扑排序(按照出度、入读两边拓扑排序)

    题目链接:Captain Flint and Treasure 题意: 一种操作为 选一个下标 使得ans+=a[i] 且 把a[b[i]]+a[i]   要求每个下标都进行一种这样的操作,问怎么样的 ...

  7. Codeforces Round #541 (Div. 2)

    Codeforces Round #541 (Div. 2) http://codeforces.com/contest/1131 A #include<bits/stdc++.h> us ...

  8. 并查集+拓扑排序 赛码 1009 Exploration

    题目传送门 /* 题意:无向图和有向图的混合图判环: 官方题解:首先对于所有的无向边,我们使用并查集将两边的点并起来,若一条边未合并之前, 两端的点已经处于同一个集合了,那么说明必定存在可行的环(因为 ...

  9. HDU 1811:Rank of Tetris(并查集+拓扑排序)

    http://acm.hdu.edu.cn/showproblem.php?pid=1811 Rank of Tetris Problem Description   自从Lele开发了Rating系 ...

随机推荐

  1. Adb logcat 抓日志

    http://blog.csdn.net/hujiachun1234/article/details/43271149 http://www.cnblogs.com/medsonk/p/6344373 ...

  2. 100-days: eight

    Title: U.S.(美国司法部)  accuses rich parents of college entry fraud accuse  v.指控,指责,谴责 accuse someone of ...

  3. java中关于null的一些理解

    1.null是Java中的关键字,像public.static.final.它是大小写敏感的,你不能将null写成Null或NULL,编译器将不能识别它们然后报错. 2.null是任何引用类型的默认值 ...

  4. Excel怎么下拉框多选

    打开Exlce, 确定,然后 右击查看代码,把这段代码复制到新建的文件里面 此时Excel会给出提示,选择否,,系统会提示保存,在保存的时候选择启用宏的工作簿然后保存,此时Excel下拉框多选就搞定了 ...

  5. sqlserver的数据库状态——脱机与联机

    1.数据库状态: online:可以对数据库进行访问 offline:数据库无法访问 2.查看数据库状态的方法: (1)使用查询语句: SELECT state_desc FROM SYS.datab ...

  6. Exploring the world of Android :: Part 1

    This blog is accidentally find out, it tells the story of one of our friends about the exploration o ...

  7. asp.net core webapi 似乎未安装在 IIS 中承载 .NET Core 项目所需的 AspNetCoreModule。请尝试修复 Visual Studio 以纠正该问题。

    安装 DotNetCore.1.1.0-WindowsHosting 后,提示如题错误. 解决办法: ASP.NET Core 应用程序运行,可以选择 IIS Express 也可以选择 自己运行 , ...

  8. vuex,文件夹整理

    不多说直接上图 1,执行事件和调用 2添加模块 3模块内执行过程

  9. storage封装

    storage.js /* storage 主要放项目中的storage相关操作:存取等 */ var storage = { /** 对本地数据进行操作的相关方法,如localStorage,ses ...

  10. jQuery的鼠标悬停时放大图片的效果

    这是一个基于jQuery的效果,当鼠标在小图片上悬停时,会弹出一个大图,该大图会跟随鼠标的移动而移动.这个效果最初源于小敏同志的一个想法,刚开始做的时候只能实现弹出的图片是固定的,不能随鼠标移动,最后 ...