Codeforces Round #541 (Div. 2) D 并查集 + 拓扑排序
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 并查集 + 拓扑排序的更多相关文章
- Codeforces Round #376 (Div. 2) C. Socks---并查集+贪心
题目链接:http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,每只都有一个颜色,现在他的妈妈要去出差m天,然后让他每天穿第 L 和第 R 只 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #660 (Div. 2) Captain Flint and Treasure 拓扑排序(按照出度、入读两边拓扑排序)
题目链接:Captain Flint and Treasure 题意: 一种操作为 选一个下标 使得ans+=a[i] 且 把a[b[i]]+a[i] 要求每个下标都进行一种这样的操作,问怎么样的 ...
- Codeforces Round #541 (Div. 2)
Codeforces Round #541 (Div. 2) http://codeforces.com/contest/1131 A #include<bits/stdc++.h> us ...
- 并查集+拓扑排序 赛码 1009 Exploration
题目传送门 /* 题意:无向图和有向图的混合图判环: 官方题解:首先对于所有的无向边,我们使用并查集将两边的点并起来,若一条边未合并之前, 两端的点已经处于同一个集合了,那么说明必定存在可行的环(因为 ...
- HDU 1811:Rank of Tetris(并查集+拓扑排序)
http://acm.hdu.edu.cn/showproblem.php?pid=1811 Rank of Tetris Problem Description 自从Lele开发了Rating系 ...
随机推荐
- Adb logcat 抓日志
http://blog.csdn.net/hujiachun1234/article/details/43271149 http://www.cnblogs.com/medsonk/p/6344373 ...
- 100-days: eight
Title: U.S.(美国司法部) accuses rich parents of college entry fraud accuse v.指控,指责,谴责 accuse someone of ...
- java中关于null的一些理解
1.null是Java中的关键字,像public.static.final.它是大小写敏感的,你不能将null写成Null或NULL,编译器将不能识别它们然后报错. 2.null是任何引用类型的默认值 ...
- Excel怎么下拉框多选
打开Exlce, 确定,然后 右击查看代码,把这段代码复制到新建的文件里面 此时Excel会给出提示,选择否,,系统会提示保存,在保存的时候选择启用宏的工作簿然后保存,此时Excel下拉框多选就搞定了 ...
- sqlserver的数据库状态——脱机与联机
1.数据库状态: online:可以对数据库进行访问 offline:数据库无法访问 2.查看数据库状态的方法: (1)使用查询语句: SELECT state_desc FROM SYS.datab ...
- 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 ...
- asp.net core webapi 似乎未安装在 IIS 中承载 .NET Core 项目所需的 AspNetCoreModule。请尝试修复 Visual Studio 以纠正该问题。
安装 DotNetCore.1.1.0-WindowsHosting 后,提示如题错误. 解决办法: ASP.NET Core 应用程序运行,可以选择 IIS Express 也可以选择 自己运行 , ...
- vuex,文件夹整理
不多说直接上图 1,执行事件和调用 2添加模块 3模块内执行过程
- storage封装
storage.js /* storage 主要放项目中的storage相关操作:存取等 */ var storage = { /** 对本地数据进行操作的相关方法,如localStorage,ses ...
- jQuery的鼠标悬停时放大图片的效果
这是一个基于jQuery的效果,当鼠标在小图片上悬停时,会弹出一个大图,该大图会跟随鼠标的移动而移动.这个效果最初源于小敏同志的一个想法,刚开始做的时候只能实现弹出的图片是固定的,不能随鼠标移动,最后 ...