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

题意

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

题解

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

坑点

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

代码

#include<bits/stdc++.h>
#define M 4005
using namespace std;
int n,m,i,j,in[M],u,v,c[M],cnt;
char s[M][M];
stack<int>S;
queue<int>Q;
vector<int>g[M];
int fa[M];int fin(int u){return fa[u]==u?u:fa[u]=fin(fa[u]);}
void merge(int u,int v){
int x=fin(u),y=fin(v);
if(x!=y)fa[x]=y;
}
int main(){
cin>>n>>m;
for(i=1;i<=n+m;i++)fa[i]=i;
for(i=1;i<=n;i++)scanf("%s",s[i]+1);
for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
if(s[i][j]=='='){
merge(i,j+n);
}
}
}
for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
u=fin(i);v=fin(j+n);
if(s[i][j]=='>'){
in[u]++;g[v].push_back(u);
}else if(s[i][j]=='<'){
in[v]++;g[u].push_back(v);
}
}
}
cnt++;
for(i=1;i<=n+m;i++){
u=fin(i);
if(in[u]==0&&!c[u]){
S.push(u);
c[u]=cnt;
}
}
while(!S.empty()||!Q.empty()){
if(!S.empty()){
u=S.top();S.pop();
for(i=0;i<g[u].size();i++){
v=g[u][i];in[v]--;
if(in[v]==0){
Q.push(v);
}
}
}else{
cnt++;
while(!Q.empty()){
u=Q.front();Q.pop();
c[u]=cnt;S.push(u);
//cout<<u<<" "<<c[u]<<endl;
}
}
}
for(i=1;i<=n+m;i++){
u=fin(i);
if(in[u]>0){cout<<"No";return 0;}
}
cout<<"Yes"<<endl;
for(i=1;i<=n+m;i++){
u=fin(i);
cout<<c[u]<<" ";
if(i==n)cout<<endl;
}
}

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. VS PDB文件

    转 PDB文件 PDB(Program Data Base),意即程序的基本数据,是VS编译链接时生成的文件.DPB文件主要存储了VS调试程序时所需要的基本信息,主要包括源文件名.变量名.函数名.FP ...

  2. pa sslvpn配置

    1.新建隧道接口 2.新建区域,并将该区域与上一步中的隧道接口关联 3.新建本地证书及配置文件 (1) 常见名称处填写防火墙外网口IP. 添加成功后的证书信息如下: 2)新建SSL/TLS服务配置文件 ...

  3. 绝对路径${pageContext.request.contextPath}

    ${pageContext.request.contextPath}用于解决使用相对路径时出现的问题,它的作用是取出所部署项目的名字. 如图,${pageContext.request.context ...

  4. hadoop fs 命令使用

    参考:https://segmentfault.com/a/1190000002672666 命令基本格式: hadoop fs -cmd < args > 1.ls hadoop fs ...

  5. Bootstrap(6)图标菜单按钮组件

    一.小图标组件 Bootstrap 提供了免费的 263 个小图标(数了两次),具体可以参考中文官网的组件链接:http://v3.bootcss.com/components/#glyphicons ...

  6. 配置go语言编辑环境 - goland

    快捷键: 移动行 Alt + Shift + up/down 行内(选中)移动(到头尾/删除) Ctrl(Shift) + left/rignt(Home/End/Backspace) 插入新行 Sh ...

  7. poj_1979(dfs)

    Red and Black There is a rectangular room, covered with square tiles. Each tile is colored either re ...

  8. iOS.FileSystem.HardLinkAndSymbolicLink

    关于iOS中的硬连接和符号连接(软连接),iOS其实是Unix的变体, 所以在这方面也继承了Unix的特性,下面这个连接比较详细的进行了 类比说明. 1. http://www.tanhao.me/p ...

  9. IOS 将图片转换为圆角图

    UIImage+wiRoundedRectImage.h #import <UIKit/UIKit.h> @interface UIImage (wiRoundedRectImage) + ...

  10. mysql if--then--else --endif 问题

    if 0 =resultValue then set @m = 2; else set @m =1; end if if 0 =resultValue then set @m = 2; else se ...